"""integrations domain — Pydantic schemas."""
from __future__ import annotations

from datetime import datetime
from typing import Any, Dict, List, Optional
from uuid import UUID

from pydantic import BaseModel, Field

from src.domain.integrations.providers import supported_providers


class InstallIntegrationRequest(BaseModel):
    """Install or update a provider integration.

    config: provider-specific non-secret fields (publishable keys, account IDs, etc.)
    secrets: provider-specific secret fields — stored encrypted, never returned
    events: which webhook events to subscribe to (provider-specific subset)
    """

    config: Optional[Dict[str, Any]] = Field(
        None, description="Non-secret config (publishable key, account ID, etc.)"
    )
    secrets: Optional[Dict[str, str]] = Field(
        None, description="Secret values (api_key, secret_key, etc.) — stored encrypted"
    )
    events: Optional[List[str]] = Field(
        None, description="Webhook events to subscribe (null = provider defaults)"
    )
    function_slug: Optional[str] = Field(
        None, description="Edge function to route webhook events to"
    )


class UpdateIntegrationSecretsRequest(BaseModel):
    """Rotate one or more secrets for an installed integration."""

    secrets: Dict[str, str] = Field(..., description="Secret names and their new plain-text values")


class IntegrationResponse(BaseModel):
    id: UUID
    project_id: UUID
    provider: str
    status: str
    config: Optional[Dict[str, Any]] = None
    # Which secret keys are set (never the values)
    secrets_configured: List[str] = Field(
        default_factory=list,
        description="Names of secrets that are currently set",
    )
    webhook_auto_registered: bool
    installed_at: datetime
    updated_at: datetime

    model_config = {"from_attributes": True}


class ProviderInfoResponse(BaseModel):
    """Static information about a supported provider."""

    name: str
    display_name: str
    config_fields: List[str]
    secret_fields: List[str]
    supports_webhooks: bool
    webhook_events: Optional[List[str]] = None
