<?php

namespace App\Console\Commands;

use App\Enums\IsActive;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class MigrateThemeCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migrate:theme';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Migrate theme data from legacy proof_airing_themes table';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $this->info('Starting theme migration...');

        $legacyConn = DB::connection('mysql_second');

        // Get legacy themes
        $legacyThemes = $legacyConn->table('proof_airing_themes')->get();

        if ($legacyThemes->isEmpty()) {
            $this->warn('No themes found in legacy database.');

            return;
        }

        $this->withProgressBar($legacyThemes, function ($legacy) {
            DB::table('themes')->updateOrInsert(
                ['id' => $legacy->id],
                [
                    'name' => $legacy->name,
                    'is_active' => IsActive::ACTIVE->value,
                    'created_at' => $legacy->created_at ?? now(),
                    'updated_at' => $legacy->updated_at ?? now(),
                    'deleted_at' => $legacy->deleted_at,
                ]
            );
        });
        $this->newLine();

        $this->info('Theme migration completed successfully!');
    }
}
