<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\DashboardService;
use Carbon\Carbon;

class DashboardController extends Controller
{
    protected $service;

    public function __construct(DashboardService $service)
    {
        $this->service = $service;
    }

    /*
    |--------------------------------------------------------------------------
    | Helper: Resolve Epiweek Range
    |--------------------------------------------------------------------------
    */

    private function getEpiRange(Request $request)
    {
        $startYear = (int) $request->query('start_year');
        $startWeek = (int) $request->query('start_week');
        $endYear = (int) $request->query('end_year');
        $endWeek = (int) $request->query('end_week');

        if (!$startYear || !$startWeek || !$endYear || !$endWeek) {
            return null;
        }

        return [
            'startYear' => $startYear,
            'startWeek' => $startWeek,
            'endYear' => $endYear,
            'endWeek' => $endWeek
        ];
    }


    /*
    |--------------------------------------------------------------------------
    | Overview Summary Cards
    |--------------------------------------------------------------------------
    */

    public function summary()
    {
        $dateFrom = Carbon::now()->subDays(7)->toDateString();
        $dateTo = Carbon::now()->toDateString();

        $data = $this->service->summaryCards($dateFrom, $dateTo);

        return response()->json($data);
    }


    /*
    |--------------------------------------------------------------------------
    | Overview Trend Chart
    |--------------------------------------------------------------------------
    */

    public function trend(Request $request)
    {
        $range = $this->getEpiRange($request);

        if (!$range) {
            return response()->json(['error' => 'Missing epiweek range'], 400);
        }

        $data = $this->service->aggregateAllPrograms(

            $range['startYear'],
            $range['startWeek'],
            $range['endYear'],
            $range['endWeek']
        );

        return response()->json($data);
    }


    /*
    |--------------------------------------------------------------------------
    | Program Dashboard
    |--------------------------------------------------------------------------
    */

    public function program(Request $request)
    {
        $surveillanceId = (int) $request->query('surveillance_id');

        if (!$surveillanceId) {
            return response()->json(['error' => 'Missing surveillance_id'], 400);
        }

        $range = $this->getEpiRange($request);

        if (!$range) {
            return response()->json(['error' => 'Missing epiweek range'], 400);
        }

        $data = $this->service->programDashboardData(
            $surveillanceId,
            $range['startYear'],
            $range['startWeek'],
            $range['endYear'],
            $range['endWeek']
        );

        return response()->json($data);
    }


    /*
    |--------------------------------------------------------------------------
    | Province Map (Overview)
    |--------------------------------------------------------------------------
    */

    public function provinceCircles(Request $request)
    {
        $range = $this->getEpiRange($request);

        if (!$range) {
            return response()->json(['error' => 'Missing epiweek range'], 400);
        }

        $data = $this->service->provinceCircles(
            $range['startYear'],
            $range['startWeek'],
            $range['endYear'],
            $range['endWeek']
        );

        return response()->json($data);
    }


    /*
    |--------------------------------------------------------------------------
    | Sentinel Map
    |--------------------------------------------------------------------------
    */

    public function sentinelMap(Request $request)
    {
        $range = $this->getEpiRange($request);

        if (!$range) {
            return response()->json(['error' => 'Missing epiweek range'], 400);
        }

        $data = $this->service->sentinelMap(
            $range['startYear'],
            $range['startWeek'],
            $range['endYear'],
            $range['endWeek']
        );

        return response()->json($data);
    }
}