from datetime import datetime, timezone
from enum import Enum
from uuid import uuid4

from pydantic import BaseModel, Field


class DisciplineOutcome(str, Enum):
    ALLOWED = "allowed"
    BLOCKED = "blocked"


class DisciplineEvent(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid4()))
    at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
    outcome: DisciplineOutcome
    reason: str | None = None
    source: str = "unknown"
    ticker: str | None = None
    action: str | None = None
    pine_strategy: str | None = None


class PineConfigPayload(BaseModel):
    session_start: str | None = None
    session_end: str | None = None
    session_timezone: str | None = None
    max_trades_per_session: int | None = None
    max_daily_loss_pct: float | None = None
    max_open_positions: int | None = None
    max_quantity: float | None = None
    strategy_name: str | None = None


class TradeCheckRequest(BaseModel):
    ticker: str = "UNKNOWN"
    action: str = "buy"
    source: str = "manual"
    strategy: str | None = None
    risk_usd: float | None = Field(default=None, gt=0)
    quantity: float | None = Field(default=None, gt=0)
    price: float | None = Field(default=None, gt=0)
    stop_loss: float | None = None
    point_value: float | None = Field(default=None, gt=0)
