"""Coordination component state model."""

from typing import Optional

from sqlalchemy import PrimaryKeyConstraint, String, Text
from sqlalchemy.orm import Mapped, mapped_column

from .base import Base, TimestampMixin


class CoordComponentState(Base, TimestampMixin):
    """Current state of a named component within a project."""

    __tablename__ = "coord_component_states"

    project_key: Mapped[str] = mapped_column(
        String(200),
        nullable=False,
        primary_key=True,
        doc="Project namespace"
    )

    component: Mapped[str] = mapped_column(
        String(200),
        nullable=False,
        primary_key=True,
        doc="Component name, e.g. 'server', 'agent', 'dashboard', 'database'"
    )

    state: Mapped[str] = mapped_column(
        String(50),
        nullable=False,
        doc="State: deployed, building, stable, broken, unknown"
    )

    version: Mapped[Optional[str]] = mapped_column(
        String(100),
        doc="Version string or git SHA"
    )

    notes: Mapped[Optional[str]] = mapped_column(
        Text,
        doc="Freeform notes about current state"
    )

    updated_by: Mapped[str] = mapped_column(
        String(200),
        nullable=False,
        doc="Session that last updated this record"
    )

    __table_args__ = (
        PrimaryKeyConstraint("project_key", "component", name="pk_coord_component_states"),
    )

    def __repr__(self) -> str:
        return f"<CoordComponentState(project_key='{self.project_key}', component='{self.component}', state='{self.state}')>"
