<?php

namespace Tests\Unit\Services\Payments;

use App\Services\EmailService;
use App\Services\Payments\PaymentNotificationDispatchService;
use App\Services\Payments\PaymentNotificationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Mockery;
use Tests\TestCase;

class PaymentNotificationServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_send_records_log_and_returns_sent_count(): void
    {
        DB::table('users')->insert([
            'id' => 10,
            'school_id' => 1,
            'firstname' => 'Parent',
            'lastname' => 'User',
            'cellphone' => '555-555-5555',
            '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-1',
            'total_amount' => 100,
            'balance' => 100,
            'paid_amount' => 0,
            'has_discount' => 0,
            'issue_date' => '2025-01-01',
            'status' => 'unpaid',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ]);

        $emailService = Mockery::mock(EmailService::class);
        $emailService->shouldReceive('send')
            ->once()
            ->with(
                'parent@example.com',
                Mockery::type('string'),
                Mockery::type('string'),
                'finance'
            )
            ->andReturn(true);

        $dispatchService = Mockery::mock(PaymentNotificationDispatchService::class);
        $dispatchService->shouldNotReceive('notifyUser');

        $service = new PaymentNotificationService($emailService, $dispatchService);
        $result = $service->send([
            'parent_id' => 10,
            'type' => 'installment',
            'school_year' => '2025-2026',
        ]);

        $this->assertSame(1, $result['sent']);
        $this->assertDatabaseHas('payment_notification_logs', [
            'parent_id' => 10,
            'status' => 'sent',
            'type' => 'installment',
        ]);
    }

    public function test_list_logs_filters_by_type(): void
    {
        DB::table('users')->insert([
            'id' => 11,
            'school_id' => 1,
            'firstname' => 'Parent',
            'lastname' => 'Two',
            'cellphone' => '555-555-5556',
            'email' => 'parent2@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('payment_notification_logs')->insert([
            'id' => 1,
            'parent_id' => 11,
            'invoice_id' => null,
            'school_year' => '2025-2026',
            'period_year' => 2025,
            'period_month' => 9,
            'type' => 'installment',
            'to_email' => 'parent2@example.com',
            'cc_email' => null,
            'head_fa_notified' => 0,
            'subject' => 'Reminder',
            'body' => 'Body',
            'status' => 'sent',
            'error_message' => null,
            'balance_snapshot' => 50,
            'sent_at' => '2025-09-01 00:00:00',
        ]);

        $emailService = Mockery::mock(EmailService::class);
        $dispatchService = Mockery::mock(PaymentNotificationDispatchService::class);
        $service = new PaymentNotificationService($emailService, $dispatchService);

        $logs = $service->listLogs(null, null, 'installment');

        $this->assertCount(1, $logs);
        $this->assertSame(11, $logs[0]->parent_id);
    }
}
