"""Broker contract — the boundary to order execution. The DTOs (Order, AccountState) are part of this
port's contract and live with it; the application/domain depend only on these shapes, never on ib_async."""
from __future__ import annotations

from typing import Protocol

from pydantic import BaseModel, Field


class Order(BaseModel):
    symbol: str
    side: str  # "BUY" | "SELL"
    quantity: float = Field(gt=0.0)


class AccountState(BaseModel):
    nlv: float                                  # net liquidation value
    cash: float
    positions: dict[str, float] = Field(default_factory=dict)   # symbol -> signed shares
    is_paper: bool = True
    gross_position_value: float | None = None   # if provided, used for a data-consistency sanity check


class Broker(Protocol):
    name: str

    def account_state(self) -> AccountState: ...

    def place_order(self, order: Order) -> str:
        """Submit an order; return its status string (e.g. 'Filled', 'PreSubmitted', 'Cancelled')."""
        ...
