<?php
declare(strict_types=1);

namespace App\Modules\Reports\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Core\App;
use App\Modules\Reports\Models\ReportDefinition;
use App\Modules\Reports\Services\ReportEngine;
use App\Modules\Reports\Services\ReportExporter;

class ReportController extends Controller
{
    public function index(Request $request): Response
    {
        $reports = ReportDefinition::getAllActive();
        $grouped = [];
        foreach ($reports as $r) {
            $grouped[$r['category']][] = $r;
        }
        return $this->view('Reports.Views.index', ['grouped' => $grouped]);
    }

    public function viewReport(Request $request, string $code): Response
    {
        $definition = ReportDefinition::findByCode($code);
        if (!$definition) return $this->redirect('/reports')->withError('التقرير غير موجود');

        $filters = [
            'date_from' => $request->get('date_from', ''),
            'date_to'   => $request->get('date_to', ''),
            'branch_id' => $request->get('branch_id', ''),
            'status'    => $request->get('status', ''),
        ];

        $data = ReportEngine::generate($code, $filters);
        $db = App::getInstance()->db();
        $branches = $db->select("SELECT id, name_ar FROM branches WHERE is_active = 1 ORDER BY name_ar");

        return $this->view('Reports.Views.view', [
            'definition' => $definition,
            'data'       => $data,
            'filters'    => $filters,
            'branches'   => $branches,
        ]);
    }

    public function export(Request $request, string $code): Response
    {
        $definition = ReportDefinition::findByCode($code);
        if (!$definition) return $this->redirect('/reports')->withError('التقرير غير موجود');

        $filters = [
            'date_from' => $request->get('date_from', ''),
            'date_to'   => $request->get('date_to', ''),
            'branch_id' => $request->get('branch_id', ''),
            'status'    => $request->get('status', ''),
        ];

        $data = ReportEngine::generate($code, $filters);
        $csv = ReportExporter::toCsv($data, $definition);

        $response = new Response();
        header('Content-Type: text/csv; charset=UTF-8');
        header('Content-Disposition: attachment; filename="' . $code . '_' . date('Y-m-d') . '.csv"');
        echo "\xEF\xBB\xBF";
        echo $csv;
        exit;
    }

    public function printReport(Request $request, string $code): Response
    {
        $definition = ReportDefinition::findByCode($code);
        if (!$definition) return $this->redirect('/reports')->withError('التقرير غير موجود');

        $filters = [
            'date_from' => $request->get('date_from', ''),
            'date_to'   => $request->get('date_to', ''),
            'branch_id' => $request->get('branch_id', ''),
            'status'    => $request->get('status', ''),
        ];

        $data = ReportEngine::generate($code, $filters);

        return $this->view('Reports.Views.print', [
            'definition' => $definition,
            'data'       => $data,
            'filters'    => $filters,
        ]);
    }
}