from fastapi import HTTPException
from sqlalchemy.orm import Session
from app.api.vessel_list.schemas import VesseResponce, VesseUpdate, VesselCreat
from app.models.main.vessel_list import TblVesselList, VesselBase
from app.utils.schemas_utils import CustomResponse

class VesselService:
    def __init__(self, db: Session, token: dict):
        self.db = db
        self.token = token

    async def create_vessel(self, request: VesselCreat):
        created_admin = VesselBase.model_validate(request)
        TblVesselList.create(created_admin, self.db)
        self.db.commit()
        return CustomResponse(status="1", message="Vessel created successfully")
    
    async def get_vessel(self, vessel_id: int) -> VesseResponce:
        vessel = self.db.query(TblVesselList).filter(TblVesselList.vessel_id == vessel_id).first()
        if not vessel:
            raise HTTPException(status_code=404, detail="vessel not found")
        return VesseResponce.model_validate(vessel)
    
    async def update_vessel(self, request: VesseUpdate):
        vessel_base = VesselBase.model_validate(request)
        vessel = TblVesselList.update(request.vessel_id, vessel_base, self.db)
        if not vessel:
            raise HTTPException(status_code=404, detail="Vessel not found")
        return CustomResponse(status="1", message="Vessel updated successfully")


   