# ruff: noqa: S101
"""Tests for Forgejo models (connections and repositories)."""

from __future__ import annotations

from datetime import datetime
from uuid import UUID, uuid4

import pytest

from app.models.forgejo_connections import ForgejoConnection
from app.models.forgejo_repositories import ForgejoRepository


def test_forgejo_connection_creation() -> None:
    """Test ForgejoConnection model construction."""
    org_id = uuid4()
    connection = ForgejoConnection(
        id=uuid4(),
        organization_id=org_id,
        name="Forgejo Production",
        base_url="https://forgejo.example.com",
        token="ghp_testtoken123",
        token_last_eight="n123",
        active=True,
    )

    assert connection.id is not None
    assert connection.organization_id == org_id
    assert connection.name == "Forgejo Production"
    assert connection.base_url == "https://forgejo.example.com"
    assert connection.token == "ghp_testtoken123"
    assert connection.token_last_eight == "n123"
    assert connection.active is True
    assert isinstance(connection.created_at, datetime)
    assert isinstance(connection.updated_at, datetime)


def test_forgejo_connection_defaults() -> None:
    """Test ForgejoConnection model with default values."""
    connection = ForgejoConnection(
        id=uuid4(),
        organization_id=uuid4(),
        name="Default Forgejo",
        base_url="https://forgejo.internal",
    )

    assert connection.token is None
    assert connection.token_last_eight is None
    assert connection.active is True  # default is True


def test_forgejo_repository_creation() -> None:
    """Test ForgejoRepository model construction."""
    org_id = uuid4()
    conn_id = uuid4()
    repo = ForgejoRepository(
        id=uuid4(),
        organization_id=org_id,
        connection_id=conn_id,
        owner="openclaw",
        repo="openclaw",
        display_name="OpenClaw",
        default_branch="main",
        active=True,
    )

    assert repo.id is not None
    assert repo.organization_id == org_id
    assert repo.connection_id == conn_id
    assert repo.owner == "openclaw"
    assert repo.repo == "openclaw"
    assert repo.display_name == "OpenClaw"
    assert repo.default_branch == "main"
    assert repo.active is True
    assert repo.last_sync_at is None
    assert repo.last_sync_error is None


def test_forgejo_repository_defaults() -> None:
    """Test ForgejoRepository model with default values."""
    repo = ForgejoRepository(
        id=uuid4(),
        organization_id=uuid4(),
        connection_id=uuid4(),
        owner="test",
        repo="test-repo",
    )

    assert repo.display_name == ""
    assert repo.default_branch == "main"
    assert repo.active is True
    assert repo.last_sync_at is None
    assert repo.last_sync_error is None


def test_forgejo_repository_updated_at() -> None:
    """Test ForgejoRepository updated_at field."""
    now = datetime(2026, 5, 19, 12, 0, 0)
    repo = ForgejoRepository(
        id=uuid4(),
        organization_id=uuid4(),
        connection_id=uuid4(),
        owner="test",
        repo="test-repo",
    )
    repo.updated_at = now

    assert repo.updated_at == now


def test_forgejo_connection_token_last_eight_format() -> None:
    """Test that token_last_eight stores last 8 chars of token."""
    connection = ForgejoConnection(
        id=uuid4(),
        organization_id=uuid4(),
        name="Forgejo Test",
        base_url="https://forgejo.test",
        token="a_very_long_token_string",
        token_last_eight="string",
    )

    assert connection.token == "a_very_long_token_string"
    assert connection.token_last_eight == "string"
