<?php
declare(strict_types=1);

namespace App\Modules\Accounting\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Core\App;
use App\Modules\Accounting\Services\AccountStatementService;

class AccountStatementController extends Controller
{
    public function customerStatement(Request $request): Response
    {
        $this->authorize('accounting.statements.view');
        $db = App::getInstance()->db();

        $memberId = (int) $request->get('member_id', 0);
        $dateFrom = $request->get('date_from', '');
        $dateTo = $request->get('date_to', '');

        $statement = null;
        $member = null;

        if ($memberId > 0) {
            $member = $db->selectOne("SELECT id, full_name_ar, membership_number, credit_limit, credit_balance FROM members WHERE id = ?", [$memberId]);
            $statement = AccountStatementService::getCustomerStatement(
                $memberId,
                $dateFrom ?: null,
                $dateTo ?: null
            );
        }

        $members = $db->select("SELECT id, full_name_ar, membership_number FROM members WHERE is_archived = 0 ORDER BY full_name_ar LIMIT 1000");

        return $this->view('Accounting.Views.statements.customer', [
            'members' => $members,
            'member' => $member,
            'statement' => $statement,
            'filters' => ['member_id' => $memberId, 'date_from' => $dateFrom, 'date_to' => $dateTo],
        ]);
    }

    public function supplierStatement(Request $request): Response
    {
        $this->authorize('accounting.statements.view');
        $db = App::getInstance()->db();

        $supplierId = (int) $request->get('supplier_id', 0);
        $dateFrom = $request->get('date_from', '');
        $dateTo = $request->get('date_to', '');

        $statement = null;
        $supplier = null;

        if ($supplierId > 0) {
            $supplier = $db->selectOne("SELECT id, name_ar, code, credit_limit, credit_balance FROM suppliers WHERE id = ?", [$supplierId]);
            $statement = AccountStatementService::getSupplierStatement(
                $supplierId,
                $dateFrom ?: null,
                $dateTo ?: null
            );
        }

        $suppliers = $db->select("SELECT id, name_ar, code FROM suppliers WHERE is_archived = 0 ORDER BY name_ar");

        return $this->view('Accounting.Views.statements.supplier', [
            'suppliers' => $suppliers,
            'supplier' => $supplier,
            'statement' => $statement,
            'filters' => ['supplier_id' => $supplierId, 'date_from' => $dateFrom, 'date_to' => $dateTo],
        ]);
    }

    public function dailyCash(Request $request): Response
    {
        $this->authorize('accounting.cash.view');
        $db = App::getInstance()->db();

        $date = $request->get('date', date('Y-m-d'));

        $movements = $db->select(
            "SELECT * FROM daily_cash_movements WHERE movement_date = ? ORDER BY bank_account_id, safe_id",
            [$date]
        );

        $bankAccounts = $db->select("SELECT id, account_name_ar FROM bank_accounts WHERE is_active = 1");

        return $this->view('Accounting.Views.statements.daily_cash', [
            'movements' => $movements,
            'bankAccounts' => $bankAccounts,
            'date' => $date,
        ]);
    }
}
