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

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

        $response = $this->postJson('/api/v1/finance/payment-transactions', [
            'transaction_id' => 'TXN-1001',
            'payment_id' => 1,
            'transaction_date' => '2025-01-10',
            'amount' => 25,
            'payment_method' => 'cash',
        ]);

        $response->assertStatus(201);
        $response->assertJson(['ok' => true]);
        $this->assertDatabaseHas('payment_transactions', [
            'transaction_id' => 'TXN-1001',
            'payment_id' => 1,
            'amount' => 25,
        ]);
    }

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

        DB::table('payment_transactions')->insert([
            'transaction_id' => 'TXN-1002',
            'payment_id' => 1,
            'transaction_date' => '2025-01-11',
            'amount' => 40,
            'payment_method' => 'cash',
            'payment_status' => 'Pending',
        ]);

        $response = $this->getJson('/api/v1/finance/payment-transactions/payment/1');

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

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

        DB::table('payment_transactions')->insert([
            'transaction_id' => 'TXN-1003',
            'payment_id' => 1,
            'transaction_date' => '2025-01-11',
            'amount' => 40,
            'payment_method' => 'cash',
            'payment_status' => 'Pending',
        ]);

        $response = $this->postJson('/api/v1/finance/payment-transactions/TXN-1003/status', [
            'status' => 'Completed',
        ]);

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $this->assertDatabaseHas('payment_transactions', [
            'transaction_id' => 'TXN-1003',
            'payment_status' => 'Completed',
        ]);
    }

    private function seedUsers(): void
    {
        DB::table('roles')->insert([
            ['id' => 1, 'name' => 'admin', 'slug' => 'admin', '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('user_roles')->insert([
            'user_id' => 1,
            'role_id' => 1,
        ]);
    }

    private function seedPayment(): void
    {
        DB::table('payments')->insert([
            'id' => 1,
            'parent_id' => 10,
            'invoice_id' => 1,
            'total_amount' => 100,
            'paid_amount' => 0,
            'balance' => 100,
            'number_of_installments' => 1,
            'payment_method' => 'cash',
            'payment_date' => '2025-01-01',
            'school_year' => '2025-2026',
            'status' => 'Pending',
        ]);
    }
}
