<?php

declare(strict_types=1);

namespace App\Modules\Expenses\Presentation\Controllers;

use App\Core\Auth;
use App\Core\Request;
use App\Core\Response;
use App\Modules\Expenses\Application\Services\ExpenseIndexService;

final class ExpenseIndexController
{
    public function __construct(private ?ExpenseIndexService $service = null)
    {
        $this->service ??= new ExpenseIndexService(db());
    }

    public function categories(): void
    {
        Auth::requireLogin();
        $this->service->ensureSchema();
        require_permission('expenses.categories.view');

        $data = $this->service->categories(
            trim((string) Request::get('q', '')),
            trim((string) Request::get('status', 'active')),
        );

        Response::view('pages/expenses/categories_index', $data + [
            'page_title' => 'مجموعات المصروفات',
            'breadcrumbs' => [
                ['label'=>'لوحة التحكم','url'=>'/admin/dashboard'],
                ['label'=>'المصاريف'],
                ['label'=>'مجموعات المصروفات'],
            ],
        ]);
    }

    public function items(): void
    {
        Auth::requireLogin();
        $this->service->ensureSchema();
        require_permission('expenses.items.view');

        $data = $this->service->items(
            trim((string) Request::get('q', '')),
            (int) Request::get('category_id', 0),
            trim((string) Request::get('status', 'active')),
        );

        Response::view('pages/expenses/items_index', $data + [
            'page_title' => 'بنود المصروفات',
            'breadcrumbs' => [
                ['label'=>'لوحة التحكم','url'=>'/admin/dashboard'],
                ['label'=>'المصاريف'],
                ['label'=>'بنود المصروفات'],
            ],
        ]);
    }
}
