<?php

namespace Modules\Lpj\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Modules\Lpj\Jobs\CleanupInspeksiDataJob;
use Modules\Lpj\Services\InspeksiCleanupService;

class CleanupSingleInspeksiCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'lpj:cleanup-single-inspeksi 
                            {permohonan-id : ID permohonan yang akan di-cleanup}
                            {created-by : ID user yang membuat data}
                            {--sync : Jalankan secara synchronous}
                            {--force : Jalankan tanpa konfirmasi}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Cleanup data inspeksi untuk 1 permohonan dan user tertentu';

    /**
     * Execute the console command.
     */
    public function handle(): int
    {
        Log::info('CleanupSingleInspeksiCommand: Memulai proses cleanup', [
            'permohonan_id' => $this->argument('permohonan-id'),
            'created_by' => $this->argument('created-by'),
            'sync' => $this->option('sync')
        ]);

        try {
            $permohonanId = (int) $this->argument('permohonan-id');
            $createdBy = (int) $this->argument('created-by');
            $sync = $this->option('sync');
            $force = $this->option('force');

            // Validasi input
            if ($permohonanId <= 0) {
                $this->error('Permohonan ID harus angka positif.');
                return Command::FAILURE;
            }

            if ($createdBy <= 0) {
                $this->error('Created By harus angka positif.');
                return Command::FAILURE;
            }

            // Tampilkan info
            $this->info('=== Cleanup Single Inspeksi ===');
            $this->info("Permohonan ID: {$permohonanId}");
            $this->info("Created By: {$createdBy}");
            $this->info("Mode: " . ($sync ? 'Synchronous' : 'Queue'));
            $this->newLine();

            // Konfirmasi jika tidak force
            if (!$force && !$this->confirm('Lanjutkan dengan cleanup?')) {
                $this->info('Cleanup dibatalkan.');
                return Command::SUCCESS;
            }

            // Jalankan cleanup
            $cleanupService = new InspeksiCleanupService();
            
            if ($sync) {
                $this->info('Menjalankan cleanup secara synchronous...');
                $cleanupService->cleanupSync($permohonanId, $createdBy);
                $this->info('Cleanup selesai.');
            } else {
                $this->info('Mengirim job ke queue...');
                $cleanupService->cleanupAsync($permohonanId, $createdBy);
                $this->info('Job telah di-dispatch ke queue.');
                $this->info('Monitor progress di log.');
            }

            return Command::SUCCESS;

        } catch (\Exception $e) {
            Log::error('CleanupSingleInspeksiCommand: Terjadi error saat proses cleanup', [
                'permohonan_id' => $this->argument('permohonan-id'),
                'created_by' => $this->argument('created-by'),
                'error' => $e->getMessage(),
                'trace' => $e->getTraceAsString()
            ]);

            $this->error('Terjadi kesalahan: ' . $e->getMessage());
            return Command::FAILURE;
        }
    }
}