<?php

namespace App\Http\Controllers\Api\Reports;

use App\Http\Controllers\Api\BaseApiController;
use App\Http\Requests\Files\FileNameRequest;
use App\Http\Resources\Files\FileMetaResource;
use App\Services\Files\ExamDraftDownloadNameService;
use App\Services\Files\FileServeService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;

class FilesController extends BaseApiController
{
    public function __construct(
        private FileServeService $fileService,
        private ExamDraftDownloadNameService $draftNameService
    ) {
        parent::__construct();
    }

    public function receipt(FileNameRequest $request, string $name): Response|JsonResponse
    {
        return $this->serveFile(
            $request,
            storage_path('uploads/receipts'),
            $name,
            ['jpg', 'jpeg', 'png', 'webp', 'gif', 'pdf'],
            null,
            false
        );
    }

    public function reimb(FileNameRequest $request, string $name): Response|JsonResponse
    {
        return $this->serveFile(
            $request,
            storage_path('uploads/reimbursements'),
            $name,
            ['jpg', 'jpeg', 'png', 'webp', 'gif', 'pdf'],
            null,
            true
        );
    }

    public function earlyDismissalSignature(FileNameRequest $request, string $name): Response|JsonResponse
    {
        return $this->serveFile(
            $request,
            storage_path('uploads/early_dismissal_signatures'),
            $name,
            ['jpg', 'jpeg', 'png', 'webp', 'gif', 'pdf'],
            null,
            true
        );
    }

    public function examDraftTeacher(FileNameRequest $request, string $name): Response|JsonResponse
    {
        $downloadName = $this->draftNameService->build($name, 'drafts');

        return $this->serveFile(
            $request,
            storage_path('uploads/exams/drafts'),
            $name,
            ['doc', 'docx', 'pdf'],
            $downloadName,
            true
        );
    }

    public function examDraftFinal(FileNameRequest $request, string $name): Response|JsonResponse
    {
        $downloadName = $this->draftNameService->build($name, 'finals');

        return $this->serveFile(
            $request,
            storage_path('uploads/exams/finals'),
            $name,
            ['doc', 'docx', 'pdf'],
            $downloadName,
            true
        );
    }

    private function serveFile(
        FileNameRequest $request,
        string $baseDir,
        string $name,
        array $allowedExtensions,
        ?string $downloadName,
        bool $nosniff
    ): Response|JsonResponse {
        if ($request->boolean('meta')) {
            $meta = $this->fileService->meta($baseDir, $name, $allowedExtensions, $downloadName);

            return response()->json([
                'ok' => true,
                'file' => new FileMetaResource($meta),
            ]);
        }

        return $this->fileService->serveInline($baseDir, $name, $allowedExtensions, $request, $downloadName, $nosniff);
    }
}
