<?php

use App\Commands\CreateCrud;
use App\Commands\RefreshPagarmeEntities;
use App\Commands\RefreshPermissions;
use App\Commands\TestWebsocketEvent;
use App\Models\Provider;
use App\Services\Pagarme\PagarmeTransferService;
use App\Services\ProviderWithdrawalService;
use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
use App\Jobs\InvalidateExpiredSchedules;
use Illuminate\Support\Facades\Schedule;

Artisan::command('inspire', function () {
    $this->comment(Inspiring::quote());
})->purpose('Display an inspiring quote');

Artisan::command('create:crud {name} {--m|model} {--s|service} {--c|controller} {--r|request} {--e|resource} {--t|route} {--g|migration} {--all}', function () {
    $this->call(CreateCrud::class, [
        'name' => $this->argument('name'),
        '--model' => $this->option('model'),
        '--service' => $this->option('service'),
        '--controller' => $this->option('controller'),
        '--request' => $this->option('request'),
        '--resource' => $this->option('resource'),
        '--route' => $this->option('route'),
        '--migration' => $this->option('migration'),
        '--all' => $this->option('all'),
    ]);
})->purpose('Create CRUD operations with typed Models');

Artisan::command('permissions:refresh', function () {
    $this->call(RefreshPermissions::class);
})->purpose('Refresh all permissions and user type permissions');

Artisan::command('pagarme:refresh-entities', function () {
    $this->call(RefreshPagarmeEntities::class);
})->purpose('Limpa os recipients e clients locais e os recria no Pagar.me');

Artisan::command('websocket:test {room} {--event=test-event} {--data=}', function () {
    $this->call(TestWebsocketEvent::class, [
        'room' => $this->argument('room'),
        '--event' => $this->option('event'),
        '--data' => $this->option('data'),
    ]);
})->purpose('Test websocket broadcasting by emitting an event');

//

Artisan::command('pagarme:recipient-balance {recipient_id} {--skip-local}', function (
    PagarmeTransferService $pagarmeTransfer,
    ProviderWithdrawalService $withdrawals,
) {
    $recipientId = $this->argument('recipient_id');

    if (! $this->option('skip-local')) {
        $provider = Provider::query()
            ->where('recipient_id', $recipientId)
            ->first();

        if ($provider) {
            $this->info('Saldo calculado localmente:');
            $this->line(sprintf('  available: R$ %s', number_format($withdrawals->getAvailableBalance($provider), 2, ',', '.')));
            $this->line(sprintf('  pending:   R$ %s', number_format($withdrawals->getPendingBalance($provider), 2, ',', '.')));
            $this->newLine();
        } else {
            $this->warn('Nenhum provider local encontrado para esse recipient_id.');
            $this->newLine();
        }
    }

    $this->info('Saldo retornado pelo Pagar.me:');

    $this->line(json_encode($pagarmeTransfer->getRecipientBalance($recipientId), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
})->purpose('Consult Pagar.me recipient balance and compare it with local withdrawal balance');

Schedule::job(new InvalidateExpiredSchedules)->dailyAt('05:00');
