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

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

        DB::table('invoices')->insert([
            'id' => 1,
            'parent_id' => 10,
            'invoice_number' => 'INV-001',
            'total_amount' => 250,
            'balance' => 250,
            'paid_amount' => 0,
            'has_discount' => 0,
            'issue_date' => '2025-01-05',
            'status' => 'unpaid',
            'school_year' => '2025-2026',
        ]);

        $response = $this->postJson('/api/v1/finance/payments', [
            'parent_id' => 10,
            'invoice_id' => 1,
            'total_amount' => 250,
            'number_of_installments' => 2,
            'payment_date' => '2025-01-05',
            'payment_method' => 'cash',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        $response->assertStatus(201);
        $response->assertJson(['ok' => true]);
        $this->assertDatabaseHas('payments', [
            'parent_id' => 10,
            'total_amount' => 250,
            'number_of_installments' => 2,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ]);
    }

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

        DB::table('payments')->insert([
            'id' => 1,
            'parent_id' => 10,
            'invoice_id' => 1,
            'total_amount' => 100,
            'paid_amount' => 25,
            'balance' => 75,
            'number_of_installments' => 1,
            'payment_method' => 'cash',
            'payment_date' => '2025-01-01',
            'school_year' => '2025-2026',
            'status' => 'Partially Paid',
        ]);

        $response = $this->getJson('/api/v1/finance/payments/parent/10?school_year=2025-2026');

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

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

        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',
        ]);

        $response = $this->postJson('/api/v1/finance/payments/1/balance', [
            'paid_amount' => 25,
        ]);

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $this->assertDatabaseHas('payments', [
            'id' => 1,
            'paid_amount' => 25,
            'balance' => 75,
        ]);
    }

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