<?php
declare(strict_types=1);

namespace App\Modules\PlaygroundAdmin\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Modules\PlaygroundAdmin\Services\ClubDashboardService;
use App\Modules\PlaygroundAdmin\Services\DashboardExportService;

final class ClubSportsDashboardController extends Controller
{
    private ClubDashboardService $dashboardService;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->dashboardService = new ClubDashboardService();
    }

    public function index(Request $request): Response
    {
        $this->authorize('playground.dashboard');

        $filter = $request->get('filter', 'monthly');
        $range = $this->dashboardService->getDateRange($filter, $request->get('from'), $request->get('to'));
        $data = $this->dashboardService->getClubWideData($range['from'], $range['to']);

        return $this->view('PlaygroundAdmin.Views.club_dashboard', [
            'filter' => $filter,
            'data' => $data,
        ]);
    }

    public function export(Request $request): Response
    {
        $this->authorize('playground.dashboard');

        $filter = $request->get('filter', 'monthly');
        $range = $this->dashboardService->getDateRange($filter, $request->get('from'), $request->get('to'));
        $data = $this->dashboardService->getClubWideData($range['from'], $range['to']);

        $exportService = new DashboardExportService();
        return $exportService->exportClubWide($data, $range);
    }
}
