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

    public function test_report_returns_financial_report(): void
    {
        $this->seedFinancialData();

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

        $response = $this->getJson('/api/v1/finance/financial-report?school_year=2025-2026');

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

    public function test_summary_returns_totals(): void
    {
        $this->seedFinancialData();

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

        $response = $this->getJson('/api/v1/finance/financial-summary?school_year=2025-2026&date_from=2025-01-01&date_to=2025-12-31');

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $this->assertGreaterThan(0, (float) $response->json('summary.totalCharges'));
    }

    public function test_unpaid_parents_lists_balances(): void
    {
        $this->seedFinancialData();

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

        $response = $this->getJson('/api/v1/finance/unpaid-parents?school_year=2025-2026');

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

    private function seedFinancialData(): void
    {
        DB::table('configuration')->insert([
            'id' => 1,
            'config_key' => 'school_year',
            'config_value' => '2025-2026',
        ]);
        DB::table('users')->insert([
            [
                'id' => 2,
                '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('invoices')->insert([
            [
                'id' => 1,
                'parent_id' => 2,
                'invoice_number' => 'INV-001',
                'total_amount' => 100,
                'balance' => 40,
                'paid_amount' => 60,
                'has_discount' => 1,
                'issue_date' => '2025-01-10',
                'due_date' => '2025-02-10',
                'status' => 'unpaid',
                'school_year' => '2025-2026',
            ],
            [
                'id' => 2,
                'parent_id' => 2,
                'invoice_number' => 'INV-002',
                'total_amount' => 50,
                'balance' => 50,
                'paid_amount' => 0,
                'has_discount' => 0,
                'issue_date' => '2025-02-01',
                'due_date' => '2025-03-01',
                'status' => 'unpaid',
                'school_year' => '2025-2026',
            ],
        ]);

        DB::table('payments')->insert([
            'id' => 1,
            'parent_id' => 2,
            'invoice_id' => 1,
            'total_amount' => 100,
            'paid_amount' => 60,
            'balance' => 40,
            'number_of_installments' => 1,
            'payment_method' => 'Cash',
            'payment_date' => '2025-01-15',
            'school_year' => '2025-2026',
            'status' => 'Paid',
        ]);

        DB::table('discount_usages')->insert([
            'id' => 1,
            'voucher_id' => 1,
            'invoice_id' => 1,
            'parent_id' => 2,
            'discount_amount' => 10,
            'school_year' => '2025-2026',
        ]);

        DB::table('refunds')->insert([
            'id' => 1,
            'parent_id' => 2,
            'school_year' => '2025-2026',
            'invoice_id' => 1,
            'refund_amount' => 5,
            'status' => 'Paid',
            'refund_paid_amount' => 5,
        ]);

        DB::table('expenses')->insert([
            'id' => 1,
            'category' => 'Expense',
            'amount' => 20,
            'date_of_purchase' => '2025-01-05',
            'purchased_by' => 2,
            'added_by' => 1,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'status' => 'approved',
        ]);

        DB::table('reimbursements')->insert([
            'id' => 1,
            'expense_id' => 1,
            'amount' => 15,
            'reimbursed_to' => 2,
            'added_by' => 1,
            'status' => 'Paid',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ]);
    }

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