from app.api.trading_month.schemas import TradingMonthCreate, TradingMonthResponce
from app.locale.messages import Messages
from sqlalchemy.orm import Session
from app.models.main.group import TblGroup
from app.models.main.trading_months import TblTradingMonth, TradingMonthBase
from app.utils.schemas_utils import CustomResponse

class TradingMonthService:
    def __init__(self, db: Session, token):
        self.db = db
        self.token = token

    async def create_trading_month(self, request: TradingMonthCreate):
       
        group = self.db.query(TblGroup).filter(TblGroup.group_id == request.group_id).first()
        if not group:
            return CustomResponse(status="-1", message=f"group_id={request.group_id} does not exist")
        try:
            trading_data = TradingMonthBase.model_validate(request.model_dump())
            TblTradingMonth.create(trading_data, self.db)
            self.db.commit()
            return CustomResponse(status="1", message="Trading Month created successfully")
        except Exception as e:
            self.db.rollback()
            return CustomResponse(status="-1", message=f"Error creating trading month: {str(e)}")

    async def get_trading(self, group_id: int):
        segment = TblTradingMonth.get_by_group_id(group_id, self.db)
        if not segment:
            return CustomResponse(status="-1", message=Messages.SEGMENT_NOT_FOUND)
        return TradingMonthResponce.model_validate(segment)
    