<?php

namespace App\Console\Commands;

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

class ClassifyVisitsCommand extends Command
{
    protected $signature = 'visits:classify {--batch= : Import batch ID to classify only that batch}';

    protected $description = 'Run territory classification (point-in-polygon) for visits';

    public function handle(TerritoryClassificationService $classifier): int
    {
        $batchId = $this->option('batch');
        $visitIds = null;
        if ($batchId !== null) {
            $visitIds = \App\Models\Visit::where('import_batch_id', $batchId)->pluck('id')->toArray();
            if (empty($visitIds)) {
                $this->warn("No visits for batch {$batchId}");
                return 0;
            }
        }

        $count = $classifier->classifyBatch($visitIds);
        $this->info("Classified {$count} visit(s).");
        return 0;
    }
}
