"""Odds repository"""

from __future__ import annotations

from typing import Optional, List
from sqlalchemy.orm import Session
from app.core.db.models.odds import Odds


class OddsRepository:
    """Repository for odds operations"""

    def __init__(self, db: Session):
        self.db = db

    def get_latest_1x2_for_match(self, match_id: int, bookmaker: Optional[str] = None) -> Optional[Odds]:
        q = self.db.query(Odds).filter(Odds.match_id == match_id)
        if bookmaker:
            q = q.filter(Odds.bookmaker == bookmaker)
        return q.order_by(Odds.captured_at.desc()).first()

    def get_history_for_match(self, match_id: int, limit: int = 50) -> List[Odds]:
        return (
            self.db.query(Odds)
            .filter(Odds.match_id == match_id)
            .order_by(Odds.captured_at.desc())
            .limit(limit)
            .all()
        )
