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

class LocationDetailsService:
    def __init__(self, db:Session, token:JWTPayloadSchema):
        self.db = db
        self.token = token
        
    async def create_location_details(self, request:schema.LocationDetailsCreate):
        created_location_details = LocationDetailsBase.model_validate(request.model_dump())
        TblLocationDetails.create_location_details(created_location_details, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.LOCATION_DETAILS)
    
    async def get_location_details(self, group_id:int):
        new_get_location_details = TblLocationDetails.get_location_details(group_id, self.db)
        if not new_get_location_details:
            raise HTTPException(status_code=404, detail="Get Group ID not found")
        return [schema.LocationDetailsResponse.model_validate(get_group) for get_group in new_get_location_details]
    
    async def update_location_details(self, request:List[schema.LocationDetailsUpdate]):
        for req in request:
            updated_location_details = LocationDetailsBase.model_validate(req.model_dump())
            if updated_location_details.details_id is None:
                return CustomResponse(status="-1", message=Messages.LOCATION_DETAILS_NOT)
            TblLocationDetails.update_location_details(updated_location_details.details_id, updated_location_details, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.LOCATION_DETAILS_UPDATE)
    
    async def delete_location_details(self, details_id:int):
        deleted_location_details = TblLocationDetails.delete_location_details(details_id, self.db)
        if not deleted_location_details:
            return CustomResponse(status="-1", message=Messages.LOCATION_DETAILS_NOT)
        return CustomResponse(status="1", message=Messages.LOCATION_DETAILS_DELETE)
        