export type ActivityCategory =
  | "land"
  | "gate"
  | "deploy"
  | "run"
  | "agent"
  | "buildbox"
  | "guard"
  | "notification"
  | "git"
  | "service"
  | "ci";

export type ActivityActor = "agent" | "human" | "timer" | "system";

export type ActivitySeverity = "debug" | "info" | "notice" | "warn" | "error";

export const ACTIVITY_SEVERITY_ORDER: Record<ActivitySeverity, number> = {
  debug: 0,
  info: 1,
  notice: 2,
  warn: 3,
  error: 4,
} as const;

export const ALL_ACTIVITY_CATEGORIES: ActivityCategory[] = [
  "land",
  "gate",
  "deploy",
  "run",
  "agent",
  "buildbox",
  "guard",
  "notification",
  "git",
  "service",
  "ci",
]

export type ActivityLifecycle = "started" | "active" | "completed" | "failed" | "cancelled" | "unknown";
export type ActivityResult = "success" | "failure" | "cancelled" | "unknown";
export interface ActivityEvidence { sourceId: string; sourcePath: string; recordId: string; href: string }
export interface ActivityCorrelation { project?: string; host?: string; sessionId?: string; runId?: string; buildKey?: string; workloadUid?: string }

export interface ActivityEvent {
  id: string;
  ts: string;
  category: ActivityCategory;
  source: string;
  actor: ActivityActor;
  severity: ActivitySeverity;
  title: string;
  lifecycle?: ActivityLifecycle;
  result?: ActivityResult;
  evidence?: ActivityEvidence[];
  correlation?: ActivityCorrelation;
  project?: string;
  host?: string;
  runtime?: string;
  session?: string;
  account?: string;
  detail?: Record<string, unknown>;
  durationMs?: number;
  rc?: number;
  dedupeKey?: string;
}

export type ActivitySourceStatus = "ok" | "absent" | "error";

export interface ActivitySourceCoverage {
  id: string;
  label: string;
  category: ActivityCategory;
  path: string;
  authority?: "authoritative" | "derived";
  storage: string;
  retention?: string;
  queryBounds: string;
  coverageFrom?: string;
  status: ActivitySourceStatus;
  records: number;
  totalRecords: number;
  earliest?: string;
  latest?: string;
  skipped: number;
  error?: string;
}

export interface SuppressedNotificationRow {
  id: string;
  program: string;
  summary: string;
  summarySample?: string;
  bodySample?: string;
  channel?: string;
  count: number;
  first?: string;
  last?: string;
}

export interface SuppressedNotifications {
  status: "ok" | "absent" | "error";
  path: string;
  rows: SuppressedNotificationRow[];
  sourceCount: number;
  attemptCount: number;
  windowFrom?: string;
  windowTo?: string;
  skipped: number;
  error?: string;
}

export type ActivitySkipReason =
  | "unreadable-file"
  | "unparseable-json"
  | "not-an-object"
  | "unsupported-schema"
  | "missing-fields"
  | "invalid-timestamp";

export interface ActivitySkippedEntry {
  id: string;
  reason: ActivitySkipReason;
  explanation: string;
  path: string;
  line?: number;
  excerpt?: string;
}

export interface ActivitySourceEntriesResponse {
  source: ActivitySourceCoverage;
  events: ActivityEvent[];
  total: number;
  offset: number;
  limit: number;
  skipped: ActivitySkippedEntry[];
  skippedTotal: number;
  skippedTruncated: boolean;
}

export interface ActivitySourceEntriesQuery {
  sourceId: string
  from?: string        // ISO
  to?: string          // ISO
  severity?: ActivitySeverity
  q?: string
  recordId?: string
  offset?: number
  limit?: number
}

export interface ActivitySeries { id: "activeAgents" | "logVolume" | "commits" | "bots" | "builds" | "incidents"; label: string; status: "available" | "unavailable"; reason?: string; coverageFrom?: string; gaps?: Array<{ from: string; to: string; reason: string }>; points: Array<{ ts: string; value: number }> }

export interface ActivityReadResponse {
  events: ActivityEvent[];
  total: number;
  truncated: boolean;
  limit: number;
  categoryCounts: Array<{ category: ActivityCategory; count: number }>;
  projectCounts?: Array<{ project: string; count: number }>;
  sourceCounts: Array<{ source: string; count: number }>;
  errorCount?: number;
  series?: ActivitySeries[];
  coverage: ActivitySourceCoverage[];
  suppressedNotifications: SuppressedNotifications;
}

export interface ActivityQuery {
  from?: string        // ISO
  to?: string          // ISO
  category?: ActivityCategory
  project?: string
  severity?: ActivitySeverity
  limit?: number
  q?: string
  node?: string
  workload?: string
  build?: string
}
