<?php

namespace Tests\Feature\Api\V1\Finance;

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

class PaymentEventChargesControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_store_creates_event_charges(): void
    {
        $this->seedUsers();
        $this->seedStudents();
        Sanctum::actingAs(User::query()->findOrFail(1));

        $response = $this->postJson('/api/v1/finance/event-charges', [
            'parent_id' => 10,
            'event_name' => 'Field Trip',
            'description' => 'Bus fee',
            'amount' => 25,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'student_ids' => [100],
        ]);

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $this->assertDatabaseHas('event_charges', [
            'parent_id' => 10,
            'student_id' => 100,
            'event_name' => 'Field Trip',
            'amount' => 25,
            'school_year' => '2025-2026',
        ]);
    }

    private function seedUsers(): void
    {
        DB::table('roles')->insert([
            ['id' => 1, 'name' => 'admin', 'slug' => 'admin', 'is_active' => 1],
            ['id' => 2, 'name' => 'parent', 'slug' => 'parent', 'is_active' => 1],
        ]);

        DB::table('users')->insert([
            'id' => 1,
            'school_id' => 1,
            'firstname' => 'Admin',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            'email' => 'admin@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('users')->insert([
            'id' => 10,
            'school_id' => 1,
            'firstname' => 'Parent',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            'email' => 'parent@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([
            ['user_id' => 1, 'role_id' => 1],
            ['user_id' => 10, 'role_id' => 2],
        ]);
    }

    private function seedStudents(): void
    {
        DB::table('students')->insert([
            'id' => 100,
            'parent_id' => 10,
            'firstname' => 'Kid',
            'lastname' => 'User',
            'school_id' => 1,
            'age' => 10,
            'gender' => 'Male',
            'is_active' => 1,
            'photo_consent' => 1,
            'year_of_registration' => '2025',
            'school_year' => '2025-2026',
        ]);
    }
}
