<?php

declare(strict_types=1);

namespace ApiModel\Action;

use ApiModel\Service\InvoiceService;
use ApiModel\Service\PaymentService;
use ApiModel\Shared\Http\HttpUtil;
use ApiModel\Shared\Support\AuthContext;
use ApiModel\Shared\Support\CompanyContext;
use Psr\Http\Message\ServerRequestInterface;

final class SalesAction
{
    public function __construct(
        private readonly InvoiceService $invoices,
        private readonly PaymentService $payments
    ) {}

    /** @return list<array<string, mixed>> */
    public function listInvoices(ServerRequestInterface $request): array
    {
        AuthContext::jwtUser($request);
        $ctx = CompanyContext::require($request);

        return $this->invoices->listInvoices($ctx['company_id'], $ctx['branch_id'] ?? null);
    }

    /** @return array<string, mixed> */
    public function getInvoice(ServerRequestInterface $request, string $id): array
    {
        AuthContext::jwtUser($request);
        $ctx = CompanyContext::require($request);

        return $this->invoices->getInvoice($ctx['company_id'], $id);
    }

    /** @return array<string, mixed> */
    public function saveInvoice(ServerRequestInterface $request): array
    {
        $user = AuthContext::jwtUser($request);
        $ctx = CompanyContext::require($request);
        $body = HttpUtil::parseJson($request);

        return $this->invoices->saveInvoice($ctx['company_id'], $ctx['branch_id'] ?? null, $user['userId'], $body);
    }

    /** @return array{ok: true} */
    public function deleteInvoice(ServerRequestInterface $request, string $id): void
    {
        AuthContext::jwtUser($request);
        $ctx = CompanyContext::require($request);
        $this->invoices->deleteInvoice($ctx['company_id'], $id);
    }

    /** @return list<array<string, mixed>> */
    public function listUnpaidInvoices(ServerRequestInterface $request, string $customerId): array
    {
        AuthContext::jwtUser($request);
        $ctx = CompanyContext::require($request);

        return $this->invoices->listUnpaidByCustomer($ctx['company_id'], $customerId);
    }

    /** @return list<array<string, mixed>> */
    public function listPayments(ServerRequestInterface $request): array
    {
        AuthContext::jwtUser($request);
        $ctx = CompanyContext::require($request);

        return $this->payments->listPayments($ctx['company_id'], $ctx['branch_id'] ?? null);
    }

    /** @return array<string, mixed> */
    public function getPayment(ServerRequestInterface $request, string $id): array
    {
        AuthContext::jwtUser($request);
        $ctx = CompanyContext::require($request);

        return $this->payments->getPayment($ctx['company_id'], $id);
    }

    /** @return array<string, mixed> */
    public function savePayment(ServerRequestInterface $request): array
    {
        $user = AuthContext::jwtUser($request);
        $ctx = CompanyContext::require($request);
        $body = HttpUtil::parseJson($request);

        return $this->payments->savePayment($ctx['company_id'], $ctx['branch_id'] ?? null, $user['userId'], $body);
    }

    /** @return array{ok: true} */
    public function deletePayment(ServerRequestInterface $request, string $id): void
    {
        AuthContext::jwtUser($request);
        $ctx = CompanyContext::require($request);
        $this->payments->deletePayment($ctx['company_id'], $id);
    }
}
