<?php

namespace Tests\Unit\Services\Billing;

use App\Services\Billing\BillingTotalsService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class BillingTotalsServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_event_and_additional_subtotals(): void
    {
        DB::table('events')->insert([
            'id' => 1,
            'event_name' => 'Field Trip',
            'amount' => 25.00,
            'expiration_date' => '2025-06-01',
        ]);

        DB::table('event_charges')->insert([
            'id' => 1,
            'event_id' => 1,
            'parent_id' => 10,
            'charged' => 25.00,
            'school_year' => '2025-2026',
        ]);

        DB::table('additional_charges')->insert([
            [
                'id' => 1,
                'invoice_id' => 99,
                'parent_id' => 10,
                'school_year' => '2025-2026',
                'semester' => 'Fall',
                'charge_type' => 'add',
                'amount' => 15.00,
                'status' => 'applied',
            ],
            [
                'id' => 2,
                'invoice_id' => 99,
                'parent_id' => 10,
                'school_year' => '2025-2026',
                'semester' => 'Fall',
                'charge_type' => 'deduct',
                'amount' => 5.00,
                'status' => 'applied',
            ],
        ]);

        $service = new BillingTotalsService();

        $eventSubtotal = $service->eventSubtotal(10, '2025-2026');
        $additionalSubtotal = $service->additionalSubtotal(99, '2025-2026');

        $this->assertSame(25.0, $eventSubtotal);
        $this->assertSame(10.0, $additionalSubtotal);
    }

    public function test_additional_subtotal_by_parent_sums_applied_charges(): void
    {
        DB::table('additional_charges')->insert([
            [
                'parent_id' => 10,
                'invoice_id' => 99,
                'school_year' => '2025-2026',
                'semester' => 'Fall',
                'charge_type' => 'add',
                'title' => 'Uniform',
                'amount' => 40.00,
                'status' => 'applied',
            ],
            [
                'parent_id' => 10,
                'invoice_id' => 99,
                'school_year' => '2025-2026',
                'semester' => 'Fall',
                'charge_type' => 'deduct',
                'title' => 'Credit',
                'amount' => 10.00,
                'status' => 'applied',
            ],
            [
                'parent_id' => 10,
                'invoice_id' => 99,
                'school_year' => '2025-2026',
                'semester' => 'Fall',
                'charge_type' => 'add',
                'title' => 'Pending',
                'amount' => 99.00,
                'status' => 'pending',
            ],
        ]);

        $service = new BillingTotalsService();

        $this->assertSame(30.0, $service->additionalSubtotalByParent(10, '2025-2026'));
    }

    public function test_event_subtotals_by_student_groups_amounts(): void
    {
        DB::table('events')->insert([
            'id' => 1,
            'event_name' => 'Trip',
            'amount' => 20.00,
            'expiration_date' => '2025-06-01',
        ]);

        DB::table('event_charges')->insert([
            [
                'event_id' => 1,
                'parent_id' => 5,
                'student_id' => 1,
                'charged' => 20.00,
                'school_year' => '2025-2026',
            ],
            [
                'event_id' => 1,
                'parent_id' => 5,
                'student_id' => 2,
                'charged' => 15.00,
                'school_year' => '2025-2026',
            ],
        ]);

        $service = new BillingTotalsService();
        $byStudent = $service->eventSubtotalsByStudent(5, '2025-2026');

        $this->assertSame(20.0, $byStudent[1]);
        $this->assertSame(15.0, $byStudent[2]);
        $this->assertSame(35.0, $service->eventSubtotal(5, '2025-2026'));
    }
}
