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


class AllContenService:
    def __init__(self, db:Session, token:JWTPayloadSchema):
        self.db = db
        self.token = token
        
    async def created(self, request:schema.AllContentCreate):
        created_data = AllContentBase.model_validate(request.model_dump())
        TblAllContent.create(created_data, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.ALL_CONTENT)
    
    async def geted(self, group_id:int):
        geted_data = TblAllContent.get(group_id, self.db)
        if not geted_data:
            raise HTTPException(status_code=404, detail="Get group ID not found")
        return [schema.AllContentResponse.model_validate(get_group) for get_group in geted_data]
    
    async def updated(self, request:List[schema.AllContentUpdate]):
        for req in request:
            updated_data = AllContentBase.model_validate(req.model_dump())
            if updated_data.all_id is None:
                return CustomResponse(status="-1", message=Messages.ALL_CONTENT_NOT)
            TblAllContent.update(updated_data.all_id, updated_data, self.db)
        self.db.commit()
        return CustomResponse(status="1", message=Messages.ALL_CONTENT_UPDATE)
        
    async def deleted(self, all_id:int):
        deleted_data = TblAllContent.delete(all_id, self.db)
        if not deleted_data:
            return CustomResponse(status="-1", message=Messages.ALL_CONTENT_NOT)
        return CustomResponse(status="1", message=Messages.ALL_CONTENT_DELETE)