<?php

namespace Tests\Feature;

use App\Models\AcademicYear;
use App\Models\Assignment;
use App\Models\SchoolClass;
use App\Models\Staff;
use App\Models\Student;
use App\Models\Subject;
use App\Models\User;
use Database\Seeders\RolesAndPermissionsSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AcademicEngineTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed(RolesAndPermissionsSeeder::class);
    }

    public function test_teacher_dashboard_loads_with_stats(): void
    {
        $staff = Staff::query()->create([
            'staff_number' => 'T-001',
            'first_name' => 'Jane',
            'last_name' => 'Teacher',
            'status' => 'active',
        ]);

        $user = User::factory()->create(['staff_id' => $staff->id]);
        $user->assignRole('Teacher');

        $year = AcademicYear::query()->create([
            'name' => '2026',
            'code' => '2026',
            'starts_on' => '2026-01-01',
            'ends_on' => '2026-12-31',
            'is_current' => true,
        ]);

        SchoolClass::query()->create([
            'name' => 'Form 1A',
            'code' => 'F1A',
            'class_teacher_id' => $staff->id,
            'academic_year_id' => $year->id,
            'status' => 'active',
        ]);

        $this->actingAs($user)
            ->get(route('teacher.home'))
            ->assertOk()
            ->assertSee('Teacher Dashboard');
    }

    public function test_teacher_can_create_assignment_for_assigned_class(): void
    {
        $staff = Staff::query()->create([
            'staff_number' => 'T-002',
            'first_name' => 'John',
            'last_name' => 'Teacher',
            'status' => 'active',
        ]);

        $user = User::factory()->create(['staff_id' => $staff->id]);
        $user->assignRole('Teacher');

        $year = AcademicYear::query()->create([
            'name' => '2026',
            'code' => '2026',
            'starts_on' => '2026-01-01',
            'ends_on' => '2026-12-31',
            'is_current' => true,
        ]);

        $class = SchoolClass::query()->create([
            'name' => 'Form 2B',
            'code' => 'F2B',
            'class_teacher_id' => $staff->id,
            'academic_year_id' => $year->id,
            'status' => 'active',
        ]);

        $subject = Subject::query()->create(['code' => 'MATH', 'name' => 'Mathematics', 'is_active' => true]);

        $this->actingAs($user)->post(route('teacher.assignments.store'), [
            'title' => 'Algebra Homework',
            'class_id' => $class->id,
            'subject_id' => $subject->id,
            'total_marks' => 20,
            'submission_type' => 'file',
            'status' => 'published',
            'description' => 'Complete exercises 1-10',
        ])->assertRedirect(route('teacher.assignments.index'));

        $this->assertDatabaseHas('assignments', ['title' => 'Algebra Homework', 'class_id' => $class->id]);
    }

    public function test_headmaster_academic_oversight_page_loads(): void
    {
        $user = User::factory()->create();
        $user->assignRole('Headmaster');

        $this->actingAs($user)
            ->get(route('school.academic-oversight.index'))
            ->assertOk()
            ->assertSee('Academic Oversight');
    }
}
