<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class ImportMecaprogramsHome extends Command
{
    protected $signature = 'mecaprograms:import-home
        {--delay=500 : Milliseconds between requests}
        {--dry-run : Print what would be imported without saving}';

    protected $description = 'Import all homepage data from mecaprograms.com';

    protected array $commands = [
        'mecaprograms:import-sliders',
        'mecaprograms:import-features',
        'mecaprograms:import-brands',
        'mecaprograms:import-payment-methods',
        'mecaprograms:import-faqs',
        'mecaprograms:import-updates',
        'mecaprograms:import-interfaces',
        'mecaprograms:import-flash-sales',
        'mecaprograms:import-categories',
        'mecaprograms:import-articles',
        'mecaprograms:import-testimonials',
        'mecaprograms:import-seo',
        'mecaprograms:import-page-home',
        'mecaprograms:import-payment-plugins',
        'mecaprograms:install-modules',
        'mecaprograms:import-menus',
        'mecaprograms:import-whatsapp',
        'mecaprograms:import-whatsapp-plugins',
        'mecaprograms:import-theme',
        'mecaprograms:import-colors',
        'mecaprograms:import-top-sellers',
        'mecaprograms:import-payment-process',
        'mecaprograms:import-dynamic-products',
        'mecaprograms:import-how-it-works',
        'mecaprograms:import-premium-features',
        'mecaprograms:import-google-plugins',
        'mecaprograms:import-video-plugins',
        'mecaprograms:import-subscription',
        'mecaprograms:import-logo',
        'mecaprograms:import-themes',
        'mecaprograms:import-cms-pages',
        'mecaprograms:import-settings',
        'mecaprograms:import-media',
    ];

    public function handle(): int
    {
        $this->info('========================================');
        $this->info('  Importing Homepage from Mecaprograms');
        $this->info('========================================');
        $this->newLine();

        $results = [];
        $exitCode = Command::SUCCESS;

        $options = [];
        if ($this->option('dry-run')) {
            $options['--dry-run'] = true;
        }

        foreach ($this->commands as $cmd) {
            $this->line("Running: <fg=yellow>$cmd</>");
            $this->newLine();

            $cmdOptions = $options;
            if ($cmd === 'mecaprograms:import-brands') {
                $cmdOptions['--with-logos'] = true;
            }

            $code = $this->call($cmd, $cmdOptions);

            $results[] = [
                'command' => $cmd,
                'status' => $code === Command::SUCCESS ? '<fg=green>OK</>' : '<fg=red>FAIL</>',
            ];

            if ($code !== Command::SUCCESS) {
                $exitCode = Command::FAILURE;
            }

            usleep((int) $this->option('delay') * 1000);
        }

        $this->newLine();
        $this->table(['Command', 'Status'], $results);

        $this->newLine();
        if ($exitCode === Command::SUCCESS) {
            $this->info('All homepage data imported successfully.');
        } else {
            $this->warn('Some imports failed. Check the output above.');
        }

        return $exitCode;
    }
}
