export type AccessEntry = {
  key_id: string;
  last_accessed_at: string;
  last_path: string;
  remote_addr: string;
  user_agent: string;
  request_count: number;
};

export type RequestAggregate = {
  key: string;
  request_count: number;
};

export type AccessMetrics = {
  total_requests: number;
  unique_principals: number;
  unique_ips: number;
  unique_agents: number;
  unique_tools: number;
  top_ips: RequestAggregate[];
  top_agents: RequestAggregate[];
  top_tools: RequestAggregate[];
};

export type StatusResponse = {
  title: string;
  description: string;
  version: string;
  build_date: string;
  commit: string;
  connected_count: number;
  total_known: number;
  connected_window: string;
  oauth_enabled: boolean;
  entries: AccessEntry[];
  metrics: AccessMetrics;
};

export type PublicStatusClient = {
  key_id: string;
  request_count: number;
  last_accessed_at: string;
};

export type PublicStatusResponse = {
  connected_count: number;
  connected_window: string;
  entries: PublicStatusClient[];
};

export type NavItem = {
  id: string;
  label: string;
  description: string;
  disabled?: boolean;
};

export type ShellPage = 'dashboard' | 'projects' | 'thoughts' | 'learnings' | 'plans' | 'skills' | 'guardrails' | 'files' | 'maintenance';

export type Project = {
  id: string;
  name: string;
  description: string;
  created_at: string;
  last_active_at: string;
};

export type ProjectSummary = Project & {
  thought_count: number;
};

export type ThoughtMetadata = {
  people: string[];
  action_items: string[];
  dates_mentioned: string[];
  topics: string[];
  type: string;
  source: string;
  metadata_status: string;
  metadata_error?: string;
  attachments?: ThoughtAttachment[];
};

export type ThoughtAttachment = {
  file_id: string;
  name: string;
  media_type: string;
  kind?: string;
  size_bytes: number;
  sha256?: string;
};

export type Thought = {
  id: string;
  guid?: string;
  content: string;
  metadata: ThoughtMetadata;
  project_id?: string;
  archived_at?: string;
  created_at: string;
  updated_at: string;
};

export type ThoughtLink = {
  id: number;
  from_id: number;
  to_id: number;
  relation: string;
  created_at: string;
};

export type SearchResult = {
  id: string;
  content: string;
  metadata: ThoughtMetadata;
  similarity: number;
  created_at: string;
};

export type AgentSkill = {
  id: string;
  name: string;
  description: string;
  content: string;
  tags: string[];
  created_at: string;
  updated_at: string;
};

export type AgentGuardrail = {
  id: string;
  name: string;
  description: string;
  content: string;
  severity: 'low' | 'medium' | 'high' | 'critical';
  tags: string[];
  created_at: string;
  updated_at: string;
};

export type StoredFile = {
  id: string;
  thought_id?: string;
  project_id?: string;
  name: string;
  media_type: string;
  kind: string;
  size_bytes: number;
  sha256: string;
  created_at: string;
  updated_at: string;
};

export type ThoughtStats = {
  total_count: number;
  type_counts: Record<string, number>;
  top_topics: { key: string; count: number }[];
  top_people: { key: string; count: number }[];
};

export type Learning = {
  id: string;
  summary: string;
  details: string;
  category: string;
  area: string;
  status: string;
  priority: string;
  confidence: string;
  action_required: boolean;
  source_type?: string;
  source_ref?: string;
  tags?: string;
  project_id?: string;
  related_thought_id?: string;
  related_skill_id?: string;
  reviewed_at?: string;
  reviewed_by?: string;
  created_at: string;
  updated_at: string;
};

export type Plan = {
  id: string;
  title: string;
  description: string;
  status: 'draft' | 'active' | 'blocked' | 'completed' | 'cancelled' | 'superseded';
  priority: 'low' | 'medium' | 'high' | 'critical';
  project_id?: string;
  owner?: string;
  due_date?: string;
  completed_at?: string;
  reviewed_by?: string;
  last_reviewed_at?: string;
  supersedes_plan_id?: string;
  tags: string[];
  created_at: string;
  updated_at: string;
};

export type MaintenanceTask = {
  id: string;
  name: string;
  category?: string;
  priority: string;
  frequency_days?: number;
  next_due?: string;
  last_completed?: string;
  notes?: string;
  created_at: string;
  updated_at: string;
};

export type MaintenanceLog = {
  id: string;
  task_id: string;
  completed_at: string;
  performed_by?: string;
  cost?: number;
  notes?: string;
  next_action?: string;
};

export type BackfillResult = {
  model: string;
  scanned: number;
  embedded: number;
  skipped: number;
  failed: number;
  dry_run: boolean;
};

export type MetadataRetryResult = {
  scanned: number;
  retried: number;
  updated: number;
  skipped: number;
  failed: number;
  dry_run: boolean;
};
