from sqlalchemy import func, select
from app.api.promotion_calender import schema
from app.dependency.authantication import JWTPayloadSchema
from sqlalchemy.orm import Session
from app.locale.messages import Messages
from fastapi import HTTPException
from app.models.main.promotion_calender import PromotionCalenderBase, TblPromotionCalender
from app.utils.schemas_utils import CustomResponse
from typing import List


class PromotionCalenderService:
    def __init__(self, db:Session, token:JWTPayloadSchema):
        self.db = db
        self.token = token
        
    async def created(self, request: schema.PromotionCalenderCreate):
        stmt = select(func.count()).select_from(TblPromotionCalender).where(
            TblPromotionCalender.month == request.month
        )
        result = self.db.execute(stmt)
        count = result.scalar_one()
        if count >= 3:
            raise HTTPException(
                status_code=400,
                detail=f"Maximum of 3 promotions allowed for {request.month}."
            )

        created_data = TblPromotionCalender(**request.model_dump())
        self.db.add(created_data)
        self.db.commit()
        self.db.refresh(created_data)

        return CustomResponse(
            status="1",
            message=Messages.PROMOTION_COLENDER
        )
        
    # async def created(self, request:schema.PromotionCalenderCreate):
    #     if TblPromotionCalender.month == request.month:
    #     if TblPromotionCalender.promotion_id <= 3:
    #         raise HTTPException(status_code=404, detail="ID not found")
    #     created_data = PromotionCalenderBase.model_validate(request.model_dump())
    #     TblPromotionCalender.create(created_data, self.db)
    #     self.db.commit()
    #     return CustomResponse(status="1", message=Messages.PROMOTION_COLENDER)
    
    async def geted(self, group_id:int):
        geted_data = TblPromotionCalender.get(group_id, self.db)
        if not geted_data:
            raise HTTPException(status_code=404, detail="Get group ID not found")
        return [schema.PromotionCalenderResponse.model_validate(get_group) for get_group in geted_data]
    
    async def updated(self, request:List[schema.PromotionCalenderUpdate]):
        for req in request:
            updated_data = PromotionCalenderBase.model_validate(req.model_dump())
            if updated_data.promotion_id is None:
                return CustomResponse(status="-1", message=Messages.PROMOTION_COLENDER_NOT)
            TblPromotionCalender.update(updated_data.promotion_id, updated_data, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.PROMOTION_COLENDER_UPDATE)
        
    async def deleted(self, promotion_id:int):
        deleted_data = TblPromotionCalender.delete(promotion_id, self.db)
        if not deleted_data:
            return CustomResponse(status="-1", message=Messages.PROMOTION_COLENDER_NOT)
        return CustomResponse(status="1", message=Messages.PROMOTION_COLENDER_DELETE)