<?php
namespace api\Controllers;

use api\ApiRouter;
use Domain\Order\OrderAdminService;
use Domain\Order\OrderRepository;

class OrdersApiController
{
    private $service;
    private $orderRepo;

    public function __construct(OrderAdminService $service, OrderRepository $orderRepo)
    {
        $this->service = $service;
        $this->orderRepo = $orderRepo;
    }

    public function list(): void
    {
        if (!ApiRouter::requireMethod('GET')) {
            return;
        }

        $filters = [
            'status' => isset($_GET['status']) ? $_GET['status'] : '',
            'paid' => isset($_GET['paid']) ? $_GET['paid'] : '',
            'date_from' => isset($_GET['date_from']) ? $_GET['date_from'] : '',
            'date_to' => isset($_GET['date_to']) ? $_GET['date_to'] : '',
            'updated_since' => isset($_GET['updated_since']) ? $_GET['updated_since'] : '',
            'number' => isset($_GET['number']) ? $_GET['number'] : '',
            'client' => isset($_GET['client']) ? $_GET['client'] : '',
        ];

        $page = max(1, (int)(isset($_GET['page']) ? $_GET['page'] : 1));
        $perPage = max(1, min(100, (int)(isset($_GET['per_page']) ? $_GET['per_page'] : 50)));

        $result = $this->orderRepo->listForApi($filters, $page, $perPage);

        ApiRouter::sendSuccess($result);
    }

    public function get(): void
    {
        if (!ApiRouter::requireMethod('GET')) {
            return;
        }

        $id = (int)(isset($_GET['id']) ? $_GET['id'] : 0);
        if ($id <= 0) {
            ApiRouter::sendError('BAD_REQUEST', 'Missing or invalid id parameter', 400);
            return;
        }

        $order = $this->orderRepo->findForApi($id);
        if ($order === null) {
            ApiRouter::sendError('NOT_FOUND', 'Order not found', 404);
            return;
        }

        ApiRouter::sendSuccess($order);
    }

    public function change_status(): void
    {
        if (!ApiRouter::requireMethod('PUT')) {
            return;
        }

        $id = (int)(isset($_GET['id']) ? $_GET['id'] : 0);
        if ($id <= 0) {
            ApiRouter::sendError('BAD_REQUEST', 'Missing or invalid id parameter', 400);
            return;
        }

        $body = ApiRouter::getJsonBody();
        if ($body === null || !isset($body['status_id'])) {
            ApiRouter::sendError('BAD_REQUEST', 'Missing status_id in request body', 400);
            return;
        }

        $statusId = (int)$body['status_id'];
        $sendEmail = !empty($body['send_email']);

        $order = $this->orderRepo->findRawById($id);
        if ($order === null) {
            ApiRouter::sendError('NOT_FOUND', 'Order not found', 404);
            return;
        }

        $result = $this->service->changeStatus($id, $statusId, $sendEmail);

        ApiRouter::sendSuccess([
            'order_id' => $id,
            'status_id' => $statusId,
            'changed' => !empty($result['result']),
        ]);
    }

    public function set_paid(): void
    {
        if (!ApiRouter::requireMethod('PUT')) {
            return;
        }

        $id = (int)(isset($_GET['id']) ? $_GET['id'] : 0);
        if ($id <= 0) {
            ApiRouter::sendError('BAD_REQUEST', 'Missing or invalid id parameter', 400);
            return;
        }

        $order = $this->orderRepo->findRawById($id);
        if ($order === null) {
            ApiRouter::sendError('NOT_FOUND', 'Order not found', 404);
            return;
        }

        $body = ApiRouter::getJsonBody();
        $sendEmail = ($body !== null && !empty($body['send_email']));

        $this->service->setOrderAsPaid($id, $sendEmail);

        ApiRouter::sendSuccess([
            'order_id' => $id,
            'paid' => 1,
        ]);
    }

    public function set_unpaid(): void
    {
        if (!ApiRouter::requireMethod('PUT')) {
            return;
        }

        $id = (int)(isset($_GET['id']) ? $_GET['id'] : 0);
        if ($id <= 0) {
            ApiRouter::sendError('BAD_REQUEST', 'Missing or invalid id parameter', 400);
            return;
        }

        $order = $this->orderRepo->findRawById($id);
        if ($order === null) {
            ApiRouter::sendError('NOT_FOUND', 'Order not found', 404);
            return;
        }

        $this->service->setOrderAsUnpaid($id);

        ApiRouter::sendSuccess([
            'order_id' => $id,
            'paid' => 0,
        ]);
    }
}
