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

class MeasuringSalesProfitabilityService:
    def __init__(self, db:Session, token:JWTPayloadSchema):
        self.db = db
        self.token = token
        
    async def created(self, request:schema.MeasuringSalesProfitabilityCreate):
        created_data = MeasuringSalesProfitabilityBase.model_validate(request.model_dump())
        TblMeasuringSalesProfitability.create(created_data, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.MEASURING_SALES_PROFITABILITY)
    
    async def geted(self, group_id:int):
        geted_data = TblMeasuringSalesProfitability.get(group_id, self.db)
        if not geted_data:
            raise HTTPException(status_code=404, detail="Get group ID not found")
        return [schema.MeasuringSalesProfitabilityUpdate.model_validate(get_group) for get_group in geted_data]
    
    async def updated(self, request:List[schema.MeasuringSalesProfitabilityUpdate]):
        for req in request:
            updated_data = MeasuringSalesProfitabilityBase.model_validate(req.model_dump())
            if updated_data.measuring_id is None:
                return CustomResponse(status="-1", message=Messages.MEASURING_SALES_PROFITABILITY_NOT)
            TblMeasuringSalesProfitability.update(updated_data.measuring_id, updated_data, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.MEASURING_SALES_PROFITABILITY_UPDATE)
    
    async def deleted(self, measuring_id:int):
        deleted_data = TblMeasuringSalesProfitability.delete(measuring_id, self.db)
        if not deleted_data:
            return CustomResponse(status="-1", message=Messages.MEASURING_SALES_PROFITABILITY_NOT)
        return CustomResponse(status="1", message=Messages.MEASURING_SALES_PROFITABILITY_DELETE)
