<?php

namespace App\Controllers\Api\V1;

use App\Controllers\BaseController;
use App\Services\Connections\ConnectionService;

class ConnectionController extends BaseController
{
    private ConnectionService $connections;

    public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->connections = new ConnectionService();
    }

    public function send()
    {
        $user = $this->currentUser();
        $input = $this->request->getJSON(true);

        if (empty($input['recipient_user_id'])) {
            return $this->jsonValidationError(['recipient_user_id' => 'recipient_user_id is required.']);
        }

        try {
            $connection = $this->connections->sendRequest($user->id, (int) $input['recipient_user_id']);
        } catch (\InvalidArgumentException $e) {
            return $this->jsonError([['code' => 'INVALID_REQUEST', 'message' => $e->getMessage()]], 422);
        } catch (\RuntimeException $e) {
            return $this->jsonError([['code' => 'CONNECTION_ERROR', 'message' => $e->getMessage()]], 409);
        }

        return $this->jsonSuccess(['connection' => $connection], [], 201);
    }

    public function accept(int $id)
    {
        try {
            $connection = $this->connections->acceptRequest($id, $this->currentUser()->id);
        } catch (\RuntimeException $e) {
            return $this->jsonError([['code' => 'CONNECTION_ERROR', 'message' => $e->getMessage()]], 422);
        }
        return $this->jsonSuccess(['connection' => $connection]);
    }

    public function reject(int $id)
    {
        try {
            $this->connections->rejectRequest($id, $this->currentUser()->id);
        } catch (\RuntimeException $e) {
            return $this->jsonError([['code' => 'CONNECTION_ERROR', 'message' => $e->getMessage()]], 422);
        }
        return $this->jsonSuccess(['message' => 'Request declined.']);
    }

    public function remove(int $id)
    {
        try {
            $this->connections->removeConnection($id, $this->currentUser()->id);
        } catch (\RuntimeException $e) {
            return $this->jsonError([['code' => 'CONNECTION_ERROR', 'message' => $e->getMessage()]], 404);
        }
        return $this->jsonSuccess(['message' => 'Connection removed.']);
    }

    public function myConnections()
    {
        return $this->jsonSuccess(['connections' => $this->connections->listConnections($this->currentUser()->id)]);
    }

    public function pendingReceived()
    {
        return $this->jsonSuccess(['requests' => $this->connections->pendingReceived($this->currentUser()->id)]);
    }

    public function pendingSent()
    {
        return $this->jsonSuccess(['requests' => $this->connections->pendingSent($this->currentUser()->id)]);
    }

    public function statusWith(int $userId)
    {
        return $this->jsonSuccess($this->connections->statusBetween($this->currentUser()->id, $userId));
    }
}
