export type ColumnLocation = "board" | "sidebar";

export interface Column {
  id: string;
  name: string;
  position: number;
  location: ColumnLocation;
  width: number;
  wip_limit: number;
  is_done: boolean;
  max_time_minutes: number;
  created_at: string;
}

// "" | mantine color name | "#rrggbb"
export type CardColor = string;

export interface Sticker {
  emoji: string;
  x: number;
  y: number;
}

export interface Card {
  id: string;
  seq_num: number;
  requester: string;
  title: string;
  description: string;
  color: CardColor;
  column_id: string;
  position: number;
  locked: boolean;
  assignee_id: string | null;
  completed_at: string | null;
  deleted_at: string | null;
  archived_at: string | null;
  tags: string[];
  stickers: Sticker[];
  deadline: string | null;
  created_at: string;
  updated_at: string;
  entered_at: string;
  time_in_column_ms: number;
  locked_at: string | null;
  total_locked_ms: number;
}

export interface CardFile {
  id: string;
  card_id: string;
  uploader_id: string;
  filename: string;
  mime: string;
  size: number;
  source: "upload" | "description" | "chat";
  url: string;
  created_at: string;
}

export interface User {
  id: string;
  username: string;
  display_name: string;
  color: string;
  is_admin?: boolean;
  created_at: string;
}

export type ModuleKind = "jira" | "webhook";

export interface KanbanModule {
  id: string;
  name: string;
  kind: ModuleKind | string;
  enabled: boolean;
  event_filter: string[];
  config: Record<string, unknown>;
  created_at: string;
  updated_at: string;
}

export interface ModuleLog {
  id: string;
  module_id: string;
  event_type: string;
  card_id: string;
  status: number;
  duration_ms: number;
  error: string;
  created_at: string;
}

export interface ModuleTestResult {
  ok: boolean;
  status: number;
  duration_ms: number;
  error?: string;
}

export interface MetricsRange {
  from: string;
  to: string;
}

export interface MetricsTotals {
  cards: number;
  cards_completed_in_range: number;
  cards_created_in_range: number;
  cards_active: number;
  cards_done: number;
  columns: number;
  users: number;
  active_locks: number;
}

export interface MetricsColumnCount {
  column_id: string;
  name: string;
  is_done: boolean;
  count: number;
}

export interface MetricsDailyCount {
  date: string;
  count: number;
}

export interface MetricsDurationStats {
  n: number;
  avg_ms: number;
  p50_ms: number;
  p90_ms: number;
  p99_ms: number;
}

export interface MetricsColumnDuration {
  column_id: string;
  name: string;
  is_done: boolean;
  stats: MetricsDurationStats;
}

export interface MetricsAssignee {
  user_id: string;
  username: string;
  display_name: string;
  active: number;
  completed_in_range: number;
}

export interface MetricsRequester {
  requester: string;
  total: number;
  active: number;
  completed_in_range: number;
}

export interface MetricsMovement {
  user_id: string;
  username: string;
  display_name: string;
  moves: number;
}

export interface MetricsCumulativePoint {
  date: string;
  total: number;
  done: number;
}

export interface Metrics {
  range: MetricsRange;
  totals: MetricsTotals;
  by_column: MetricsColumnCount[];
  throughput_daily: MetricsDailyCount[];
  created_daily: MetricsDailyCount[];
  lead_time: MetricsDurationStats;
  cycle_time_per_column: MetricsColumnDuration[];
  top_assignees: MetricsAssignee[];
  top_requesters: MetricsRequester[];
  movements_by_user: MetricsMovement[];
  lock_total_ms: number;
  lock_active_count: number;
  cumulative_flow: MetricsCumulativePoint[];
}

export interface MetricsFilter {
  from?: string;
  to?: string;
  assignee_id?: string;
  requester?: string;
  tags?: string[];
}

export interface Board {
  columns: Column[];
  cards: Card[];
}

export interface HistoryEntry {
  id: string;
  card_id: string;
  column_id: string;
  column_name: string;
  entered_at: string;
  exited_at: string | null;
  duration_ms: number;
  actor_id: string | null;
}

export interface LockPeriod {
  id: string;
  card_id: string;
  locked_at: string;
  unlocked_at: string | null;
  duration_ms: number;
  actor_id: string | null;
}

export interface CardEvent {
  id: string;
  card_id: string;
  kind: string;
  actor_id: string | null;
  payload: string;
  created_at: string;
}

export interface CardHistoryResponse {
  column_history: HistoryEntry[];
  lock_periods: LockPeriod[];
  events: CardEvent[];
  total_locked_ms: number;
  currently_locked: boolean;
}

export interface CardMessage {
  id: string;
  card_id: string;
  author_id: string | null;
  body: string;
  created_at: string;
}

export type NotificationKind = "mention" | "assigned_chat" | "reply";

export interface Notification {
  id: string;
  user_id: string;
  card_id: string;
  message_id: string;
  kind: NotificationKind;
  actor_id: string;
  created_at: string;
  read_at: string | null;
  card_title: string;
  card_seq_num: number;
  actor_name: string;
  snippet: string;
}
