<?php

namespace App\Http\Controllers\Api\Parents;

use App\Http\Controllers\Api\BaseApiController;
use App\Http\Requests\Parents\ParentAttendanceReportSubmitRequest;
use App\Http\Requests\Parents\ParentAttendanceReportListRequest;
use App\Http\Requests\Parents\ParentAttendanceReportCheckRequest;
use App\Http\Requests\Parents\ParentAttendanceReportUpdateRequest;
use App\Http\Resources\Parents\ParentAttendanceReportResource;
use App\Services\Parents\ParentAttendanceReportService;
use Illuminate\Http\JsonResponse;

class ParentAttendanceReportController extends BaseApiController
{
    public function __construct(private ParentAttendanceReportService $reportService)
    {
        parent::__construct();
    }

    public function form(): JsonResponse
    {
        $parentId = (int) (auth()->id() ?? 0);
        if ($parentId <= 0) {
            return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
        }

        $data = $this->reportService->formData($parentId);

        return response()->json([
            'ok' => true,
            'students' => $data['students'],
            'today' => $data['today'],
            'sundays' => $data['sundays'],
            'defaultDate' => $data['defaultDate'],
            'myReports' => ParentAttendanceReportResource::collection($data['myReports']),
            'cutoffThreshold' => $data['cutoffThreshold'],
        ]);
    }

    public function submit(ParentAttendanceReportSubmitRequest $request): JsonResponse
    {
        $parentId = (int) (auth()->id() ?? 0);
        if ($parentId <= 0) {
            return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
        }

        try {
            $result = $this->reportService->submit($parentId, $request->validated());
        } catch (\RuntimeException $e) {
            return response()->json(['ok' => false, 'message' => $e->getMessage()], 422);
        }

        return response()->json([
            'ok' => true,
            'inserted' => $result['inserted'],
            'blocked' => $result['blocked'],
            'students' => $result['students'],
            'dates' => $result['dates'],
        ], 201);
    }

    public function list(ParentAttendanceReportListRequest $request): JsonResponse
    {
        $payload = $request->validated();
        $start = $payload['start'] ?? now()->toDateString();
        $end = $payload['end'] ?? null;
        $schoolYear = $payload['school_year'] ?? null;
        $semester = $payload['semester'] ?? null;

        $rows = $this->reportService->listReports($start, $end, $schoolYear, $semester);

        return response()->json([
            'ok' => true,
            'reports' => ParentAttendanceReportResource::collection($rows),
        ]);
    }

    public function checkExisting(ParentAttendanceReportCheckRequest $request): JsonResponse
    {
        try {
            $rows = $this->reportService->checkExisting($request->validated());
        } catch (\RuntimeException $e) {
            return response()->json(['ok' => false, 'message' => $e->getMessage()], 422);
        }

        return response()->json([
            'ok' => true,
            'students' => $rows,
        ]);
    }

    public function update(ParentAttendanceReportUpdateRequest $request, int $reportId): JsonResponse
    {
        $parentId = (int) (auth()->id() ?? 0);
        if ($parentId <= 0) {
            return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
        }

        try {
            $this->reportService->updateReport($parentId, $reportId, $request->validated());
        } catch (\RuntimeException $e) {
            return response()->json(['ok' => false, 'message' => $e->getMessage()], 422);
        }

        return response()->json(['ok' => true]);
    }
}
