<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Jobs\TransformDataPoolToProduction;
use Illuminate\Console\Command;

use function Laravel\Prompts\confirm;
use function Laravel\Prompts\info;
use function Laravel\Prompts\spin;
use function Laravel\Prompts\table;
use function Laravel\Prompts\warning;

class TransformDataPoolCommand extends Command
{
    /**
     * The name and signature of the console command.
     */
    protected $signature = 'backend:transform-data-pool
                            {--batch-size=100 : Number of records to process per batch}
                            {--queue : Dispatch the job to the queue instead of running synchronously}
                            {--stats : Show statistics only, do not perform transformation}';

    /**
     * The console command description.
     */
    protected $description = 'Transform backend_data_pool into companies and transactions tables';

    /**
     * Execute the console command.
     */
    public function handle(): int
    {
        // Show stats only
        if ($this->option('stats')) {
            return $this->showStats();
        }

        info('Preparing for DATA POOL TRANSFORMATION');
        warning('This will create/update companies and transactions from the data pool.');

        if (! $this->option('no-interaction') && ! confirm('Do you want to continue?', true)) {
            info('Transformation cancelled.');

            return self::SUCCESS;
        }

        $batchSize = (int) $this->option('batch-size');

        // Show current stats before transformation
        $this->showStatsBefore();

        // Create the job
        $job = new TransformDataPoolToProduction(batchSize: $batchSize);

        // Execute the job
        if ($this->option('queue')) {
            info('Dispatching job to queue...');
            dispatch($job);
            info('Job dispatched successfully!');
        } else {
            info('Running transformation synchronously...');

            spin(
                fn () => $job->handle(app(\App\Services\KycRiskCalculator::class)),
                'Transforming data...'
            );

            info('Transformation completed successfully!');
        }

        // Show stats after transformation
        $this->showStatsAfter();

        return self::SUCCESS;
    }

    /**
     * Show statistics only
     */
    private function showStats(): int
    {
        $job = new TransformDataPoolToProduction;
        $stats = $job->getStats();

        info('Data Pool Transformation Statistics');

        table(
            ['Metric', 'Value'],
            [
                ['Data Pool Transactions', number_format($stats['data_pool_transactions'])],
                ['Companies in Database', number_format($stats['companies_count'])],
                ['Transactions in Database', number_format($stats['transactions_count'])],
                ['Migrated Transactions', number_format($stats['migrated_transactions'])],
            ]
        );

        return self::SUCCESS;
    }

    /**
     * Show stats before transformation
     */
    private function showStatsBefore(): void
    {
        $job = new TransformDataPoolToProduction;
        $stats = $job->getStats();

        $this->newLine();
        info('Stats BEFORE transformation:');
        table(
            ['Metric', 'Value'],
            [
                ['Data Pool Transactions', number_format($stats['data_pool_transactions'])],
                ['Companies in Database', number_format($stats['companies_count'])],
                ['Migrated Transactions', number_format($stats['migrated_transactions'])],
            ]
        );
        $this->newLine();
    }

    /**
     * Show stats after transformation
     */
    private function showStatsAfter(): void
    {
        $job = new TransformDataPoolToProduction;
        $stats = $job->getStats();

        $this->newLine();
        info('Stats AFTER transformation:');
        table(
            ['Metric', 'Value'],
            [
                ['Data Pool Transactions', number_format($stats['data_pool_transactions'])],
                ['Companies in Database', number_format($stats['companies_count'])],
                ['Migrated Transactions', number_format($stats['migrated_transactions'])],
            ]
        );
        $this->newLine();
    }
}
