from datetime import datetime
from typing import Optional
from pydantic import BaseModel, Field
import uuid

from autopil.models import Decision, SensitivityLevel


# ── API Key schemas ───────────────────────────────────────────────────────────

class CreateKeyRequest(BaseModel):
    name: str = Field(..., description="Human-readable label for this key")


class CreateKeyResponse(BaseModel):
    key_id:     str
    name:       str
    key:        str = Field(..., description="Plaintext key — shown once, store it now")
    created_at: str


class KeySummary(BaseModel):
    key_id:       str
    name:         str
    created_at:   str
    last_used:    Optional[str] = None
    is_active:    bool
    tenant_id:    str
    is_superadmin: bool = False


# ── Tenant schemas ────────────────────────────────────────────────────────────

class CreateTenantRequest(BaseModel):
    name: str = Field(..., description="Unique tenant name (e.g. company slug)")


class TenantResponse(BaseModel):
    tenant_id:  str
    name:       str
    created_at: str
    is_active:  bool


class CreateTenantResponse(BaseModel):
    tenant_id:  str
    name:       str
    created_at: str
    admin_key:  str = Field(..., description="Initial admin key for this tenant — shown once")


# ── Request schemas ──────────────────────────────────────────────────────────

class EvaluateRequest(BaseModel):
    query: str
    agent_role: str
    user_id: str
    source_id: str
    sensitivity_level: SensitivityLevel = SensitivityLevel.MEDIUM
    session_id: str = Field(default_factory=lambda: str(uuid.uuid4()))


# ── Response schemas ─────────────────────────────────────────────────────────

class EvaluateResponse(BaseModel):
    decision: Decision
    policy_name: str
    reason: str
    event_id: str
    session_id: str
    timestamp: datetime


class AuditEventResponse(BaseModel):
    event_id: str
    session_id: str
    agent_role: str
    user_id: str
    source_id: str
    query: str
    decision: Decision
    policy_name: str
    reason: str
    timestamp: datetime
    context_hash: Optional[str] = None


class AuditTrailResponse(BaseModel):
    session_id: str
    total_events: int
    allowed: int
    denied: int
    events: list[AuditEventResponse]


class PolicySummary(BaseModel):
    name: str
    agent_role: str
    allowed_sources: list[str]
    denied_sources: list[str]
    max_sensitivity: str


class HealthResponse(BaseModel):
    status: str
    version: str
    policy_file: str
    audit_db: str


class CreatePolicyRequest(BaseModel):
    name: str = Field(..., description="Unique policy name")
    agent_role: str = Field(..., description="Agent role this policy applies to")
    allowed_sources: list[str] = Field(default=[], description="Allowed data sources (empty = all non-denied)")
    denied_sources: list[str] = Field(default=[], description="Always-blocked data sources")
    max_sensitivity: str = Field(default="high", description="Sensitivity ceiling: low | medium | high | restricted")


class UpdatePolicyRequest(BaseModel):
    agent_role: str
    allowed_sources: list[str] = []
    denied_sources: list[str] = []
    max_sensitivity: str = "high"


class PolicyDetailResponse(BaseModel):
    policy_id: str
    name: str
    agent_role: str
    allowed_sources: list[str]
    denied_sources: list[str]
    max_sensitivity: str
    version: int
    created_at: str
    updated_at: str
    changed_by: Optional[str] = None


class PolicyVersionResponse(BaseModel):
    version_id: str
    version: int
    agent_role: str
    allowed_sources: list[str]
    denied_sources: list[str]
    max_sensitivity: str
    changed_by: Optional[str] = None
    changed_at: str
