from app.services.subject_service import SubjectService
from app.helpers import make_response, make_error_response


class SubjectController:
    def __init__(self, service: SubjectService):
        self.service = service

    def create(self, req_body):
        try:
            new_id = self.service.create_subject(req_body)
            return make_response(message="Subject created", data={"id": new_id})
        except Exception as e:
            return make_error_response(e)

    def get_all(self):
        try:
            subjects = self.service.get_all_subjects()
            return make_response(message="success retrieve subject", data=subjects)
        except Exception as e:
            return make_error_response(e)

    def get_by_id(self, subject_id: str):
        try:
            subject = self.service.get_subject_by_id(subject_id)
            if not subject:
                return make_response(message="Subject not found", status_code=404)
            return make_response(data=subject.model_dump())
        except Exception as e:
            return make_error_response(e)

    def update(self, subject_id: str, req_body):
        try:
            updated = self.service.update_subject(subject_id, req_body)
            if not updated:
                return make_response(message="No subject updated", status_code=404)
            return make_response(message="Subject updated")
        except Exception as e:
            return make_error_response(e)

    def delete(self, subject_id: str):
        try:
            deleted = self.service.delete_subject(subject_id)
            if not deleted:
                return make_response(message="No subject deleted", status_code=404)
            return make_response(message="Subject deleted")
        except Exception as e:
            return make_error_response(e)
