<?php

namespace App\Services\Exams;

use App\Models\Configuration;
use Illuminate\Support\Facades\Schema;

class ExamDraftConfigService
{
    public const TEACHER_UPLOAD_DIR = 'exams/drafts';
    public const FINAL_UPLOAD_DIR = 'exams/finals';
    public const MAX_UPLOAD_BYTES = 12 * 1024 * 1024;
    public const ALLOWED_EXTENSIONS = ['doc', 'docx'];
    public const ADMIN_ALLOWED_EXTENSIONS = ['doc', 'docx', 'pdf'];

    public const EXAM_TYPES = [
        'Final Exam',
        'Midterm Exam',
        'Quiz',
        'Study Guide',
        'Practice Exam',
        'Other',
    ];

    public const STATUS_OPTIONS = ['draft', 'submitted', 'reviewed', 'finalized', 'rejected'];

    public function context(): array
    {
        return [
            'school_year' => (string) (Configuration::getConfig('school_year') ?? ''),
            'semester' => (string) (Configuration::getConfig('semester') ?? ''),
            'has_final_pdf' => $this->hasColumn('exam_drafts', 'final_pdf_file'),
            'has_legacy' => $this->hasColumn('exam_drafts', 'is_legacy'),
        ];
    }

    private function hasColumn(string $table, string $column): bool
    {
        try {
            return Schema::hasColumn($table, $column);
        } catch (\Throwable) {
            return false;
        }
    }
}
