<?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 ReimbursementControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_create_batch_creates_open_batch(): void
    {
        $user = $this->createUser();
        Sanctum::actingAs($user);

        DB::table('configuration')->insert([
            ['config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['config_key' => 'semester', 'config_value' => 'Fall'],
        ]);

        $response = $this->postJson('/api/v1/finance/reimbursements/batches', [
            'title' => 'Test Batch',
        ]);

        $response->assertCreated();
        $response->assertJsonPath('ok', true);

        $this->assertDatabaseHas('reimbursement_batches', [
            'title' => 'Test Batch',
            'status' => 'open',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ]);
    }

    public function test_mark_donation_updates_expense(): void
    {
        $user = $this->createUser();
        Sanctum::actingAs($user);

        DB::table('expenses')->insert([
            'id' => 1,
            'category' => 'Expense',
            'amount' => 12.00,
            'date_of_purchase' => '2025-02-01',
            'added_by' => $user->id,
            'purchased_by' => $user->id,
            'status' => 'approved',
        ]);

        $response = $this->postJson('/api/v1/finance/reimbursements/mark-donation', [
            'expense_id' => 1,
        ]);

        $response->assertOk();
        $response->assertJsonPath('ok', true);

        $this->assertDatabaseHas('expenses', [
            'id' => 1,
            'category' => 'Donation',
            'status' => 'approved',
        ]);
    }

    public function test_store_reimbursement_creates_record_and_updates_expense(): void
    {
        $user = $this->createUser();
        Sanctum::actingAs($user);

        DB::table('users')->insert([
            'id' => 2,
            'school_id' => 1,
            'firstname' => 'Recipient',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            'email' => 'recipient@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('expenses')->insert([
            'id' => 2,
            'category' => 'Expense',
            'amount' => 50.00,
            'date_of_purchase' => '2025-02-05',
            'added_by' => $user->id,
            'purchased_by' => $user->id,
            'status' => 'approved',
        ]);

        $response = $this->postJson('/api/v1/finance/reimbursements', [
            'expense_id' => 2,
            'amount' => 50.00,
            'reimbursed_to' => 2,
            'reimbursement_method' => 'Cash',
            'description' => 'Test reimbursement',
        ]);

        $response->assertCreated();
        $response->assertJsonPath('ok', true);

        $this->assertDatabaseHas('reimbursements', [
            'expense_id' => 2,
            'amount' => 50.00,
            'reimbursed_to' => 2,
            'status' => 'Paid',
        ]);

        $this->assertDatabaseMissing('expenses', [
            'id' => 2,
            'reimbursement_id' => null,
        ]);
    }

    public function test_update_reimbursement_updates_fields(): void
    {
        $user = $this->createUser();
        Sanctum::actingAs($user);

        DB::table('reimbursements')->insert([
            'id' => 10,
            'amount' => 25.00,
            'reimbursed_to' => $user->id,
            'added_by' => $user->id,
            'status' => 'Paid',
            'reimbursement_method' => 'Cash',
        ]);

        $response = $this->putJson('/api/v1/finance/reimbursements/10', [
            'amount' => 30.00,
            'reimbursed_to' => $user->id,
            'reimbursement_method' => 'Cash',
            'description' => 'Updated',
        ]);

        $response->assertOk();
        $response->assertJsonPath('ok', true);

        $this->assertDatabaseHas('reimbursements', [
            'id' => 10,
            'amount' => 30.00,
            'description' => 'Updated',
        ]);
    }

    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);
    }
}
