<?php

namespace Tests\Feature\Api\V1\Finance;

use App\Models\EventCharge;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Concerns\CreatesApiTestUsers;
use Tests\TestCase;

class EventChargeControllerTest extends TestCase
{
    use CreatesApiTestUsers;
    use RefreshDatabase;

    private function makeCharge(array $overrides = []): EventCharge
    {
        return EventCharge::query()->create(array_merge([
            'event_name' => 'Field Trip',
            'description' => 'Annual field trip charge',
            'amount' => 25.00,
            'charged' => 0,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ], $overrides));
    }

    public function test_show_requires_authentication(): void
    {
        $charge = $this->makeCharge();

        $this->getJson('/api/v1/finance/event-charges/' . $charge->id)
            ->assertStatus(401);
    }

    public function test_show_returns_event_charge(): void
    {
        $this->actingAsApiAdministrator();
        $charge = $this->makeCharge();

        $this->getJson('/api/v1/finance/event-charges/' . $charge->id)
            ->assertOk()
            ->assertJsonPath('ok', true)
            ->assertJsonPath('event_charge.id', $charge->id)
            ->assertJsonPath('event_charge.event_name', 'Field Trip');
    }

    public function test_show_returns_404_for_unknown_charge(): void
    {
        $this->actingAsApiAdministrator();

        $this->getJson('/api/v1/finance/event-charges/999999')
            ->assertNotFound();
    }

    public function test_update_modifies_event_charge(): void
    {
        $this->actingAsApiAdministrator();
        $charge = $this->makeCharge();

        $this->putJson('/api/v1/finance/event-charges/' . $charge->id, [
            'event_name' => 'Museum Visit',
            'amount' => 40.50,
        ])
            ->assertOk()
            ->assertJsonPath('ok', true)
            ->assertJsonPath('event_charge.event_name', 'Museum Visit');

        $this->assertDatabaseHas('event_charges', [
            'id' => $charge->id,
            'event_name' => 'Museum Visit',
        ]);
    }

    public function test_approve_marks_charge_as_charged(): void
    {
        $this->actingAsApiAdministrator();
        $charge = $this->makeCharge(['charged' => 0]);

        $this->postJson('/api/v1/finance/event-charges/' . $charge->id . '/approve')
            ->assertOk()
            ->assertJsonPath('ok', true);

        $this->assertDatabaseHas('event_charges', [
            'id' => $charge->id,
            'charged' => 1,
        ]);
    }

    public function test_void_clears_charged_flag(): void
    {
        $this->actingAsApiAdministrator();
        $charge = $this->makeCharge(['charged' => 1]);

        $this->postJson('/api/v1/finance/event-charges/' . $charge->id . '/void')
            ->assertOk()
            ->assertJsonPath('ok', true);

        $this->assertDatabaseHas('event_charges', [
            'id' => $charge->id,
            'charged' => 0,
        ]);
    }

    public function test_destroy_voids_the_charge(): void
    {
        $this->actingAsApiAdministrator();
        $charge = $this->makeCharge(['charged' => 1]);

        $this->deleteJson('/api/v1/finance/event-charges/' . $charge->id)
            ->assertOk()
            ->assertJsonPath('ok', true);

        $this->assertDatabaseHas('event_charges', [
            'id' => $charge->id,
            'charged' => 0,
        ]);
    }
}
