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

class BrandingAttributesService:
    def __init__(self, db:Session, token:JWTPayloadSchema):
        self.db = db
        self.token = token
        
    async def create_branding_attributes(self, request:schema.BrandingAttributesCreate):
        created_branding_attributes = BrandingAttributesBase.model_validate(request.model_dump())
        TblBrandingAttributes.create_branding_attributes(created_branding_attributes, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.BRANDING_ATTRIBUTES)
    
    async def get_branding_attributes(self, group_id:int):
        new_get_branding_attributes = TblBrandingAttributes.get_branding_attributes(group_id, self.db)
        if not new_get_branding_attributes:
            raise HTTPException(status_code=404, detail="Get group ID not found")
        return [schema.BrandingAttributesResponse.model_validate(get_group) for get_group in new_get_branding_attributes]
    
    async def update_branding_attributes(self, request:List[schema.BrandingAttributesUpdate]):
        for req in request:
            updated_branding_attributes = BrandingAttributesBase.model_validate(req.model_dump())
            if updated_branding_attributes.brand_id is None:
                return CustomResponse(status="-1", message=Messages.BRANDING_ATTRIBUTES_NOT)
            TblBrandingAttributes.update_branding_attributes(updated_branding_attributes.brand_id, updated_branding_attributes, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.BRANDING_ATTRIBUTES_UPDATE)
    
    async def delete_branding_attributes(self, brand_id:int):
        deleted_branding_attributes = TblBrandingAttributes.delete_branding_attributes(brand_id, self.db)
        if not deleted_branding_attributes:
            return CustomResponse(status="-1", message=Messages.BRANDING_ATTRIBUTES_NOT)
        return CustomResponse(status="1", message=Messages.BRANDING_ATTRIBUTES_DELETE)