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

class ComparativeAnalysisService:
    def __init__(self, db:Session, token:JWTPayloadSchema):
        self.db = db
        self.token = token
        
    async def created(self, request:schema.ComparativeAnalysisCreate):
        created_data = ComparativeAnalysisBase.model_validate(request.model_dump())
        TblComparativeAnalysis.create(created_data, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.COMPARATIVEE_ANALYSIS)
    
    async def geted(self, group_id:int):
        geted_data = TblComparativeAnalysis.get(group_id, self.db)
        if not geted_data:
            raise HTTPException(status_code=404, detail="Get group ID not found")
        return [schema.ComparativeAnalysisResponse.model_validate(get_group) for get_group in geted_data]
    
    # async def get_own_brand_data(self, group_id:int):
    #     get_data = self.db.query(TblBrandingAttributes).filter(TblBrandingAttributes.group_id == group_id).all()
    #     if not get_data:
    #         raise HTTPException(status_code=404, detail="Get group ID not found")
    #     return [schema.OwnBrand.model_validate({
    #         "own_brand": get_group.own_brand,
    #         "group_id": group_id
    #     }) for get_group in get_data]
    #     # return schema.OwnBrand.model_validate(get_data)
    async def get_own_brand_data(self):
        get_data = self.db.query(TblBrandingAttributes.own_brand).all()
        if not get_data:
            raise HTTPException(status_code=404, detail="Get group ID not found")
        return [schema.OwnBrand.model_validate(get_group) for get_group in get_data]
        # return schema.OwnBrand.model_validate(get_data)
    
    async def updated(self, request:List[schema.ComparativeAnalysisUpdate]):
        for req in request:
            updated_data = ComparativeAnalysisBase.model_validate(req.model_dump())
            if updated_data.comparative_id is None:
                return CustomResponse(status="-1", message=Messages.COMPARATIVEE_ANALYSIS_NOT)
            TblComparativeAnalysis.update(updated_data.comparative_id, updated_data, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.COMPARATIVEE_ANALYSIS_UPDATE)
    
    async def deleted(self, comparative_id:int):
        deleted_data = TblComparativeAnalysis.delete(comparative_id, self.db)
        if not deleted_data:
            return CustomResponse(status="-1", message=Messages.COMPARATIVEE_ANALYSIS_NOT)
        return CustomResponse(status="1", message=Messages.COMPARATIVEE_ANALYSIS_DELETE)
