<?php

namespace Tests\Feature\Api\V1\Scores;

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

class ParticipationControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_index_returns_participation_scores(): void
    {
        $this->seedBase();
        Sanctum::actingAs(User::query()->findOrFail(1));

        DB::table('participation')->insert([
            'id' => 1,
            'student_id' => 100,
            'school_id' => 1,
            'class_section_id' => 1,
            'updated_by' => 1,
            'score' => 93,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $response = $this->getJson('/api/v1/scores/participation?class_section_id=1&semester=Fall&school_year=2025-2026');

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

    private function seedBase(): void
    {
        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('students')->insert([
            'id' => 100,
            'parent_id' => 10,
            'firstname' => 'Kid',
            'lastname' => 'User',
            'school_id' => 1,
            'age' => 10,
            'gender' => 'M',
            'photo_consent' => 1,
            'year_of_registration' => '2025',
            'is_active' => 1,
        ]);

        DB::table('student_class')->insert([
            'id' => 1,
            'student_id' => 100,
            'class_section_id' => 1,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'is_event_only' => 0,
        ]);
    }
}
