# #region Test.Models.Git [C:2] [TYPE Module] [SEMANTICS test,models,git,sqlalchemy]
# @BRIEF Tests for models/git.py — GitServerConfig, GitRepository, DeploymentEnvironment,
#        and enums GitProvider, GitStatus, SyncStatus.
# @RELATION BINDS_TO -> [GitModels]
# @TEST_EDGE: missing_field -> optional defaults
# @TEST_EDGE: serialization -> SQLAlchemy column attributes exist

from datetime import datetime, timezone
import pytest


class TestGitEnums:
    """Git enums — GitProvider, GitStatus, SyncStatus."""

    def test_git_provider_values(self):
        from src.models.git import GitProvider

        assert GitProvider.GITHUB == "GITHUB"
        assert GitProvider.GITLAB == "GITLAB"
        assert GitProvider.GITEA == "GITEA"

    def test_git_status_values(self):
        from src.models.git import GitStatus

        assert GitStatus.CONNECTED == "CONNECTED"
        assert GitStatus.FAILED == "FAILED"
        assert GitStatus.UNKNOWN == "UNKNOWN"

    def test_sync_status_values(self):
        from src.models.git import SyncStatus

        assert SyncStatus.CLEAN == "CLEAN"
        assert SyncStatus.DIRTY == "DIRTY"
        assert SyncStatus.CONFLICT == "CONFLICT"


class TestGitServerConfig:
    """GitServerConfig — SQLAlchemy model for Git server connection."""

    def test_table_name(self):
        from src.models.git import GitServerConfig

        assert GitServerConfig.__tablename__ == "git_server_configs"

    def test_columns_exist(self):
        from src.models.git import GitProvider, GitServerConfig, GitStatus

        obj = GitServerConfig(
            id="cfg-1",
            name="My GitHub",
            provider=GitProvider.GITHUB,
            url="https://github.com/org",
            pat="ghp_abc123",
            status=GitStatus.UNKNOWN,
        )
        assert obj.name == "My GitHub"
        assert obj.provider == GitProvider.GITHUB
        assert obj.status == GitStatus.UNKNOWN

    def test_default_branch(self):
        from src.models.git import GitProvider, GitServerConfig, GitStatus

        obj = GitServerConfig(
            id="cfg-2", name="Test", provider=GitProvider.GITLAB,
            url="https://gitlab.com/org", pat="abc",
            default_branch="main", status=GitStatus.CONNECTED,
        )
        assert obj.default_branch == "main"

    def test_with_repository(self):
        from src.models.git import GitProvider, GitServerConfig, GitStatus

        obj = GitServerConfig(
            id="cfg-3", name="Gitea", provider=GitProvider.GITEA,
            url="https://gitea.local", pat="tok",
            default_repository="org/repo", status=GitStatus.CONNECTED,
        )
        assert obj.default_repository == "org/repo"

    def test_last_validated(self):
        from src.models.git import GitProvider, GitServerConfig, GitStatus

        ts = datetime.now()
        obj = GitServerConfig(
            id="cfg-4", name="GH", provider=GitProvider.GITHUB,
            url="https://github.com", pat="tok",
            status=GitStatus.CONNECTED, last_validated=ts,
        )
        assert obj.last_validated == ts


class TestGitRepository:
    """GitRepository — SQLAlchemy model for local Git repo tracking."""

    def test_table_name(self):
        from src.models.git import GitRepository

        assert GitRepository.__tablename__ == "git_repositories"

    def test_columns_exist(self):
        from src.models.git import GitRepository, SyncStatus

        obj = GitRepository(
            id="repo-1",
            dashboard_id=42,
            config_id="cfg-1",
            remote_url="https://github.com/org/repo.git",
            local_path="/repos/repo",
            sync_status=SyncStatus.CLEAN,
        )
        assert obj.dashboard_id == 42
        assert obj.config_id == "cfg-1"
        assert obj.remote_url == "https://github.com/org/repo.git"
        assert obj.local_path == "/repos/repo"

    def test_current_branch(self):
        from src.models.git import GitRepository, SyncStatus

        obj = GitRepository(
            id="repo-2", dashboard_id=99, config_id="cfg-2",
            remote_url="https://example.com/repo.git",
            local_path="/repos/repo",
            current_branch="feature/test",
            sync_status=SyncStatus.DIRTY,
        )
        assert obj.current_branch == "feature/test"
        assert obj.sync_status == SyncStatus.DIRTY


class TestDeploymentEnvironment:
    """DeploymentEnvironment — SQLAlchemy model for Superset deployment target."""

    def test_table_name(self):
        from src.models.git import DeploymentEnvironment

        assert DeploymentEnvironment.__tablename__ == "deployment_environments"

    def test_columns_exist(self):
        from src.models.git import DeploymentEnvironment

        obj = DeploymentEnvironment(
            id="deploy-1",
            name="Production",
            superset_url="https://superset.example.com",
            superset_token="token123",
            is_active=True,
        )
        assert obj.name == "Production"
        assert obj.superset_url == "https://superset.example.com"
        assert obj.is_active is True

    def test_inactive(self):
        from src.models.git import DeploymentEnvironment

        obj = DeploymentEnvironment(
            id="deploy-2",
            name="Staging",
            superset_url="https://staging.example.com",
            superset_token="tok",
            is_active=False,
        )
        assert obj.is_active is False
# #endregion Test.Models.Git
