"""Forgejo repository model storing organization-level tracked repository metadata."""

from __future__ import annotations

from datetime import datetime
from uuid import UUID, uuid4

from sqlalchemy import Column
from sqlalchemy import JSON
from sqlmodel import Field

from app.core.time import utcnow
from app.models.base import QueryModel

RUNTIME_ANNOTATION_TYPES = (datetime,)


class ForgejoRepository(QueryModel, table=True):
    """Tracked Forgejo repository for organization."""

    __tablename__ = "forgejo_repositories"  # pyright: ignore[reportAssignmentType]

    id: UUID = Field(default_factory=uuid4, primary_key=True)
    organization_id: UUID = Field(foreign_key="organizations.id", index=True)
    connection_id: UUID = Field(foreign_key="forgejo_connections.id", index=True)
    owner: str
    repo: str
    display_name: str = Field(default="")
    default_branch: str = Field(default="main")
    active: bool = Field(default=True)
    webhook_secret: str | None = Field(default=None)
    description: str | None = Field(default=None)
    open_issues_count: int = Field(default=0)
    is_archived: bool = Field(default=False)
    topics: list[str] = Field(
        default_factory=list,
        sa_column=Column(JSON, nullable=False, server_default="[]"),
    )
    labels: list[dict[str, object]] = Field(
        default_factory=list,
        sa_column=Column(JSON, nullable=False, server_default="[]"),
    )
    last_sync_at: datetime | None = Field(default=None)
    last_sync_error: str | None = Field(default=None)
    created_at: datetime = Field(default_factory=utcnow)
    updated_at: datetime = Field(default_factory=utcnow)
