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

class CustomerEngagementService:
    def __init__(self, db:Session, token:JWTPayloadSchema):
        self.db = db
        self.token = token
        
    async def create_customer(self, request:schema.CustomerEngagementCreate):
        created_customer = CustomerEngagementBase.model_validate(request.model_dump())
        TblCustomerEngagement.create_customer(created_customer, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.CUSTOMER_ENGAGEMENT)
    
    async def get_customer(self, group_id:int):
        new_get_customer = TblCustomerEngagement.get_customer(group_id, self.db)
        if not new_get_customer:
            raise HTTPException(status_code=404, detail="Get groupp ID not found")
        return [schema.CustomerEngagementResponse.model_validate(get_group) for get_group in new_get_customer]
    
    async def update_customer(self, request:List[schema.CustomerEngagementUpdate]):
        for req in request:
            updated_customer = CustomerEngagementBase.model_validate(req.model_dump())
            if updated_customer.customer_id is None:
                return CustomResponse(status="-1", message=Messages.CUSTOMER_ENGAGEMENT_NOT)
            TblCustomerEngagement.update_customer(updated_customer.customer_id, updated_customer, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.CUSTOMER_ENGAGEMENT_UPDATE)
    
    async def delete_customer(self, customer_id:int):
        deleted_customer = TblCustomerEngagement.delete_customer(customer_id, self.db)
        if not deleted_customer:
            return CustomResponse(status="-1", message=Messages.CUSTOMER_ENGAGEMENT_NOT)
        return CustomResponse(status="1", message=Messages.CUSTOMER_ENGAGEMENT_DELETE)