<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        // 课程分类
        Schema::create('course_categories', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100);
            $table->foreignId('parent_id')->nullable()->constrained('course_categories')->nullOnDelete();
            $table->integer('sort')->default(0);
            $table->boolean('status')->default(true);
            $table->timestamps();
        });

        // 课程
        Schema::create('courses', function (Blueprint $table) {
            $table->id();
            $table->string('name', 200);
            $table->string('code', 50)->nullable()->unique();
            $table->foreignId('category_id')->nullable()->constrained('course_categories')->nullOnDelete();
            $table->string('type', 20)->default('offline');
            $table->integer('hours')->default(0);
            $table->integer('duration')->default(45);
            $table->integer('max_students')->default(30);
            $table->decimal('price', 10, 2)->default(0);
            $table->string('age_range', 50)->nullable();
            $table->text('description')->nullable();
            $table->string('cover_image', 500)->nullable();
            $table->string('status', 20)->default('draft');
            $table->foreignId('teacher_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->index('status');
            $table->index('type');
        });

        // 课程章节
        Schema::create('course_chapters', function (Blueprint $table) {
            $table->id();
            $table->foreignId('course_id')->constrained('courses')->cascadeOnDelete();
            $table->foreignId('parent_id')->nullable()->constrained('course_chapters')->cascadeOnDelete();
            $table->string('title', 200);
            $table->integer('sort')->default(0);
            $table->string('type', 20)->default('video');
            $table->text('content')->nullable();
            $table->string('video_url', 500)->nullable();
            $table->integer('duration')->default(0);
            $table->timestamps();
        });

        // 教室
        Schema::create('classrooms', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100);
            $table->integer('capacity')->default(30);
            $table->string('equipment', 500)->nullable();
            $table->json('available_slots')->nullable();
            $table->boolean('status')->default(true);
            $table->timestamps();
        });

        // 学员
        Schema::create('students', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->unique()->constrained('users')->cascadeOnDelete();
            $table->string('student_no', 50)->unique();
            $table->date('birthday');
            $table->string('gender', 10);
            $table->text('id_card_encrypted')->nullable();
            $table->string('guardian_name', 100)->nullable();
            $table->string('guardian_phone', 20)->nullable();
            $table->string('guardian_relation', 20)->nullable();
            $table->boolean('guardian_consent')->default(false);
            $table->string('consent_document', 500)->nullable();
            $table->string('status', 20)->default('active');
            $table->string('source', 20)->nullable();
            $table->json('tags')->nullable();
            $table->timestamps();
            $table->index('status');
        });

        // 班级
        Schema::create('student_classes', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100);
            $table->foreignId('course_id')->nullable()->constrained('courses')->nullOnDelete();
            $table->foreignId('teacher_id')->nullable()->constrained('users')->nullOnDelete();
            $table->integer('capacity')->default(30);
            $table->boolean('status')->default(true);
            $table->timestamps();
        });

        // 学员-班级关联
        Schema::create('student_class_pivot', function (Blueprint $table) {
            $table->id();
            $table->foreignId('student_id')->constrained('students')->cascadeOnDelete();
            $table->foreignId('class_id')->constrained('student_classes')->cascadeOnDelete();
            $table->timestamps();
            $table->unique(['student_id', 'class_id']);
        });

        // 报名
        Schema::create('enrollments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('student_id')->constrained('students')->cascadeOnDelete();
            $table->foreignId('course_id')->constrained('courses')->cascadeOnDelete();
            $table->string('status', 20)->default('active');
            $table->timestamp('enrolled_at')->nullable();
            $table->timestamps();
            $table->index(['student_id', 'course_id']);
        });

        // 排课
        Schema::create('schedules', function (Blueprint $table) {
            $table->id();
            $table->foreignId('course_id')->constrained('courses')->cascadeOnDelete();
            $table->foreignId('teacher_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('classroom_id')->constrained('classrooms')->cascadeOnDelete();
            $table->tinyInteger('day_of_week');
            $table->time('start_time');
            $table->time('end_time');
            $table->date('effective_date');
            $table->date('expiry_date')->nullable();
            $table->string('status', 20)->default('active');
            $table->timestamps();
            $table->index(['teacher_id', 'day_of_week']);
            $table->index(['classroom_id', 'day_of_week']);
        });

        // 题库
        Schema::create('questions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('course_id')->nullable()->constrained('courses')->nullOnDelete();
            $table->string('type', 20);
            $table->text('content');
            $table->json('options')->nullable();
            $table->text('answer');
            $table->text('analysis')->nullable();
            $table->string('difficulty', 20)->default('medium');
            $table->integer('score')->default(1);
            $table->json('knowledge_tags')->nullable();
            $table->timestamps();
            $table->index('type');
            $table->index('difficulty');
        });

        // 考试
        Schema::create('exams', function (Blueprint $table) {
            $table->id();
            $table->string('name', 200);
            $table->foreignId('course_id')->nullable()->constrained('courses')->nullOnDelete();
            $table->string('type', 20)->default('fixed');
            $table->integer('duration');
            $table->integer('pass_score');
            $table->integer('total_score');
            $table->timestamp('start_time');
            $table->timestamp('end_time');
            $table->json('anti_cheat')->nullable();
            $table->string('status', 20)->default('draft');
            $table->timestamps();
            $table->index('status');
        });

        // 考试-题目关联（固定组卷）
        Schema::create('exam_questions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('exam_id')->constrained('exams')->cascadeOnDelete();
            $table->foreignId('question_id')->constrained('questions')->cascadeOnDelete();
            $table->integer('sort')->default(0);
            $table->timestamps();
            $table->unique(['exam_id', 'question_id']);
        });

        // 答题记录
        Schema::create('attempts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('exam_id')->constrained('exams')->cascadeOnDelete();
            $table->foreignId('student_id')->constrained('students')->cascadeOnDelete();
            $table->decimal('score', 5, 1)->nullable();
            $table->string('status', 20)->default('in_progress');
            $table->integer('switch_count')->default(0);
            $table->timestamp('started_at')->nullable();
            $table->timestamp('submitted_at')->nullable();
            $table->timestamps();
            $table->index(['exam_id', 'student_id']);
        });

        // 答题明细
        Schema::create('attempt_answers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('attempt_id')->constrained('attempts')->cascadeOnDelete();
            $table->foreignId('question_id')->constrained('questions')->cascadeOnDelete();
            $table->text('answer')->nullable();
            $table->boolean('is_correct')->nullable();
            $table->decimal('score', 5, 1)->nullable();
            $table->text('teacher_comment')->nullable();
            $table->timestamps();
            $table->unique(['attempt_id', 'question_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('attempt_answers');
        Schema::dropIfExists('attempts');
        Schema::dropIfExists('exam_questions');
        Schema::dropIfExists('exams');
        Schema::dropIfExists('questions');
        Schema::dropIfExists('schedules');
        Schema::dropIfExists('enrollments');
        Schema::dropIfExists('student_class_pivot');
        Schema::dropIfExists('student_classes');
        Schema::dropIfExists('students');
        Schema::dropIfExists('classrooms');
        Schema::dropIfExists('course_chapters');
        Schema::dropIfExists('courses');
        Schema::dropIfExists('course_categories');
    }
};
