<?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 PlaygroundDashboardController extends Controller
{
    private ClubDashboardService $dashboardService;

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

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

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

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

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

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

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