from datetime import datetime
from typing import Optional
import uuid
from sqlalchemy import Column, DateTime, ForeignKey, UniqueConstraint
from sqlmodel import Field, SQLModel
from utils.datetime_utils import get_current_utc_datetime


class TeamMembershipModel(SQLModel, table=True):
    __tablename__ = "team_memberships"
    __table_args__ = (UniqueConstraint("user_id", "team_id"),)

    id: uuid.UUID = Field(primary_key=True, default_factory=uuid.uuid4)
    user_id: uuid.UUID = Field(sa_column=Column(ForeignKey("users.id"), index=True))
    team_id: uuid.UUID = Field(sa_column=Column(ForeignKey("teams.id"), index=True))
    assigned_by: Optional[uuid.UUID] = Field(
        sa_column=Column(ForeignKey("users.id", name="fk_assigned_by"), nullable=True),
        default=None,
    )
    assigned_at: datetime = Field(
        sa_column=Column(
            DateTime(timezone=True), nullable=False, default=get_current_utc_datetime
        ),
    )
