<?php

namespace Tests\Feature\Api\V1\ExtraCharges;

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

class ExtraChargesControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_list_returns_rows(): void
    {
        $this->seedBaseData();
        $this->seedCharge();

        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->getJson('/api/v1/extra-charges');

        $response->assertOk();
        $this->assertNotEmpty($response->json('rows'));
    }

    public function test_options_returns_parent_options(): void
    {
        $this->seedBaseData();

        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->getJson('/api/v1/extra-charges/options?q=Parent');

        $response->assertOk();
        $this->assertNotEmpty($response->json('parents'));
    }

    public function test_parent_options_returns_results(): void
    {
        $this->seedBaseData();

        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->getJson('/api/v1/extra-charges/parents?q=Parent');

        $response->assertOk();
        $this->assertNotEmpty($response->json('results'));
    }

    public function test_invoices_for_parent_returns_results(): void
    {
        $this->seedBaseData();

        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->getJson('/api/v1/extra-charges/invoices?parent_id=10&school_year=2025-2026');

        $response->assertOk();
        $this->assertNotEmpty($response->json('results'));
    }

    public function test_store_creates_charge_and_updates_invoice(): void
    {
        $this->seedBaseData();

        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->postJson('/api/v1/extra-charges', [
            'parent_id' => 10,
            'title' => 'Books',
            'amount' => 20,
            'charge_type' => 'add',
            'due_date' => '2025-09-01',
            'invoice_id' => 1,
            'description' => 'Extra books',
        ]);

        $response->assertCreated();
        $response->assertJson(['ok' => true]);

        $this->assertDatabaseHas('additional_charges', [
            'parent_id' => 10,
            'title' => 'Books',
            'status' => 'applied',
        ]);

        $this->assertDatabaseHas('invoices', [
            'id' => 1,
            'total_amount' => 120,
            'balance' => 120,
        ]);
    }

    public function test_update_adjusts_charge(): void
    {
        $this->seedBaseData();
        $this->seedCharge();

        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->putJson('/api/v1/extra-charges/1', [
            'title' => 'Books Updated',
            'amount' => 15,
            'charge_type' => 'add',
            'due_date' => '2025-09-01',
            'description' => 'Updated',
        ]);

        $response->assertOk();
        $response->assertJson(['ok' => true]);

        $this->assertDatabaseHas('additional_charges', [
            'id' => 1,
            'title' => 'Books Updated',
        ]);
    }

    public function test_void_changes_status(): void
    {
        $this->seedBaseData();
        $this->seedCharge();

        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->postJson('/api/v1/extra-charges/1/void');

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $this->assertDatabaseHas('additional_charges', [
            'id' => 1,
            'status' => 'void',
        ]);
    }

    public function test_reverse_changes_status(): void
    {
        $this->seedBaseData();
        $this->seedCharge();

        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->postJson('/api/v1/extra-charges/1/reverse');

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $this->assertDatabaseHas('additional_charges', [
            'id' => 1,
            'status' => 'pending',
        ]);
    }

    private function seedBaseData(): 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' => 'parent', 'slug' => 'parent', 'is_active' => 1],
        ]);

        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' => 10,
            'role_id' => 1,
        ]);

        DB::table('invoices')->insert([
            'id' => 1,
            'parent_id' => 10,
            'invoice_number' => 'INV-1',
            'total_amount' => 100,
            'balance' => 100,
            'paid_amount' => 0,
            'issue_date' => '2025-09-01',
            'school_year' => '2025-2026',
            'status' => 'Unpaid',
        ]);
    }

    private function seedCharge(): void
    {
        DB::table('additional_charges')->insert([
            'id' => 1,
            'parent_id' => 10,
            'invoice_id' => 1,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'charge_type' => 'add',
            'title' => 'Books',
            'description' => 'Extra books',
            'amount' => 10,
            'due_date' => '2025-09-01',
            'status' => 'applied',
            'created_by' => 1,
            'created_at' => '2025-09-01 00:00:00',
        ]);

        DB::table('invoices')->where('id', 1)->update([
            'total_amount' => 110,
            'balance' => 110,
        ]);
    }

    private function createUser(): User
    {
        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',
        ]);

        return User::query()->findOrFail(1);
    }
}
