<?php

namespace App\Services\Exams;

use App\Models\ExamDraft;
use App\Models\TeacherClass;
use Illuminate\Support\Facades\DB;

class ExamDraftTeacherService
{
    public function __construct(
        private ExamDraftConfigService $configService,
        private ExamDraftFileService $fileService
    ) {
    }

    public function listForTeacher(int $teacherId): array
    {
        $context = $this->configService->context();
        $schoolYear = (string) ($context['school_year'] ?? '');

        $assignments = TeacherClass::getClassAssignmentsByUserId($teacherId, $schoolYear);
        $classSectionIds = array_map(static fn ($a) => (int) $a['class_section_id'], $assignments);

        $drafts = ExamDraft::query()
            ->where('teacher_id', $teacherId)
            ->orderByDesc('created_at')
            ->get()
            ->toArray();

        $legacyExams = [];
        if (($context['has_legacy'] ?? false) && !empty($classSectionIds)) {
            $legacyExams = DB::table('exam_drafts as ed')
                ->select('ed.*', 'cs.class_section_name')
                ->leftJoin('classSection as cs', 'cs.class_section_id', '=', 'ed.class_section_id')
                ->whereIn('ed.class_section_id', $classSectionIds)
                ->where('ed.is_legacy', 1)
                ->whereNotNull('ed.final_file')
                ->orderBy('cs.class_section_name')
                ->orderByDesc('ed.created_at')
                ->get()
                ->map(fn ($r) => (array) $r)
                ->all();
        }

        return [
            'assignments' => $assignments,
            'drafts' => $drafts,
            'legacyExams' => $legacyExams,
            'examTypes' => ExamDraftConfigService::EXAM_TYPES,
            'statusOptions' => ExamDraftConfigService::STATUS_OPTIONS,
            'schoolYear' => $schoolYear,
            'semester' => (string) ($context['semester'] ?? ''),
        ];
    }

    public function store(int $teacherId, array $payload, ?\Illuminate\Http\UploadedFile $file): array
    {
        $context = $this->configService->context();
        $schoolYear = (string) ($context['school_year'] ?? '');
        $semester = (string) ($context['semester'] ?? '');

        $classSectionId = (int) ($payload['class_section_id'] ?? 0);
        $examType = trim((string) ($payload['exam_type'] ?? ''));
        $description = trim((string) ($payload['description'] ?? ''));

        if ($classSectionId <= 0) {
            return ['ok' => false, 'message' => 'Select a class section before submitting.'];
        }

        $assignment = TeacherClass::query()
            ->where('teacher_id', $teacherId)
            ->where('class_section_id', $classSectionId)
            ->first();

        if (!$assignment) {
            return ['ok' => false, 'message' => 'You are not assigned to the selected class section.'];
        }

        $teacherFile = null;
        $teacherFilename = null;
        if ($file) {
            $stored = $this->fileService->storeUploadedFile(
                $file,
                ExamDraftConfigService::TEACHER_UPLOAD_DIR,
                ExamDraftConfigService::ALLOWED_EXTENSIONS,
                ExamDraftConfigService::MAX_UPLOAD_BYTES
            );
            if ($stored === null) {
                return ['ok' => false, 'message' => 'Failed to store the uploaded file.'];
            }
            $teacherFile = $stored['stored'];
            $teacherFilename = $stored['original'];
        }

        $existingDraft = ExamDraft::query()
            ->where('teacher_id', $teacherId)
            ->where('class_section_id', $classSectionId)
            ->where('semester', $semester)
            ->where('school_year', $schoolYear)
            ->orderByDesc('version')
            ->first();

        $nextVersion = 1;
        $previousId = null;
        if ($existingDraft) {
            $nextVersion = ((int) ($existingDraft->version ?? 1)) + 1;
            $previousId = (int) ($existingDraft->id ?? 0) ?: null;
        }

        $title = $examType !== '' ? $examType : 'Exam Draft';

        $draft = ExamDraft::query()->create([
            'teacher_id' => $teacherId,
            'class_section_id' => $classSectionId,
            'semester' => $semester,
            'school_year' => $schoolYear,
            'exam_type' => $examType !== '' ? $examType : null,
            'draft_title' => $title,
            'description' => $description !== '' ? $description : null,
            'teacher_file' => $teacherFile,
            'teacher_filename' => $teacherFilename,
            'status' => 'submitted',
            'version' => $nextVersion,
            'previous_draft_id' => $previousId,
        ]);

        return [
            'ok' => true,
            'draft' => $draft->toArray(),
        ];
    }
}
