<?php

namespace App\Console\Commands;

use App\Services\AiEmbeddingService;
use Illuminate\Console\Command;

class SyncAiEmbeddings extends Command
{
    protected $signature = 'ai:sync-embeddings
                            {--source= : Sync specific source (kb, predefined, replies)}
                            {--force : Force re-sync even if content unchanged}';

    protected $description = 'Sync AI embeddings from knowledgebase, predefined replies, and ticket replies';

    public function handle(AiEmbeddingService $service): int
    {
        $source = $this->option('source');
        $total = 0;

        if (! $source || $source === 'kb') {
            $this->info('Syncing knowledgebase articles...');
            $count = $service->syncKnowledgebase();
            $this->info("  Synced {$count} knowledgebase embeddings.");
            $total += $count;
        }

        if (! $source || $source === 'predefined') {
            $this->info('Syncing predefined replies...');
            $count = $service->syncPredefinedReplies();
            $this->info("  Synced {$count} predefined reply embeddings.");
            $total += $count;
        }

        if (! $source || $source === 'replies') {
            $this->info('Syncing ticket replies...');
            $count = $service->syncTicketReplies();
            $this->info("  Synced {$count} ticket reply embeddings.");
            $total += $count;
        }

        $this->info("Total: {$total} embeddings synced.");

        return 0;
    }
}
