<?php

namespace Tests\Feature\Api\V1\Grading;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class HomeworkTrackingControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_index_returns_tracking_payload(): void
    {
        DB::table('configuration')->insert([
            ['id' => 1, 'config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['id' => 2, 'config_key' => 'semester', 'config_value' => 'Fall'],
        ]);

        DB::table('roles')->insert([
            ['id' => 1, 'name' => 'teacher', 'slug' => 'teacher', 'is_active' => 1],
        ]);

        DB::table('users')->insert([
            'id' => 1,
            'school_id' => 1,
            'firstname' => 'Teacher',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            'email' => 'teacher@example.com',
            'address_street' => '123 Main',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'is_verified' => 1,
            'status' => 'Active',
            'is_suspended' => 0,
            'failed_attempts' => 0,
            'password' => bcrypt('secret'),
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        DB::table('user_roles')->insert([
            'id' => 1,
            'user_id' => 1,
            'role_id' => 1,
        ]);

        DB::table('classSection')->insert([
            'id' => 1,
            'class_section_id' => 1,
            'class_section_name' => '1A',
            'class_id' => 1,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        DB::table('teacher_class')->insert([
            'id' => 1,
            'class_section_id' => 1,
            'teacher_id' => 1,
            'position' => 'main',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        Sanctum::actingAs(User::query()->findOrFail(1));

        $response = $this->getJson('/api/v1/grading/homework-tracking?school_year=2025-2026&semester=Fall');

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $this->assertNotEmpty($response->json('teachers'));
    }
}
