# generated by fastapi-codegen:
#   filename:  /home/circleci/project/guardicore/connector
#   timestamp: 2025-08-23T16:14:27+00:00

from __future__ import annotations

from enum import Enum
from typing import Any, Dict, List, Optional
from uuid import UUID

from centra_sdk.gc_serializer import GcSerializer
from pydantic import AwareDatetime, BaseModel, ConfigDict, Field, RootModel

from ..common.common import Connector


class ComponentDetails(BaseModel, GcSerializer):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    component_version: Optional[str] = Field(None, alias='component-version')
    public_ip_addresses: Optional[List[str]] = Field(None, alias='public-ip-addresses')
    private_ip_addresses: Optional[List[str]] = Field(
        None, alias='private-ip-addresses'
    )
    hostname: Optional[str] = None
    dc_inventory_revision: int = Field(..., alias='dc-inventory-revision')
    policy_revision: int = Field(..., alias='policy-revision')
    config_revision: Optional[int] = Field(None, alias='config-revision')


class Status(Enum):
    UP = 'up'
    INITIALIZING = 'initializing'
    CONNECTING = 'connecting'
    STOPPED = 'stopped'
    DOWN = 'down'
    PARTIALLY_UP = 'partially_up'
    ERROR = 'error'
    MISSING = 'missing'
    VERIFYING = 'verifying'
    VERIFICATION_FAILED = 'verification_failed'


class Metric(BaseModel, GcSerializer):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    name: str = Field(..., description='the metric that will be added to telegraf')
    value: float
    tag_name: Optional[str] = None
    tag_value: Optional[str] = None


class FlagTypes(Enum):
    CLOUD_APP_API_AUTHENTICATION_FAILED = 'CloudAppAPIAuthenticationFailed'
    CLOUD_APP_NON_PROTECTED_RESOURCE_DISCOVERED = (
        'CloudAppNonProtectedResourceDiscovered'
    )
    CLOUD_APP_FLOWLOG_DISABLED_NSG_DISCOVERED = 'CloudAppFlowlogDisabledNSGDiscovered'


class FlagSeverity(Enum):
    ERROR = 'ERROR'
    WARNING = 'WARNING'
    INFORMATION = 'INFORMATION'


class ComponentFlags(BaseModel, GcSerializer):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    flag: FlagTypes
    raised_time: AwareDatetime
    severity: FlagSeverity
    description: str
    details: Optional[Dict[str, Any]] = None


class Flags(RootModel[List[ComponentFlags]]):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    root: List[ComponentFlags]

    def __getattr__(self, attr: str):
        return getattr(self.root, attr)

    def __iter__(self):
        for item in self.root:
            yield item


class Severity(Enum):
    INFO = 'info'
    WARNING = 'warning'
    ERROR = 'error'


class SystemEvent(BaseModel, GcSerializer):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    source: Connector
    source_id: UUID = Field(..., alias='source-id')
    timestamp: AwareDatetime
    category: Optional[str] = Field(None, examples=['deployment'])
    severity: Severity
    title: str
    message: str
    additional_info: Optional[Dict[str, str]] = Field(
        None, examples=[{'region-code': 'eastus', 'region-name': 'East US'}]
    )


class V1OperationsFlagsGetResponse(RootModel[List[ComponentFlags]]):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    root: List[ComponentFlags]

    def __getattr__(self, attr: str):
        return getattr(self.root, attr)

    def __iter__(self):
        for item in self.root:
            yield item


class ComponentMetrics(BaseModel, GcSerializer):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    component_id: UUID = Field(..., alias='component-id')
    component_type: str = Field(..., alias='component-type')
    component_metrics: List[Metric] = Field(..., alias='component-metrics')


class ServiceStatus(BaseModel, GcSerializer):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    service_status: Status = Field(..., alias='service-status')
    msg: Optional[str] = None


class V1OperationsMetricsGetResponse(RootModel[List[ComponentMetrics]]):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    root: List[ComponentMetrics] = Field(..., min_length=1)

    def __getattr__(self, attr: str):
        return getattr(self.root, attr)

    def __iter__(self):
        for item in self.root:
            yield item


class ApplicationStatus(BaseModel, GcSerializer):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    application_status: Status = Field(..., alias='application-status')
    services_status: Optional[Dict[str, ServiceStatus]] = Field(
        None, alias='services-status'
    )


class ComponentStatus(BaseModel, GcSerializer):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    overall_status: Status = Field(..., alias='overall-status')
    applications_status: Optional[Dict[str, ApplicationStatus]] = Field(
        None, alias='applications-status'
    )


class ComponentHealth(BaseModel, GcSerializer):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    component_id: UUID = Field(..., alias='component-id')
    component_type: Connector = Field(..., alias='component-type')
    component_details: ComponentDetails = Field(..., alias='component-details')
    status: ComponentStatus
    statistics: Optional[Dict[str, Any]] = None


class V1OperationsHealthGetResponse(RootModel[List[ComponentHealth]]):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    root: List[ComponentHealth] = Field(..., min_length=1)

    def __getattr__(self, attr: str):
        return getattr(self.root, attr)

    def __iter__(self):
        for item in self.root:
            yield item
