from fastapi import HTTPException
from app.api.marketing_competitor_analysis import schema
from app.dependency.authantication import JWTPayloadSchema
from app.locale.messages import Messages
from app.models.main.marketing_competitor_analysis import MarketingCompetitorBase, TblMarketingCompetitor
from app.utils.schemas_utils import CustomResponse
from sqlalchemy.orm import Session

class MarketingCompetitorService:
    def __init__(self, db:Session, token:JWTPayloadSchema):
        self.db = db
        self.token = token
        
    async def create_marketing_competitor(self, request:schema.MarketingCompetitorCreate):
        created_marketing_competitor = MarketingCompetitorBase.model_validate(request.model_dump())
        TblMarketingCompetitor.create_marketing_competitor(created_marketing_competitor, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.MARKETING_COMPETITOR)
    
    async def get_marketing_competitor(self, group_id:int):
        new_get_marketing_competitor = TblMarketingCompetitor.get_marketing_competitor(group_id, self.db)
        if not new_get_marketing_competitor:
            raise HTTPException(status_code=404, detail="Get group ID not found")
        return [schema.MarketingCompetitorResponse.model_validate(get_group) for get_group in new_get_marketing_competitor]
    
    async def update_marketing_competitor(self, request:schema.MarketingCompetitorUpdate):
        updated_marketing_competitor = MarketingCompetitorBase.model_validate(request.model_dump())
        if updated_marketing_competitor.competitor_id is None:
            return CustomResponse(status="-1", message=Messages.MARKETING_COMPETITOR_NOT)
        TblMarketingCompetitor.update_market_comppetitor(updated_marketing_competitor.competitor_id, updated_marketing_competitor, self.db)
        return CustomResponse(status="1", message=Messages.MARKETING_COMPETITOR_UPDATE)
    
    async def delete_marketing_competitor(self, group_id:int):
        deleted_marketing_competitor = TblMarketingCompetitor.delete_market_competitor(group_id, self.db)
        if not deleted_marketing_competitor:
            return CustomResponse(status="-1", message=Messages.MARKETING_COMPETITOR_NOT)
        return CustomResponse(status="1", message=Messages.MARKETING_COMPETITOR_DELETE)
        