<?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
    {
        $teacherUserColumn = $this->hasColumn('exam_drafts', 'teacher_id')
            ? 'teacher_id'
            : ($this->hasColumn('exam_drafts', 'author_id') ? 'author_id' : null);

        $teacherFileColumn = $this->hasColumn('exam_drafts', 'teacher_file')
            ? 'teacher_file'
            : ($this->hasColumn('exam_drafts', 'author_file') ? 'author_file' : null);

        $teacherFilenameColumn = $this->hasColumn('exam_drafts', 'teacher_filename')
            ? 'teacher_filename'
            : ($this->hasColumn('exam_drafts', 'author_filename') ? 'author_filename' : null);

        $teacherCommentColumn = $this->hasColumn('exam_drafts', 'description')
            ? 'description'
            : ($this->hasColumn('exam_drafts', 'author_comment') ? 'author_comment' : null);

        $adminUserColumn = $this->hasColumn('exam_drafts', 'admin_id')
            ? 'admin_id'
            : ($this->hasColumn('exam_drafts', 'reviewer_id') ? 'reviewer_id' : null);

        $adminCommentsColumn = $this->hasColumn('exam_drafts', 'admin_comments')
            ? 'admin_comments'
            : ($this->hasColumn('exam_drafts', 'reviewer_comments') ? 'reviewer_comments' : null);

        return [
            'school_year' => (string) (Configuration::getConfig('school_year') ?? ''),
            'semester' => (string) (Configuration::getConfig('semester') ?? ''),
            'teacher_user_column' => $teacherUserColumn,
            'teacher_file_column' => $teacherFileColumn,
            'teacher_filename_column' => $teacherFilenameColumn,
            'teacher_comment_column' => $teacherCommentColumn,
            'admin_user_column' => $adminUserColumn,
            'admin_comments_column' => $adminCommentsColumn,
            'has_teacher_id' => $this->hasColumn('exam_drafts', 'teacher_id'),
            'has_admin_id' => $this->hasColumn('exam_drafts', 'admin_id'),
            'has_author_id' => $this->hasColumn('exam_drafts', 'author_id'),
            'has_reviewer_id' => $this->hasColumn('exam_drafts', 'reviewer_id'),
            'has_admin_comments' => $this->hasColumn('exam_drafts', 'admin_comments'),
            'has_reviewer_comments' => $this->hasColumn('exam_drafts', 'reviewer_comments'),
            'has_reviewed_at' => $this->hasColumn('exam_drafts', 'reviewed_at'),
            'has_final_pdf' => $this->hasColumn('exam_drafts', 'final_pdf_file'),
            'has_final_filename' => $this->hasColumn('exam_drafts', 'final_filename'),
            'has_previous_draft_id' => $this->hasColumn('exam_drafts', 'previous_draft_id'),
            '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;
        }
    }
}
