<?php

namespace App\Http\Controllers\Api\Reports;

use App\Http\Controllers\Api\Core\BaseApiController;
use App\Http\Requests\Reports\Stickers\StickerFormDataRequest;
use App\Http\Requests\Reports\Stickers\StickerPrintRequest;
use App\Http\Requests\Reports\Stickers\StickerStudentsByClassRequest;
use App\Http\Resources\Reports\Stickers\StickerClassSectionResource;
use App\Http\Resources\Reports\Stickers\StickerPresetResource;
use App\Http\Resources\Reports\Stickers\StickerStudentResource;
use App\Services\Reports\Stickers\StickerPresetService;
use App\Services\Reports\Stickers\StickerPrintService;
use App\Services\Reports\Stickers\StickerQueryService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Response;

class StickersController extends BaseApiController
{
    public function __construct(
        private StickerQueryService $queryService,
        private StickerPresetService $presetService,
        private StickerPrintService $printService
    ) {
        parent::__construct();
    }

    public function formData(StickerFormDataRequest $request): JsonResponse
    {
        $payload = $request->validated();
        $schoolYear = $this->queryService->resolveSchoolYear($payload['school_year'] ?? null);
        $classSectionId = (int) ($payload['class_section_id'] ?? 0);

        $classes = $this->queryService->listClassesWithStudents($schoolYear);
        $students = $classSectionId > 0
            ? $this->queryService->listStudentsByClass($classSectionId, $schoolYear)
            : $this->queryService->listStudentsForYear($schoolYear);

        $presets = $this->presetService->presets();

        return $this->success([
            'classes' => StickerClassSectionResource::collection($classes),
            'students' => StickerStudentResource::collection($students),
            'presets' => StickerPresetResource::collection($presets),
        ]);
    }

    public function studentsByClass(StickerStudentsByClassRequest $request): JsonResponse
    {
        $payload = $request->validated();
        $schoolYear = $this->queryService->resolveSchoolYear($payload['school_year'] ?? null);
        $classSectionId = (int) ($payload['class_section_id'] ?? 0);

        $students = $this->queryService->listStudentsByClass($classSectionId, $schoolYear);

        return $this->success([
            'students' => StickerStudentResource::collection($students),
        ]);
    }

    public function print(StickerPrintRequest $request)
    {
        try {
            $result = $this->printService->generate($request->validated());
        } catch (\Throwable $e) {
            Log::error('Sticker print failed: ' . $e->getMessage());
            return $this->error('Failed to generate stickers.', Response::HTTP_INTERNAL_SERVER_ERROR);
        }

        if (!($result['ok'] ?? false)) {
            return $this->error($result['message'] ?? 'Unable to generate stickers.', Response::HTTP_UNPROCESSABLE_ENTITY);
        }

        return response($result['pdf'], 200, [
            'Content-Type' => 'application/pdf',
            'Content-Disposition' => 'inline; filename="' . ($result['filename'] ?? 'Student_Stickers.pdf') . '"',
        ]);
    }
}
