from typing import List
from sqlalchemy.orm import Session
from app.api.promotion_competitor_analysis import schema
from app.models.main.promotion_competitor_analysis import PromotionCompetitorAnalysisBase, TblPromotionCompetitorAnalysis
from app.utils.schemas_utils import CustomResponse
from app.locale.messages import Messages
from fastapi import HTTPException
from app.dependency.authantication import JWTPayloadSchema

class PromotionCompetitorAnalysisService:
    def __init__(self, db:Session, token:JWTPayloadSchema):
        self.db = db
        self.token = token
        
    async def create_promotion(self, request:schema.PromotionCompetitorAnalysisCreated):
        created_promotion = PromotionCompetitorAnalysisBase.model_validate(request.model_dump())
        TblPromotionCompetitorAnalysis.create_promotion(created_promotion, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.PROMOTION_COMPETITOR_ANALYSIS)
    
    async def get_promotion(self, group_id:int):
        new_get_promotion = TblPromotionCompetitorAnalysis.get_promotion(group_id, self.db)
        if not new_get_promotion:
            raise HTTPException(status_code=404, detail="Get group ID not found")
        return [schema.PromotionCompetitorAnalysisResponse.model_validate(get_group) for get_group in new_get_promotion]
    
    async def update_promotion(self, request:List[schema.PromotionCompetitorAnalysisUpdate]):
        for req in request:
            updated_promotion = PromotionCompetitorAnalysisBase.model_validate(req.model_dump())
            if updated_promotion.promotion_id is None:
                return CustomResponse(status="-1", message=Messages.PROMOTION_COMPETITOR_ANALYSIS_NOT)
            TblPromotionCompetitorAnalysis.update_promotion(updated_promotion.promotion_id, updated_promotion, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.PROMOTION_COMPETITOR_ANALYSIS_UPDATE)
    
    async def delete_promotion(self, promotion_id:int):
        deleted_promotion = TblPromotionCompetitorAnalysis.delete_promotion(promotion_id, self.db)
        if not deleted_promotion:
            return CustomResponse(status="-1", message=Messages.PROMOTION_COMPETITOR_ANALYSIS_NOT)
        return CustomResponse(status="1", message=Messages.PROMOTION_COMPETITOR_ANALYSIS_DELETE)