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

class OnlinePreSelectServiceService:
    def __init__(self, db:Session, token:JWTPayloadSchema):
        self.db = db
        self.token = token
        
    async def created(self, request:list[schema.OnlinePreSelectServiceCreate]):
        create = []
        for req in request:
            created_data = OnlinePreSelectServiceBase.model_validate(req.model_dump())
            sale = TblOnlinePreSelectService.create(created_data, self.db)
            create.append(sale)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.ONLINE_PRE_SELECTES)
    
    async def geted(self, group_id:int):
        geted_data = TblOnlinePreSelectService.get(group_id, self.db)
        if not geted_data:
            raise HTTPException(status_code=404, detail="Get group ID not found")
        return [schema.OnlinePreSelectServiceResponse.model_validate(get_group) for get_group in geted_data]
    
    async def updated(self, request:List[schema.OnlinePreSelectServiceUpdate]):
        for req in request:
            updated_data = OnlinePreSelectServiceBase.model_validate(req.model_dump())
            if updated_data.online_id is None:
                return CustomResponse(status="-1", message=Messages.ONLINE_PRE_SELECTES_NOT)
            TblOnlinePreSelectService.update(updated_data.online_id, updated_data, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.ONLINE_PRE_SELECTES_UPDATE)
    
    async def deleted(self, online_id:int):
        deleted_data = TblOnlinePreSelectService.delete(online_id, self.db)
        if not deleted_data:
            return CustomResponse(status="-1", message=Messages.ONLINE_PRE_SELECTES_NOT)
        return CustomResponse(status="1", message=Messages.ONLINE_PRE_SELECTES_DELETE)
            
