from pydantic import BaseModel, ConfigDict, model_validator

from core.config import get_db
from models import CourseAllocation, Course


class CourseAllocationBase(BaseModel):
    course_id: int
    staff_id: int
    role_id: int


class CourseAllocationCreateSchema(CourseAllocationBase):

    @model_validator(mode='after')
    def validate(self):
        with get_db() as db:
            if db.query(CourseAllocation).filter(CourseAllocation.staff_id == self.staff_id,
                                                 CourseAllocation.course_id == self.course_id,
                                                 CourseAllocation.deleted_at.is_(None)).count() > 0:
                raise ValueError("Teacher already assigned to this course")

        return self


class CourseAllocationResponse(CourseAllocationBase):
    id: int
    staff_name: str
    staff_role: str
    course_name: str
    model_config = ConfigDict(from_attributes=True)


class CourseAllocationUpdateSchema(CourseAllocationBase):
    id: int
