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

class VisualMerchStrategyService:
    def __init__(self, db:Session, token:JWTPayloadSchema):
        self.db = db
        self.token = token
        
    async def created(self, request:schema.VisualMerchStrategyCreate):
        created_data = VisualMerchStrategyBase.model_validate(request.model_dump())
        TblVisualMerchStrategy.create(created_data, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.VISUAL_MERCHAIDISING)
    
    async def geted(self, group_id:int):
        geted_data = TblVisualMerchStrategy.get(group_id, self.db)
        if not group_id:
            raise HTTPException(status_code=404, detail="Get group ID not found")
        return [schema.VisualMerchStrategyResponse.model_validate(get_group) for get_group in geted_data]
    
    async def updated(self, request:List[schema.VisualMerchStrategyUpdate]):
        for req in request:
            updated_data = VisualMerchStrategyBase.model_validate(req.model_dump())
            if updated_data.visual_id is None:
                return CustomResponse(status="-1", message=Messages.VISUAL_MERCHAIDISING_NOT)
            TblVisualMerchStrategy.update(updated_data.visual_id, updated_data,self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.VISUAL_MERCHAIDISING_UPDATE)
    
    async def deleted(self, visual_id:int):
        deleted_data = TblVisualMerchStrategy.delete(visual_id,self.db)
        if not deleted_data:
            return CustomResponse(status="1", message=Messages.VISUAL_MERCHAIDISING_NOT)
        return CustomResponse(status="1", message=Messages.VISUAL_MERCHAIDISING_DELETE)