<?php

namespace App\Console\Commands;

use App\Models\BotRuntimeState;
use App\Services\FlatTrade\FlatTradeAccountSyncService;
use Illuminate\Console\Command;

class SyncFlatTradeAccountsCommand extends Command
{
    protected $signature = 'flattrade:sync-accounts';

    protected $description = 'Sync balance, positions, orders and P&L for all connected users';

    public function handle(FlatTradeAccountSyncService $sync): int
    {
        $count = $sync->syncAllConnectedUsers();
        BotRuntimeState::heartbeat('account_sync', 'completed', ['users' => $count]);
        $this->info("Synced {$count} user account(s).");

        return self::SUCCESS;
    }
}
