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


class ShortNegotiationFinalService:
    def __init__(self, db:Session, token:JWTPayloadSchema):
        self.db = db
        self.token = token
        
    async def create(self, request:schema.ShortNegotiationFinalCreate):
        created = ShortNegotiationFinalBase.model_validate(request.model_dump())
        TblShortNegotiationFinal.create(created, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.SHORT_NEGOTIATION_FINAL)
    
    async def get(self, group_id:int):
        geted = TblShortNegotiationFinal.get(group_id, self.db)
        if not geted:
            raise HTTPException(status_code=404, detail="Get group ID not found")
        return [schema.ShortNegotiationFinalResponse.model_validate(get_group) for get_group in geted]
    
    async def update(self, request:List[schema.ShortNegotiationFinalUpdate]):
        for req in request:
            updated = ShortNegotiationFinalBase.model_validate(req.model_dump())
            if updated.short_negotiation_final_id is None:
                return CustomResponse(status="-1", message=Messages.SHORT_NEGOTIATION_FINAL_NOT)
            TblShortNegotiationFinal.update(updated.short_negotiation_final_id, updated, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.SHORT_NEGOTIATION_FINAL_UPDATE)
    
    async def delete(self, short_negotiation_final_id:int):
        deleted = TblShortNegotiationFinal.delete(short_negotiation_final_id, self.db)
        if not deleted:
            return CustomResponse(status="-1", message=Messages.SHORT_NEGOTIATION_FINAL_NOT)
        return CustomResponse(status="1", message=Messages.SHORT_NEGOTIATION_FINAL_DELETE)
        