<?php

namespace App\Console\Commands;

use App\Models\BotRuntimeState;
use App\Services\MarketData\MarketDataSyncService;
use Illuminate\Console\Command;

class MarketSyncCommand extends Command
{
    protected $signature = 'market:sync';

    protected $description = 'Sync live NIFTY spot, indices, option chain, ticks and greeks from FlatTrade';

    public function handle(MarketDataSyncService $sync): int
    {
        try {
            $count = $sync->syncScanUniverse()->count();
            BotRuntimeState::heartbeat('market_sync', 'completed', ['instruments' => $count]);
            $this->info("Synced {$count} live option instruments from FlatTrade.");

            return self::SUCCESS;
        } catch (\Throwable $e) {
            $this->error($e->getMessage());
            BotRuntimeState::heartbeat('market_sync', 'failed', ['error' => $e->getMessage()]);

            return self::FAILURE;
        }
    }
}
