from typing import TYPE_CHECKING, Optional
from uuid import UUID

from sqlalchemy.orm import joinedload
from sqlalchemy.schema import Column, ForeignKey
from sqlmodel import Field, Relationship
from sqlmodel.sql.sqltypes import GUID

from joj.horse.models.base import ORMUtils
from joj.horse.utils.base import is_uuid

if TYPE_CHECKING:
    from joj.horse.models import Problem, ProblemSet


class ProblemProblemSetLink(ORMUtils, table=True):  # type: ignore[call-arg]
    __tablename__ = "problem_problem_set_links"

    problem_id: UUID = Field(
        sa_column=Column(
            GUID, ForeignKey("problems.id", ondelete="CASCADE"), primary_key=True
        ),
    )
    problem_set_id: UUID = Field(
        sa_column=Column(
            GUID, ForeignKey("problem_sets.id", ondelete="CASCADE"), primary_key=True
        ),
    )
    position: int = Field(
        index=True, nullable=False, sa_column_kwargs={"server_default": "0"}
    )

    problem: "Problem" = Relationship(back_populates="problem_problem_set_links")
    problem_set: "ProblemSet" = 