<?php

namespace Tests\Unit\Services\Finance;

use App\Services\Finance\FinancialPaymentService;
use App\Services\Finance\FinancialReportService;
use App\Services\Finance\FinancialSchoolYearService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class FinancialReportServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_report_returns_grouped_data(): void
    {
        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('invoices')->insert([
            'id' => 1,
            'parent_id' => 10,
            'invoice_number' => 'INV-001',
            'total_amount' => 100,
            'balance' => 40,
            'paid_amount' => 60,
            'has_discount' => 1,
            'issue_date' => '2025-01-01',
            'due_date' => '2025-02-01',
            'status' => 'unpaid',
            'school_year' => '2025-2026',
        ]);

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

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

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

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

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

        $service = new FinancialReportService(
            new FinancialPaymentService(),
            new FinancialSchoolYearService()
        );

        $report = $service->getReport('2025-01-01', '2025-12-31', '2025-2026');

        $this->assertSame('2025-2026', $report['selectedYear']);
        $this->assertSame(['2025-2026'], $report['schoolYears']);
        $this->assertCount(1, $report['invoices']);
        $this->assertCount(1, $report['payments']);
        $this->assertCount(1, $report['refunds']);
        $this->assertCount(1, $report['discounts']);
        $this->assertCount(1, $report['expenses']);
        $this->assertCount(1, $report['reimbursements']);
        $this->assertSame(60.0, $report['paymentTotals']['total_all']);
    }
}
