# [DEF:GitModels:Module]
# @TIER: TRIVIAL
# @SEMANTICS: git, models, sqlalchemy, database, schema
# @PURPOSE: Git-specific SQLAlchemy models for configuration and repository tracking.
# @LAYER: Model
# @RELATION: specs/011-git-integration-dashboard/data-model.md

import enum
from datetime import datetime
from sqlalchemy import Column, String, Integer, DateTime, Enum, ForeignKey, Boolean
import uuid
from src.core.database import Base

class GitProvider(str, enum.Enum):
    GITHUB = "GITHUB"
    GITLAB = "GITLAB"
    GITEA = "GITEA"

class GitStatus(str, enum.Enum):
    CONNECTED = "CONNECTED"
    FAILED = "FAILED"
    UNKNOWN = "UNKNOWN"

class SyncStatus(str, enum.Enum):
    CLEAN = "CLEAN"
    DIRTY = "DIRTY"
    CONFLICT = "CONFLICT"

# [DEF:GitServerConfig:Class]
# @TIER: TRIVIAL
# @PURPOSE: Configuration for a Git server connection.
class GitServerConfig(Base):
    __tablename__ = "git_server_configs"

    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    name = Column(String(255), nullable=False)
    provider = Column(Enum(GitProvider), nullable=False)
    url = Column(String(255), nullable=False)
    pat = Column(String(255), nullable=False)  # PERSONAL ACCESS TOKEN
    default_repository = Column(String(255), nullable=True)
    default_branch = Column(String(255), default="main")
    status = Column(Enum(GitStatus), default=GitStatus.UNKNOWN)
    last_validated = Column(DateTime, default=datetime.utcnow)
# [/DEF:GitServerConfig:Class]

# [DEF:GitRepository:Class]
# @TIER: TRIVIAL
# @PURPOSE: Tracking for a local Git repository linked to a dashboard.
class GitRepository(Base):
    __tablename__ = "git_repositories"

    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    dashboard_id = Column(Integer, nullable=False, unique=True)
    config_id = Column(String(36), ForeignKey("git_server_configs.id"), nullable=False)
    remote_url = Column(String(255), nullable=False)
    local_path = Column(String(255), nullable=False)
    current_branch = Column(String(255), default="dev")
    sync_status = Column(Enum(SyncStatus), default=SyncStatus.CLEAN)
# [/DEF:GitRepository:Class]

# [DEF:DeploymentEnvironment:Class]
# @TIER: TRIVIAL
# @PURPOSE: Target Superset environments for dashboard deployment.
class DeploymentEnvironment(Base):
    __tablename__ = "deployment_environments"

    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    name = Column(String(255), nullable=False)
    superset_url = Column(String(255), nullable=False)
    superset_token = Column(String(255), nullable=False)
    is_active = Column(Boolean, default=True)
# [/DEF:DeploymentEnvironment:Class]

# [/DEF:GitModels:Module]
