<?php

namespace Tests\Feature\Api\V1\Students;

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

class StudentControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_assign_and_remove_class(): void
    {
        $this->seedConfig();
        $user = $this->seedUser();
        $studentId = $this->seedStudent();
        $classSectionId = $this->seedClassSection();

        Sanctum::actingAs($user);
        $assignResponse = $this->postJson('/api/v1/students/assign-class', [
            'student_id' => $studentId,
            'class_section_id' => [$classSectionId],
        ]);

        $assignResponse->assertOk();
        $this->assertDatabaseHas('student_class', [
            'student_id' => $studentId,
            'class_section_id' => $classSectionId,
        ]);

        $removeResponse = $this->postJson('/api/v1/students/remove-class', [
            'student_id' => $studentId,
            'class_section_id' => $classSectionId,
        ]);

        $removeResponse->assertOk();
        $this->assertDatabaseMissing('student_class', [
            'student_id' => $studentId,
            'class_section_id' => $classSectionId,
        ]);
    }

    public function test_score_card_returns_rows(): void
    {
        $this->seedConfig();
        $user = $this->seedUser();
        $studentId = $this->seedStudent();
        $this->seedClassSection();

        DB::table('semester_scores')->insert([
            'student_id' => $studentId,
            'class_section_id' => 701,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'semester_score' => 90,
        ]);

        Sanctum::actingAs($user);
        $response = $this->getJson('/api/v1/students/' . $studentId . '/score-card');

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

    public function test_store_creates_emergency_contact_for_first_parent(): void
    {
        $user = $this->seedUser();
        $parentId = 1234;

        Sanctum::actingAs($user, [], 'api');
        $response = $this->postJson('/api/v1/students', [
            'school_year' => '2025-2026',
            'firstname' => 'Sara',
            'lastname' => 'Ahmed',
            'dob' => '2016-03-25',
            'age' => 8,
            'gender' => 'Female',
            'is_active' => true,
            'registration_grade' => '3',
            'is_new' => true,
            'photo_consent' => true,
            'parent_id' => $parentId,
            'registration_date' => '2026-03-25',
            'tuition_paid' => true,
            'semester' => 'Fall',
            'year_of_registration' => 2026,
            'name' => 'Amina Ahmed',
            'cellphone' => '5555555555',
            'email' => 'amina.ahmed@example.com',
            'relation' => 'Mother',
        ]);

        $response->assertStatus(201);
        $response->assertJson(['ok' => true]);
        $this->assertDatabaseHas('students', [
            'parent_id' => $parentId,
            'firstname' => 'Sara',
            'lastname' => 'Ahmed',
        ]);
        $this->assertDatabaseHas('emergency_contacts', [
            'parent_id' => $parentId,
            'emergency_contact_name' => 'Amina Ahmed',
            'cellphone' => '5555555555',
            'email' => 'amina.ahmed@example.com',
            'relation' => 'Mother',
        ]);
    }

    private function seedConfig(): void
    {
        DB::table('configuration')->insert([
            ['config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['config_key' => 'semester', 'config_value' => 'Fall'],
        ]);
    }

    private function seedUser(): User
    {
        $userId = DB::table('users')->insertGetId([
            'firstname' => 'Admin',
            'lastname' => 'User',
            'cellphone' => '7777777777',
            'email' => 'student.controller@example.com',
            'address_street' => '123 Street',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'password' => bcrypt('password'),
            'user_type' => 'primary',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'status' => 'Active',
        ]);

        return User::query()->findOrFail($userId);
    }

    private function seedStudent(): int
    {
        return DB::table('students')->insertGetId([
            'school_id' => 'S-900',
            'firstname' => 'Omar',
            'lastname' => 'Student',
            'dob' => '2014-09-01',
            'age' => 11,
            'gender' => 'Male',
            'photo_consent' => 1,
            'parent_id' => 1,
            'year_of_registration' => '2025',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ]);
    }

    private function seedClassSection(): int
    {
        DB::table('classSection')->insert([
            'class_id' => 1,
            'class_section_id' => 701,
            'class_section_name' => '7A',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        return 701;
    }
}
