<?php

namespace App\Http\Controllers\Api\Grading;

use App\Http\Controllers\Api\BaseApiController;
use App\Http\Requests\Grading\HomeworkTrackingRequest;
use App\Http\Resources\Grading\HomeworkTrackingTeacherResource;
use App\Services\Grading\HomeworkTrackingService;
use Illuminate\Http\JsonResponse;

class HomeworkTrackingController extends BaseApiController
{
    public function __construct(private HomeworkTrackingService $service)
    {
        parent::__construct();
    }

    public function index(HomeworkTrackingRequest $request): JsonResponse
    {
        $payload = $request->validated();
        $data = $this->service->report(
            $payload['semester'] ?? null,
            $payload['school_year'] ?? null,
            isset($payload['page']) ? (int) $payload['page'] : 1
        );

        return response()->json([
            'ok' => true,
            'semester' => $data['semester'],
            'school_year' => $data['school_year'],
            'sundays' => $data['sundays'],
            'event_days' => $data['event_days'],
            'date_to_index' => $data['date_to_index'],
            'teachers' => HomeworkTrackingTeacherResource::collection($data['teachers']),
            'has_homework' => $data['has_homework'],
            'hw_entered_at' => $data['hw_entered_at'],
            'has_homework_by_date' => $data['has_homework_by_date'],
            'hw_entered_at_by_date' => $data['hw_entered_at_by_date'],
            'page' => $data['page'],
            'total_pages' => $data['total_pages'],
            'per_page' => $data['per_page'],
            'total_rows' => $data['total_rows'],
        ]);
    }
}
