<?php

namespace Tests\Feature\Console;

use App\Services\Payments\PaymentMissedCheckService;
use App\Services\Payments\PaymentNotificationService;
use App\Services\Payments\PaymentTestNotificationService;
use App\Services\Payments\PaypalPaymentSyncService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Mockery;
use Tests\TestCase;

class PaymentsCommandsTest extends TestCase
{
    use RefreshDatabase;

    public function test_check_missed_payments_command_runs(): void
    {
        $service = Mockery::mock(PaymentMissedCheckService::class);
        $service->shouldReceive('findUsersWithMissedPayments')->once()->andReturn([]);

        $this->app->instance(PaymentMissedCheckService::class, $service);

        $this->artisan('payments:check-missed')->assertExitCode(0);
    }

    public function test_monthly_reminder_command_sends_to_email(): void
    {
        DB::table('users')->insert([
            'id' => 1,
            'firstname' => 'Parent',
            'lastname' => 'User',
            'email' => 'parent@example.com',
            'status' => 'Active',
        ]);

        $service = Mockery::mock(PaymentNotificationService::class);
        $service->shouldReceive('send')->once()->andReturn([
            'sent' => 1,
            'skipped' => 0,
            'failed' => 0,
            'details' => [],
        ]);

        $this->app->instance(PaymentNotificationService::class, $service);

        $this->artisan('payments:monthly-reminder', ['--email' => 'parent@example.com'])->assertExitCode(0);
    }

    public function test_send_test_payment_notification_command_runs(): void
    {
        $service = Mockery::mock(PaymentTestNotificationService::class);
        $service->shouldReceive('send')->once()->andReturn(['ok' => true]);

        $this->app->instance(PaymentTestNotificationService::class, $service);

        $this->artisan('payments:send-test', ['--email' => 'parent@example.com'])->assertExitCode(0);
    }

    public function test_sync_paypal_payments_command_runs(): void
    {
        $service = Mockery::mock(PaypalPaymentSyncService::class);
        $service->shouldReceive('sync')->once()->andReturn([
            'mode' => 'DRY-RUN',
            'processed' => 0,
            'failed' => [],
        ]);

        $this->app->instance(PaypalPaymentSyncService::class, $service);

        $this->artisan('payments:sync-paypal', ['--dry-run' => true])->assertExitCode(0);
    }
}
