<?php

namespace Tests\Unit\Services\Finance;

use App\Services\Finance\FinancialDonationRecipientService;
use App\Services\Finance\FinancialSummaryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class FinancialSummaryServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_summary_calculates_totals(): void
    {
        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',
        ]);

        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',
            'used_at' => '2025-01-11 10:00:00',
            'created_at' => '2025-01-11 10:00:00',
        ]);

        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,
            'refunded_at' => '2025-01-12 10:00:00',
            'created_at' => '2025-01-12 10:00:00',
        ]);

        DB::table('additional_charges')->insert([
            [
                'id' => 1,
                'parent_id' => 2,
                'invoice_id' => null,
                'semester' => 'Fall',
                'title' => 'Extra Charge',
                'amount' => 20,
                'due_date' => '2025-01-12',
                'school_year' => '2025-2026',
                'status' => 'pending',
            ],
            [
                'id' => 2,
                'parent_id' => 2,
                'amount' => 30,
                'invoice_id' => 1,
                'semester' => 'Fall',
                'title' => 'Applied Charge',
                'due_date' => '2025-01-12',
                'school_year' => '2025-2026',
                'status' => 'applied',
            ],
        ]);

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

        DB::table('reimbursements')->insert([
            [
                'id' => 1,
                'expense_id' => 2,
                'amount' => 15,
                'reimbursed_to' => 2,
                'added_by' => 1,
                'status' => 'Paid',
                'school_year' => '2025-2026',
                'semester' => 'Fall',
                'created_at' => '2025-01-07 12:00:00',
            ],
            [
                'id' => 2,
                'expense_id' => 1,
                'amount' => 7,
                'reimbursed_to' => 990002,
                'added_by' => 1,
                'status' => 'Paid',
                'school_year' => '2025-2026',
                'semester' => 'Fall',
                'created_at' => '2025-01-08 12:00:00',
            ],
        ]);

        $service = new FinancialSummaryService(new FinancialDonationRecipientService());
        $summary = $service->getSummary('2025-01-01', '2025-12-31', '2025-2026');

        $this->assertSame(120.0, $summary['totalCharges']);
        $this->assertSame(50.0, $summary['totalExtraCharges']);
        $this->assertSame(10.0, $summary['totalDiscounts']);
        $this->assertSame(5.0, $summary['totalRefunds']);
        $this->assertSame(20.0, $summary['totalExpenses']);
        $this->assertSame(15.0, $summary['totalReimbursements']);
        $this->assertSame(19.0, $summary['donationToSchool']);
        $this->assertSame(60.0, $summary['totalPaid']);
        $this->assertSame(45.0, $summary['totalUnpaid']);
        $this->assertSame(105.0, $summary['netAmount']);
    }
}
