from typing import List
from fastapi import HTTPException
from sqlalchemy import func
from sqlalchemy.orm import Session
from app.api.depreciation import schema
from app.dependency.authantication import JWTPayloadSchema
from app.locale.messages import Messages
from app.models.main.depreciation import DepreciationBase, TblDepreciation
from app.utils.schemas_utils import CustomResponse

class DepreciationService:
    def __init__(self, db:Session, token:JWTPayloadSchema):
        self.db = db
        self.token = token
        
    async def create_depreciation(self, request:List[schema.DepreciationCreate]):
        created_depreciation = []
        for item in request:
            sale = TblDepreciation.create(item, self.db)
            created_depreciation.append(sale)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.DEPRECIATION_CREATE)
    
    # async def create_other_depreciation(self, request: schema.OtherDepreciationCreate):
    #     create = DepreciationBases.model_validate(request.model_dump())
    #     created_data = TblDepreciation(**create.model_dump())
    #     self.db.add(created_data)
    #     self.db.commit()
    #     self.db.refresh(created_data)
    #     return CustomResponse(status="1", message=Messages.OTHER_DEPRICIATION)
    
    async def get_depreciation(self, depreciation_get_id:int):
        new_get_depreciation = TblDepreciation.get_depreciation_data(depreciation_get_id, self.db)
        return schema.DepreciationResponse.model_validate(new_get_depreciation)
    
    async def get_group_depreciation(self, group_id:int):
        new_get_group_depreciation = self.db.query(TblDepreciation).filter(TblDepreciation.group_id == group_id).all()
        if not new_get_group_depreciation:
            raise HTTPException(status_code=404, detail="Get group ID not found")
        return [schema.DepreciationResponse.model_validate(get_group) for get_group in new_get_group_depreciation]
    
    async def update_depreciation_data(self, request:List[schema.DepreciationUpdate]):
        for req in request:
            updated_depreciation_data = DepreciationBase.model_validate(req.model_dump())
            if updated_depreciation_data.depreciation_id is None:
                return CustomResponse(status="-1", message=Messages.DEPRECIATION_UPDATE_NOT)
            TblDepreciation.update_depreciation(updated_depreciation_data.depreciation_id,updated_depreciation_data,self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.DEPRECIATION_UPDATE_SUC)
    
    async def delete_depreciation(self, depreciation_delete_id:int):
        deleted_depreciation = TblDepreciation.delete_depreciation_data(depreciation_delete_id,self.db)
        if not deleted_depreciation:
            return CustomResponse(status="-1", message=Messages.DEPRECIATION_UPDATE_NOT)
        return CustomResponse(status="1", message=Messages.DEPRECIATION_DELETE_SUC)