# #region GitModels [TYPE Module] [SEMANTICS sqlalchemy, git, model, config, sync]

from datetime import UTC, datetime
import enum
import uuid

from sqlalchemy import Boolean, Column, DateTime, Enum, ForeignKey, Integer, String

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"

# #region GitServerConfig [C:1] [TYPE Class]
# @BRIEF 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=lambda: datetime.now(UTC))
# #endregion GitServerConfig

# #region GitRepository [C:1] [TYPE Class]
# @BRIEF 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)
# #endregion GitRepository

# #region DeploymentEnvironment [C:1] [TYPE Class]
# @BRIEF 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)
# #endregion DeploymentEnvironment

# #endregion GitModels
