<?php

namespace App\Http\Controllers\Web\Payroll;

use App\Http\Controllers\Controller;
use App\Models\PayrollCycle;
use App\Services\AuditService;
use App\Services\Payroll\PayrollBankExportService;

class BankExportController extends Controller
{
    public function __construct(
        protected PayrollBankExportService $exportService,
        protected AuditService $audit
    ) {}

    public function download(PayrollCycle $cycle)
    {
        $companyId = auth()->user()?->company_id;
        if ($companyId && (int) $cycle->company_id !== (int) $companyId) {
            abort(403);
        }

        $this->audit->log('payroll.bank_export', $cycle, [
            'year' => $cycle->year,
            'month' => $cycle->month,
            'rows' => count($this->exportService->rowsForCycle($cycle)),
        ]);

        return $this->exportService->downloadCsv($cycle);
    }

    public function payrollExport(PayrollCycle $cycle)
    {
        $companyId = auth()->user()?->company_id;
        if ($companyId && (int) $cycle->company_id !== (int) $companyId) {
            abort(403);
        }

        $this->audit->log('payroll.summary_export', $cycle, [
            'year' => $cycle->year,
            'month' => $cycle->month,
        ]);

        return $this->exportService->downloadPayrollSummaryCsv($cycle);
    }
}
