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

class LocationSpillageFactorAnalysisService:
    def __init__(self, db:Session, token:JWTPayloadSchema):
        self.db = db
        self.token = token
    
    async def create_location(self, request:schema.LocationSpillageFactorCreate):
        created_location = LocationSpillageFactorBase.model_validate(request.model_dump())
        TblLocationSpillageFactor.create_location(created_location, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.LOCATION_FACTORS)
    
    async def get_location(self, group_id:int):
        new_get_location = TblLocationSpillageFactor.get_location(group_id, self.db)
        if not new_get_location:
            raise HTTPException(status_code=404, detail="Get group ID not found")
        return [schema.LocationSpillageFactorResponse.model_validate(get_group) for get_group in new_get_location]
    
    async def update_location1(self, request:List[schema.LocationSpillageFactorUpdate]):
        for req in request:
            updated_location = LocationSpillageFactorBase.model_validate(req.model_dump())
            if updated_location.location_id is None:
                return CustomResponse(status="-1", message=Messages.LOCATION_FACTORS_NOT)
            TblLocationSpillageFactor.update_location(updated_location.location_id, updated_location, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.LOCATION_FACTORS_UPDATE)
    
    async def delete_location(self, location_id:int):
        deleted_location = TblLocationSpillageFactor.delete_location(location_id, self.db)
        if not deleted_location:
            return CustomResponse(status="-1", message=Messages.LOCATION_FACTORS_NOT)
        return CustomResponse(status="1", message=Messages.LOCATION_FACTORS_DELETE)
        