from typing import Any

from infrahub.core.constants import (
    AllowOverrideType,
    BranchSupportType,
    InfrahubKind,
    RepositoryInternalStatus,
    RepositoryOperationalStatus,
    RepositorySyncStatus,
)
from infrahub.core.schema.attribute_schema import AttributeSchema
from infrahub.core.schema.node_schema import NodeSchema

core_repository = NodeSchema(
    name="Repository",
    namespace="Core",
    description="A Git Repository integrated with Infrahub",
    include_in_menu=False,
    icon="mdi:source-repository",
    label="Repository",
    default_filter="name__value",
    order_by=["name__value"],
    display_labels=["name__value"],
    generate_profile=False,
    branch=BranchSupportType.AGNOSTIC,
    inherit_from=[
        InfrahubKind.LINEAGEOWNER,
        InfrahubKind.LINEAGESOURCE,
        InfrahubKind.GENERICREPOSITORY,
        InfrahubKind.TASKTARGET,
    ],
    documentation="/topics/repository",
    attributes=[
        AttributeSchema(
            name="default_branch",
            kind="Text",
            default_value="main",
            order_weight=6000,
        ),
        AttributeSchema(
            name="commit",
            kind="Text",
            optional=True,
            branch=BranchSupportType.LOCAL,
            order_weight=7000,
        ),
    ],
)

core_read_only_repository: dict[str, Any] = {
    "name": "ReadOnlyRepository",
    "namespace": "Core",
    "description": "A Git Repository integrated with Infrahub, Git-side will not be updated",
    "include_in_menu": False,
    "label": "Read-Only Repository",
    "default_filter": "name__value",
    "order_by": ["name__value"],
    "display_labels": ["name__value"],
    "generate_profile": False,
    "branch": BranchSupportType.AGNOSTIC.value,
    "inherit_from": [
        InfrahubKind.LINEAGEOWNER,
        InfrahubKind.LINEAGESOURCE,
        InfrahubKind.GENERICREPOSITORY,
        InfrahubKind.TASKTARGET,
    ],
    "documentation": "/topics/repository",
    "attributes": [
        {
            "name": "ref",
            "kind": "Text",
            "default_value": "main",
            "branch": BranchSupportType.AWARE.value,
            "order_weight": 6000,
        },
        {
            "name": "commit",
            "kind": "Text",
            "optional": True,
            "branch": BranchSupportType.AWARE.value,
            "order_weight": 7000,
        },
    ],
}

core_generic_repository: dict[str, Any] = {
    "name": "GenericRepository",
    "namespace": "Core",
    "label": "Git Repository",
    "description": "A Git Repository integrated with Infrahub",
    "include_in_menu": False,
    "default_filter": "name__value",
    "order_by": ["name__value"],
    "display_labels": ["name__value"],
    "icon": "mdi:source-repository",
    "branch": BranchSupportType.AGNOSTIC.value,
    "uniqueness_constraints": [["name__value"], ["location__value"]],
    "documentation": "/topics/repository",
    "attributes": [
        {
            "name": "name",
            "regex": "^[^/]*$",
            "kind": "Text",
            "unique": True,
            "branch": BranchSupportType.AGNOSTIC.value,
            "order_weight": 1000,
            "allow_override": AllowOverrideType.NONE,
        },
        {
            "name": "description",
            "kind": "Text",
            "optional": True,
            "branch": BranchSupportType.AGNOSTIC.value,
            "order_weight": 2000,
            "allow_override": AllowOverrideType.NONE,
        },
        {
            "name": "location",
            "kind": "Text",
            "unique": True,
            "branch": BranchSupportType.AGNOSTIC.value,
            "order_weight": 3000,
            "allow_override": AllowOverrideType.NONE,
        },
        {
            "name": "internal_status",
            "kind": "Dropdown",
            "choices": [
                {
                    "name": RepositoryInternalStatus.STAGING.value,
                    "label": "Staging",
                    "description": "Repository was recently added to this branch.",
                    "color": "#fef08a",
                },
                {
                    "name": RepositoryInternalStatus.ACTIVE.value,
                    "label": "Active",
                    "description": "Repository is actively being synced for this branch",
                    "color": "#86efac",
                },
                {
                    "name": RepositoryInternalStatus.INACTIVE.value,
                    "label": "Inactive",
                    "description": "Repository is not active on this branch.",
                    "color": "#e5e7eb",
                },
            ],
            "default_value": "inactive",
            "optional": False,
            "branch": BranchSupportType.LOCAL.value,
            "order_weight": 7000,
            "allow_override": AllowOverrideType.NONE,
        },
        {
            "name": "operational_status",
            "kind": "Dropdown",
            "choices": [
                {
                    "name": RepositoryOperationalStatus.UNKNOWN.value,
                    "label": "Unknown",
                    "description": "Status of the repository is unknown and mostlikely because it hasn't been synced yet",
                    "color": "#9ca3af",
                },
                {
                    "name": RepositoryOperationalStatus.ONLINE.value,
                    "label": "Online",
                    "description": "Repository connection is working",
                    "color": "#86efac",
                },
                {
                    "name": RepositoryOperationalStatus.ERROR_CRED.value,
                    "label": "Credential Error",
                    "description": "Repository can't be synced due to some credential error(s)",
                    "color": "#f87171",
                },
                {
                    "name": RepositoryOperationalStatus.ERROR_CONNECTION.value,
                    "label": "Connectivity Error",
                    "description": "Repository can't be synced due to some connectivity error(s)",
                    "color": "#f87171",
                },
                {
                    "name": RepositoryOperationalStatus.ERROR.value,
                    "label": "Error",
                    "description": "Repository can't be synced due to an unknown error",
                    "color": "#ef4444",
                },
            ],
            "optional": False,
            "branch": BranchSupportType.AGNOSTIC.value,
            "default_value": RepositoryOperationalStatus.UNKNOWN.value,
            "order_weight": 5000,
        },
        {
            "name": "sync_status",
            "kind": "Dropdown",
            "choices": [
                {
                    "name": RepositorySyncStatus.UNKNOWN.value,
                    "label": "Unknown",
                    "description": "Status of the repository is unknown and mostlikely because it hasn't been synced yet",
                    "color": "#9ca3af",
                },
                {
                    "name": RepositorySyncStatus.ERROR_IMPORT.value,
                    "label": "Import Error",
                    "description": "Repository import error observed",
                    "color": "#f87171",
                },
                {
                    "name": RepositorySyncStatus.IN_SYNC.value,
                    "label": "In Sync",
                    "description": "The repository is syncing correctly",
                    "color": "#60a5fa",
                },
                {
                    "name": RepositorySyncStatus.SYNCING.value,
                    "label": "Syncing",
                    "description": "A sync job is currently running against the repository.",
                    "color": "#a855f7",
                },
            ],
            "optional": False,
            "branch": BranchSupportType.LOCAL.value,
            "default_value": RepositorySyncStatus.UNKNOWN.value,
            "order_weight": 6000,
        },
    ],
    "relationships": [
        {
            "name": "credential",
            "peer": InfrahubKind.CREDENTIAL,
            "identifier": "gitrepository__credential",
            "kind": "Attribute",
            "optional": True,
            "cardinality": "one",
            "order_weight": 4000,
        },
        {
            "name": "tags",
            "peer": InfrahubKind.TAG,
            "kind": "Attribute",
            "optional": True,
            "cardinality": "many",
            "order_weight": 8000,
        },
        {
            "name": "transformations",
            "peer": InfrahubKind.TRANSFORM,
            "identifier": "repository__transformation",
            "optional": True,
            "cardinality": "many",
            "order_weight": 10000,
        },
        {
            "name": "queries",
            "peer": InfrahubKind.GRAPHQLQUERY,
            "identifier": "graphql_query__repository",
            "optional": True,
            "cardinality": "many",
            "order_weight": 9000,
        },
        {
            "name": "checks",
            "peer": InfrahubKind.CHECKDEFINITION,
            "identifier": "check_definition__repository",
            "optional": True,
            "cardinality": "many",
            "order_weight": 11000,
        },
        {
            "name": "generators",
            "peer": InfrahubKind.GENERATORDEFINITION,
            "identifier": "generator_definition__repository",
            "optional": True,
            "cardinality": "many",
            "order_weight": 12000,
        },
    ],
}
