# Copyright (c) CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com
#

"""
Auto-link API V4 Schemas for CLI

This module provides Pydantic models for auto-link operations using the v4 API
with clean, modern data contracts and proper validation.
"""

from datetime import datetime

from pydantic import BaseModel, Field, field_validator


class AutoLinkConfiguration(BaseModel):
    """Auto-link configuration for a connection."""

    role_name: str = Field(..., description="IAM role name for auto-link")
    enabled: bool = Field(default=True, description="Whether auto-link is enabled")
    last_run: datetime | None = Field(None, description="Last auto-link run timestamp")
    next_run: datetime | None = Field(None, description="Next scheduled auto-link run")

    # Link tracking
    linked_accounts: list[str] = Field(default_factory=list, description="Successfully linked account IDs")
    failed_accounts: list[str] = Field(default_factory=list, description="Account IDs that failed to link")
    ignored_accounts: list[str] = Field(default_factory=list, description="Account IDs to ignore")

    # Statistics
    total_discovered: int = Field(default=0, description="Total accounts discovered")
    total_linked: int = Field(default=0, description="Total accounts successfully linked")
    total_failed: int = Field(default=0, description="Total accounts that failed to link")

    # Metadata
    created_at: datetime | None = Field(None, description="Configuration creation timestamp")
    updated_at: datetime | None = Field(None, description="Last configuration update timestamp")

    class Config:
        extra = "allow"  # Allow additional fields from backend


class ConnectionAutoLinkStatus(BaseModel):
    """Auto-link status for a specific connection."""

    connection_id: str = Field(..., description="Connection identifier")
    cz_organization_id: str = Field(..., description="Organization identifier")
    connection_name: str | None = Field(None, description="Connection display name")
    provider: str = Field(..., description="Cloud provider (AWS, GCP, AZURE, SNOWFLAKE, etc.)")
    account_type: str | None = Field(None, description="Account type (payer, resource, etc.)")

    # Auto-link configuration
    auto_link: AutoLinkConfiguration | None = Field(None, description="Auto-link configuration")
    auto_link_enabled: bool = Field(default=False, description="Whether auto-link is enabled")

    # Health and status
    is_healthy: bool = Field(default=True, description="Whether the connection is healthy")
    last_checked: datetime | None = Field(None, description="Last health check timestamp")

    class Config:
        extra = "allow"  # Allow additional fields from backend


class OrganizationAutoLinkStatus(BaseModel):
    """Auto-link status for an organization."""

    cz_organization_id: str = Field(..., description="Organization identifier")
    cz_organization_name: str | None = Field(None, description="Organization display name")

    # Connection summaries
    total_connections: int = Field(default=0, description="Total connections in organization")
    auto_link_enabled_connections: dict[str, ConnectionAutoLinkStatus] = Field(default_factory=dict, description="Connections with auto-link enabled")
    auto_link_disabled_connections: dict[str, ConnectionAutoLinkStatus] = Field(
        default_factory=dict, description="Connections with auto-link disabled"
    )

    # Statistics
    total_auto_link_enabled: int = Field(default=0, description="Total connections with auto-link enabled")
    total_linked_accounts: int = Field(default=0, description="Total linked accounts across all connections")
    total_failed_accounts: int = Field(default=0, description="Total failed accounts across all connections")
    total_ignored_accounts: int = Field(default=0, description="Total ignored accounts across all connections")

    # Timestamps
    last_updated: datetime | None = Field(None, description="Last status update timestamp")

    class Config:
        extra = "allow"  # Allow additional fields from backend


class EnableAutoLinkRequest(BaseModel):
    """Request model for enabling auto-link."""

    role_name: str = Field(..., min_length=1, max_length=255, description="IAM role name for auto-link")
    force: bool = Field(default=False, description="Force enable even if already enabled")

    @field_validator("role_name")
    @classmethod
    def validate_role_name(cls, v):
        if not v or not v.strip():
            raise ValueError("Role name cannot be empty")
        return v.strip()


class DisableAutoLinkRequest(BaseModel):
    """Request model for disabling auto-link."""

    force: bool = Field(default=False, description="Force disable even if already disabled")


class UpdateIgnoreListRequest(BaseModel):
    """Request model for updating auto-link ignore list."""

    ignore_list: list[str] = Field(..., min_length=1, description="Account IDs to ignore")
    action: str = Field(default="add", description="Action to perform: 'add', 'remove', or 'replace'")

    @field_validator("ignore_list")
    @classmethod
    def validate_ignore_list(cls, v):
        if not v:
            raise ValueError("Ignore list cannot be empty")
        # Remove duplicates and empty strings
        cleaned = [item.strip() for item in v if item and item.strip()]
        if not cleaned:
            raise ValueError("Ignore list must contain at least one valid account ID")
        return cleaned

    @field_validator("action")
    @classmethod
    def validate_action(cls, v):
        if v not in ["add", "remove", "replace"]:
            raise ValueError('Action must be "add", "remove", or "replace"')
        return v
