<?php

namespace App\Services\Bot;

use App\Models\User;
use App\Services\FlatTrade\FlatTradeAccountSyncService;

class PositionSyncService
{
    public function __construct(private FlatTradeAccountSyncService $accountSync) {}

    public function syncUser(User $user): void
    {
        $this->accountSync->syncUser($user);
    }

    public function recalculateUnrealized(\App\Models\Position $position, float $ltp): void
    {
        $pnl = $position->net_quantity * ($ltp - (float) $position->net_avg_price) * (float) $position->price_factor;
        $position->update([
            'ltp' => $ltp,
            'unrealized_pnl' => $pnl,
            'peak_ltp' => max((float) $position->peak_ltp, $ltp),
        ]);
    }
}
