<?php
declare(strict_types=1);

namespace App\Modules\FacilityGrids\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Modules\FacilityGrids\Services\PoolHourPlanService;

final class PoolHourPlanController extends Controller
{
    private PoolHourPlanService $hourPlanService;

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

    public function index(Request $request, string $gridId, string $planId): Response
    {
        $this->authorize('facility_grid.view');

        $service = $this->hourPlanService;
        $plan = \App\Core\App::getInstance()->db()->selectOne(
            "SELECT * FROM facility_monthly_plans WHERE id = ? AND grid_id = ?",
            [(int) $planId, (int) $gridId]
        );

        if (!$plan) {
            return $this->redirect('/facility-grids/' . $gridId . '/plans')->withError('الخطة غير موجودة');
        }

        $workingDays = json_decode($plan['working_days_json'] ?? '[]', true);

        return $this->view('FacilityGrids.Views.pool_hour_plans', [
            'grid_id' => (int) $gridId,
            'plan' => $plan,
            'working_days' => $workingDays,
        ]);
    }

    public function dayView(Request $request, string $gridId, string $planId, string $date): Response
    {
        $this->authorize('facility_grid.view');

        $hours = $this->hourPlanService->getHoursForDate((int) $planId, $date);
        $workingHours = $this->hourPlanService->getWorkingHoursForDate((int) $planId, $date);

        return $this->view('FacilityGrids.Views.pool_hour_plan_edit', [
            'grid_id' => (int) $gridId,
            'plan_id' => (int) $planId,
            'date' => $date,
            'hours' => $hours,
            'working_hours' => $workingHours,
        ]);
    }

    public function showHour(Request $request, string $gridId, string $planId, string $date, string $hour): Response
    {
        $this->authorize('facility_grid.view');
        $hourPlan = $this->hourPlanService->getHourPlan((int) $planId, $date, $hour . ':00');
        return $this->json($hourPlan ?? ['grid_snapshot_json' => '{}']);
    }

    public function storeHour(Request $request, string $gridId, string $planId, string $date, string $hour): Response
    {
        $this->authorize('facility_grid.manage');

        $snapshot = $request->jsonBody()['grid_snapshot'] ?? [];
        $notes = $request->jsonBody()['notes'] ?? null;

        $id = $this->hourPlanService->saveHourPlan((int) $planId, $date, $hour . ':00', $snapshot, $notes);

        return $this->json(['success' => true, 'id' => $id]);
    }

    public function copyHour(Request $request, string $gridId, string $planId, string $date, string $hour): Response
    {
        $this->authorize('facility_grid.manage');

        $body = $request->jsonBody();
        $fromDate = $body['from_date'] ?? $date;
        $fromHour = $body['from_hour'] ?? '';

        $success = $this->hourPlanService->copyHourPlan((int) $planId, $fromDate, $fromHour . ':00', $date, $hour . ':00');

        if (!$success) {
            return $this->json(['success' => false, 'error' => 'لا يوجد خطة لنسخها']);
        }

        return $this->json(['success' => true]);
    }
}
