<?php

namespace App\Console\Commands;

use App\Models\AiResponseLog;
use Illuminate\Console\Command;

class ProcessPendingAiResponses extends Command
{
    protected $signature = 'ai:process-pending-responses';

    protected $description = 'Display pending AI draft responses awaiting admin review';

    public function handle(): int
    {
        $pending = AiResponseLog::pendingDrafts()
            ->with(['ticket', 'profile'])
            ->orderBy('created_at', 'desc')
            ->get();

        if ($pending->isEmpty()) {
            $this->info('No pending AI drafts.');

            return 0;
        }

        $this->info("Found {$pending->count()} pending draft(s):");

        $rows = $pending->map(fn ($log) => [
            $log->id,
            $log->ticket?->title ?? '-',
            $log->profile?->name ?? '-',
            number_format($log->confidence_score, 3),
            $log->created_at->format('d.m.Y H:i'),
        ]);

        $this->table(['ID', 'Ticket', 'AI Staff', 'Confidence', 'Created'], $rows->toArray());

        return 0;
    }
}
