from pydantic import BaseModel, Field, ConfigDict
from decimal import Decimal
from datetime import datetime
from app.models.bet import BetCategory, BetStatus, BetVisibility, ProposalStatus
from app.schemas.user import UserSummary


class BetCreate(BaseModel):
    title: str = Field(..., min_length=5, max_length=200)
    description: str = Field(..., max_length=2000)
    category: BetCategory
    event_name: str = Field(..., max_length=200)
    event_date: datetime | None = None
    creator_position: str = Field(..., max_length=500)
    opponent_position: str = Field(..., max_length=500)
    stake_amount: Decimal = Field(..., gt=0, le=10000)
    creator_odds: float = Field(default=1.0, gt=0)
    opponent_odds: float = Field(default=1.0, gt=0)
    visibility: BetVisibility = BetVisibility.PUBLIC
    expires_at: datetime | None = None


class BetUpdate(BaseModel):
    title: str | None = Field(None, min_length=5, max_length=200)
    description: str | None = Field(None, max_length=2000)
    event_date: datetime | None = None
    creator_position: str | None = Field(None, max_length=500)
    opponent_position: str | None = Field(None, max_length=500)
    stake_amount: Decimal | None = Field(None, gt=0, le=10000)
    creator_odds: float | None = Field(None, gt=0)
    opponent_odds: float | None = Field(None, gt=0)
    expires_at: datetime | None = None


class BetResponse(BaseModel):
    id: int
    title: str
    description: str
    category: BetCategory
    event_name: str
    event_date: datetime | None
    creator_position: str
    opponent_position: str
    creator_odds: float
    opponent_odds: float
    stake_amount: Decimal
    currency: str
    status: BetStatus
    visibility: BetVisibility
    creator: UserSummary
    opponent: UserSummary | None
    expires_at: datetime | None
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(from_attributes=True)


class BetDetailResponse(BetResponse):
    winner_id: int | None
    settled_at: datetime | None
    settled_by: str | None


class SettleBetRequest(BaseModel):
    winner_id: int


class ProposalCreate(BaseModel):
    proposed_stake: Decimal = Field(..., gt=0, le=10000)
    proposed_creator_odds: float = Field(..., gt=0)
    proposed_opponent_odds: float = Field(..., gt=0)
    message: str | None = Field(None, max_length=500)
    expires_at: datetime


class ProposalResponse(BaseModel):
    id: int
    bet_id: int
    proposer_id: int
    proposed_stake: Decimal
    proposed_creator_odds: float
    proposed_opponent_odds: float
    message: str | None
    status: ProposalStatus
    created_at: datetime
    expires_at: datetime

    model_config = ConfigDict(from_attributes=True)
