<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Core\Auth;
use App\Core\Csrf;
use App\Core\DB;
use App\Core\Request;
use App\Core\Response;
use App\Modules\Finance\Application\Services\FinanceRouteContext;
use App\Repositories\BankTransactionRepository;
use App\Services\BankTransactionService;
use Throwable;

final class BankTransactionController
{
    private \PDO $pdo; private BankTransactionRepository $repo; private BankTransactionService $service; private FinanceRouteContext $routeContext;
    public function __construct(){ $this->pdo=DB::pdo(); $this->repo=new BankTransactionRepository($this->pdo); $this->service=new BankTransactionService($this->pdo,$this->repo); $this->routeContext = new FinanceRouteContext(); }
    public function index(): void { Auth::requireLogin(); $this->service->ensureSchema(); require_permission('banks.txns.view'); $q=trim((string)Request::get('q','')); $bankId=(int)Request::get('bank_account_id',0); $type=trim((string)Request::get('action_type','')); $page=max(1,(int)Request::get('page',1)); $pagination=paginate_rows($this->repo->search($q,$bankId,$type), $page, 25); Response::view('pages/banks/transactions_index', ['rows'=>$pagination['items'],'page'=>$pagination['page'],'per_page'=>$pagination['per_page'],'total_pages'=>$pagination['total_pages'],'total_rows'=>$pagination['total_rows'],'from_row'=>$pagination['from'],'to_row'=>$pagination['to'],'bankAccounts'=>$this->repo->activeBankAccounts(),'q'=>$q,'bank_id'=>$bankId,'action_type'=>$type,'page_title'=>'حركات البنوك','breadcrumbs'=>[$this->routeContext->dashboardCrumb(),['label'=>'البنوك'],['label'=>'حركات البنوك']], 'bank_transactions_url'=>$this->routeContext->bankTransactionsPath(), 'bank_transaction_form_url'=>$this->routeContext->bankTransactionFormPath(), 'bank_statement_url'=>$this->routeContext->bankStatementPath()]); }
    public function form(): void { Auth::requireLogin(); $this->service->ensureSchema(); require_permission('banks.txns.create'); $data=$this->service->loadForm(); if(Request::isPost()){ try{ Csrf::verify(); $this->service->save(Request::all(), (int)(current_user_id() ?: 0)); Response::flash('success','تم تسجيل الحركة البنكية.'); Response::redirect($this->routeContext->bankTransactionsPath()); return; } catch(Throwable $e){ $data['errors']=[$e->getMessage()]; $data['form']=array_merge($data['form'], Request::all()); } } Response::view('pages/banks/transaction_form', $data + ['page_title'=>'حركة بنك','breadcrumbs'=>[$this->routeContext->dashboardCrumb(),['label'=>'البنوك'],['label'=>'حركات البنوك','url'=>$this->routeContext->bankTransactionsPath()],['label'=>'حركة بنك']], 'bank_transactions_url'=>$this->routeContext->bankTransactionsPath()]); }
    public function statement(): void { Auth::requireLogin(); $this->service->ensureSchema(); require_permission('banks.txns.view'); $data=$this->service->loadStatement((int)Request::get('bank_account_id',0), trim((string)Request::get('from','')), trim((string)Request::get('to','')), trim((string)Request::get('q',''))); $page=max(1,(int)Request::get('page',1)); $pagination=paginate_rows($data['rows'], $page, 25); Response::view('pages/banks/statement', ['bankAccounts'=>$data['bankAccounts'],'bank_account_id'=>$data['bankId'],'account'=>$data['account'],'from'=>$data['from'],'to'=>$data['to'],'q'=>$data['q'],'rows'=>$pagination['items'],'page'=>$pagination['page'],'per_page'=>$pagination['per_page'],'total_pages'=>$pagination['total_pages'],'total_rows'=>$pagination['total_rows'],'from_row'=>$pagination['from'],'to_row'=>$pagination['to'],'opening'=>$data['opening'],'closing'=>$data['closing'],'totals'=>$data['totals'],'page_title'=>'كشف حساب بنك','breadcrumbs'=>[$this->routeContext->dashboardCrumb(),['label'=>'البنوك'],['label'=>'كشف حساب بنك']], 'bank_transactions_url'=>$this->routeContext->bankTransactionsPath()]); }
}
