<?php

declare(strict_types=1);

namespace App\Modules\Reporting\Application\Services;

use App\Repositories\BankAccountRepository;
use App\Repositories\BankTransactionRepository;
use App\Services\BankAccountService;
use App\Services\BankTransactionService;
use PDO;

final class BankReportService
{
    private BankTransactionRepository $bankTransactionRepo;
    private BankTransactionService $bankTransactionService;
    private BankAccountRepository $bankAccountRepo;
    private BankAccountService $bankAccountService;

    public function __construct(private PDO $pdo)
    {
        $this->bankTransactionRepo = new BankTransactionRepository($pdo);
        $this->bankTransactionService = new BankTransactionService($pdo, $this->bankTransactionRepo);
        $this->bankAccountRepo = new BankAccountRepository($pdo);
        $this->bankAccountService = new BankAccountService($pdo, $this->bankAccountRepo);
    }

    public function ensureSchema(): void
    {
        $this->bankTransactionService->ensureSchema();
        $this->bankAccountService->ensureSchema();
    }

    /** @return array<string,mixed> */
    public function statement(int $bankId, string $from, string $to, string $q): array
    {
        return $this->bankTransactionService->loadStatement($bankId, trim($from), trim($to), trim($q));
    }

    /** @return array<string,mixed> */
    public function balances(string $q): array
    {
        return $this->bankAccountService->balancesReport(trim($q)) + ['q' => trim($q)];
    }
}
