<?php

namespace Tests\Unit\Services\Reimbursements;

use App\Services\Reimbursements\ReimbursementDonationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class ReimbursementDonationServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_mark_donation_updates_expense_and_unassigns(): void
    {
        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('expenses')->insert([
            'id' => 200,
            'category' => 'Expense',
            'amount' => 40.00,
            'date_of_purchase' => '2025-02-01',
            'added_by' => 1,
            'purchased_by' => 1,
            'status' => 'approved',
        ]);

        DB::table('reimbursement_batches')->insert([
            'id' => 5,
            'title' => 'Batch 5',
            'status' => 'open',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'opened_at' => now(),
        ]);

        DB::table('reimbursement_batch_items')->insert([
            'batch_id' => 5,
            'expense_id' => 200,
            'assigned_at' => now(),
        ]);

        $service = new ReimbursementDonationService();
        $service->markDonation(200, 1);

        $this->assertDatabaseHas('expenses', [
            'id' => 200,
            'category' => 'Donation',
            'status' => 'approved',
        ]);

        $this->assertDatabaseMissing('reimbursement_batch_items', [
            'batch_id' => 5,
            'expense_id' => 200,
            'unassigned_at' => null,
        ]);
    }
}
