from __future__ import annotations

from typing import Any

from pydantic import BaseModel


class StatusResponse(BaseModel):
    cogent_name: str
    active_sessions: int = 0
    total_conversations: int = 0
    trigger_count: int = 0
    unresolved_alerts: int = 0


class Execution(BaseModel):
    id: str
    program_name: str
    conversation_id: str | None = None
    status: str | None = None
    started_at: str | None = None
    completed_at: str | None = None
    duration_ms: int | None = None
    tokens_input: int | None = None
    tokens_output: int | None = None
    cost_usd: float = 0
    error: str | None = None


class Program(BaseModel):
    name: str
    description: str = ""
    trigger_count: int = 0
    runs: int = 0
    ok: int = 0
    fail: int = 0
    total_cost: float = 0
    last_run: str | None = None


class ProgramsResponse(BaseModel):
    cogent_name: str
    count: int
    programs: list[Program]


class ExecutionsResponse(BaseModel):
    cogent_name: str
    count: int
    executions: list[Execution]


class Session(BaseModel):
    id: str
    context_key: str | None = None
    status: str | None = None
    cli_session_id: str | None = None
    started_at: str | None = None
    last_active: str | None = None
    metadata: dict[str, Any] | None = None
    runs: int = 0
    ok: int = 0
    fail: int = 0
    tokens_in: int = 0
    tokens_out: int = 0
    total_cost: float = 0


class SessionsResponse(BaseModel):
    cogent_name: str
    count: int
    sessions: list[Session]


class Trigger(BaseModel):
    id: str
    name: str = ""
    channel_pattern: str | None = None
    program_name: str | None = None
    priority: int | None = None
    enabled: bool = True
    created_at: str | None = None
    fired_1m: int = 0
    fired_5m: int = 0
    fired_1h: int = 0
    fired_24h: int = 0
    max_events: int = 0
    throttle_window_seconds: int = 60
    throttle_rejected: int = 0
    throttle_active: bool = False


class TriggersResponse(BaseModel):
    cogent_name: str
    count: int
    triggers: list[Trigger]


class TriggerCreate(BaseModel):
    program_name: str
    channel_pattern: str
    priority: int = 10
    enabled: bool = True
    metadata: dict[str, Any] = {}
    max_events: int = 0
    throttle_window_seconds: int = 60


class TriggerUpdate(BaseModel):
    program_name: str | None = None
    channel_pattern: str | None = None
    priority: int | None = None
    max_events: int | None = None
    throttle_window_seconds: int | None = None


class ToggleRequest(BaseModel):
    ids: list[str]
    enabled: bool


class ToggleResponse(BaseModel):
    updated: int
    enabled: bool


class MemoryVersionItem(BaseModel):
    id: str
    version: int
    content: str = ""
    source: str = "cogent"
    read_only: bool = False
    created_at: str | None = None


class MemoryItem(BaseModel):
    id: str
    name: str = ""
    group: str = ""
    active_version: int = 1
    includes: list[str] = []
    content: str = ""
    source: str = "cogent"
    read_only: bool = False
    created_at: str | None = None
    modified_at: str | None = None
    versions: list[MemoryVersionItem] = []


class MemoryCreate(BaseModel):
    name: str
    content: str = ""
    source: str = "cogent"
    read_only: bool = False


class MemoryUpdate(BaseModel):
    content: str | None = None
    source: str | None = None
    read_only: bool | None = None


class MemoryResponse(BaseModel):
    cogent_name: str
    count: int
    memory: list[MemoryItem]


class Task(BaseModel):
    id: str
    name: str | None = None
    description: str | None = None
    program_name: str | None = None
    content: str | None = None
    status: str | None = None
    priority: float | None = None
    runner: str | None = None
    clear_context: bool | None = None
    recurrent: bool | None = None
    memory_keys: list[str] | None = None
    tools: list[str] | None = None
    resources: list[str] | None = None
    creator: str | None = None
    parent_task_id: str | None = None
    source_event: str | None = None
    limits: dict[str, Any] | None = None
    metadata: dict[str, Any] | None = None
    created_at: str | None = None
    updated_at: str | None = None
    completed_at: str | None = None
    last_run_status: str | None = None
    last_run_error: str | None = None
    last_run_at: str | None = None
    run_counts: dict[str, dict[str, int]] | None = None


class TaskCreate(BaseModel):
    name: str
    description: str = ""
    content: str = ""
    program_name: str = "vsm/s1/do-content"
    status: str = "runnable"
    priority: float = 0.0
    runner: str | None = None
    clear_context: bool = False
    recurrent: bool = False
    memory_keys: list[str] | None = None
    tools: list[str] | None = None
    resources: list[str] | None = None
    creator: str = "dashboard"
    source_event: str | None = None
    limits: dict[str, Any] | None = None
    metadata: dict[str, Any] | None = None


class TaskUpdate(BaseModel):
    name: str | None = None
    description: str | None = None
    content: str | None = None
    program_name: str | None = None
    status: str | None = None
    priority: float | None = None
    runner: str | None = None
    clear_context: bool | None = None
    recurrent: bool | None = None
    memory_keys: list[str] | None = None
    tools: list[str] | None = None
    resources: list[str] | None = None
    creator: str | None = None
    source_event: str | None = None
    limits: dict[str, Any] | None = None
    metadata: dict[str, Any] | None = None


class TasksResponse(BaseModel):
    cogent_name: str
    count: int
    tasks: list[Task]


class Alert(BaseModel):
    id: str
    severity: str | None = None
    alert_type: str | None = None
    source: str | None = None
    message: str | None = None
    metadata: dict[str, Any] | None = None
    resolved_at: str | None = None
    created_at: str | None = None


class AlertCreate(BaseModel):
    severity: str = "warning"
    alert_type: str = ""
    source: str = ""
    message: str
    metadata: dict[str, Any] = {}


class AlertsResponse(BaseModel):
    cogent_name: str
    count: int
    alerts: list[Alert]


class CronItem(BaseModel):
    id: str
    cron_expression: str
    channel_name: str
    enabled: bool = True
    metadata: dict[str, Any] = {}
    created_at: str | None = None


class CronCreate(BaseModel):
    cron_expression: str
    channel_name: str
    enabled: bool = True
    metadata: dict[str, Any] = {}


class CronUpdate(BaseModel):
    cron_expression: str | None = None
    channel_name: str | None = None
    enabled: bool | None = None
    metadata: dict[str, Any] | None = None


class CronsResponse(BaseModel):
    cogent_name: str
    count: int
    crons: list[CronItem]


class ResourceItem(BaseModel):
    name: str
    resource_type: str = "pool"
    capacity: float = 1.0
    used: float = 0.0
    metadata: dict[str, Any] = {}
    created_at: str | None = None


class ResourcesResponse(BaseModel):
    cogent_name: str
    count: int = 0
    resources: list[ResourceItem] = []


class ToolItem(BaseModel):
    id: str
    name: str
    description: str = ""
    instructions: str = ""
    input_schema: dict[str, Any] = {}
    handler: str = ""
    iam_role_arn: str | None = None
    enabled: bool = True
    metadata: dict[str, Any] = {}
    created_at: str | None = None
    updated_at: str | None = None


class ToolsResponse(BaseModel):
    cogent_name: str
    count: int
    tools: list[ToolItem]


class ToolUpdate(BaseModel):
    description: str | None = None
    instructions: str | None = None
    input_schema: dict[str, Any] | None = None
    enabled: bool | None = None
    metadata: dict[str, Any] | None = None
