<?php

namespace Modules\Finance\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Modules\Finance\Clients\FinanceModuleClient;

class ReportController extends Controller
{
    public function __construct(
        private readonly FinanceModuleClient $finance,
    ) {}

    public function index(Request $request): View
    {
        $this->authorize('finance.reports.view');

        $filters = $request->only([
            'date_preset',
            'date_from',
            'date_to',
            'type',
            'search',
        ]);

        $reports = $this->finance->reports($filters);

        return view('finance::reports.index', [
            'filters' => $filters,
            'incomeReport' => $reports['income'] ?? ['by_category' => []],
            'expenseReport' => $reports['expense'] ?? ['by_category' => []],
            'profitLossReport' => $reports['profit_loss'] ?? [
                'total_income' => 0,
                'total_expense' => 0,
                'net_profit' => 0,
                'profit_margin' => 0,
            ],
        ]);
    }
}
