export type TopError = {
  ts?: string;
  event: string;
  message?: string | null;
  source: string;
  user_id?: string | null;
  run_id?: string | null;
};

export type AdminAccessResponse = {
  id: string;
  username: string;
  role: "ADMIN" | "SUPER_ADMIN";
  can_manage_admins: boolean;
};

export type OverviewResponse = {
  total_users: number;
  users_logged_in_last_24h: number;
  total_runs: number;
  running_runs: number;
  stopped_runs: number;
  error_runs: number;
  live_runs_count: number;
  paper_runs_count: number;
  orders_last_24h: number;
  trades_last_24h: number;
  sip_executed_last_24h: number;
  unresolved_orders: number;
  blocked_strategies: number;
  open_support_tickets: number;
  top_errors: TopError[];
};

export type UserSummary = {
  user_id: string;
  username: string;
  role: "USER" | "ADMIN" | "SUPER_ADMIN";
  is_admin: boolean;
  created_at?: string | null;
  last_login_at?: string | null;
  active_run_id?: string | null;
  active_run_status?: string | null;
  runs_count: number;
  broker_connected: boolean;
};

export type UsersResponse = {
  page: number;
  page_size: number;
  total: number;
  users: UserSummary[];
};

export type RunSummary = {
  run_id: string;
  user_id: string;
  status: string;
  created_at?: string | null;
  started_at?: string | null;
  stopped_at?: string | null;
  strategy?: string | null;
  mode?: string | null;
  broker?: string | null;
  sip_amount?: number | null;
  sip_frequency_value?: number | null;
  sip_frequency_unit?: string | null;
  last_event_time?: string | null;
  last_sip_time?: string | null;
  next_sip_time?: string | null;
  order_count: number;
  trade_count: number;
  equity_latest?: number | null;
  pnl_latest?: number | null;
};

export type RunsResponse = {
  page: number;
  page_size: number;
  total: number;
  runs: RunSummary[];
};

export type UserDetailResponse = {
  user: UserSummary;
  runs: RunSummary[];
  current_config?: Record<string, unknown> | null;
  events: Array<{
    ts?: string | null;
    source: string;
    event: string;
    message?: string | null;
    level?: string | null;
    run_id?: string | null;
    meta?: Record<string, unknown> | null;
  }>;
  capital_summary: {
    cash?: number | null;
    invested?: number | null;
    mtm?: number | null;
    equity?: number | null;
    pnl?: number | null;
  };
};

export type RunDetailResponse = {
  run: RunSummary;
  config?: Record<string, unknown> | null;
  engine_status?: { status?: string | null; last_updated?: string | null } | null;
  state_snapshot?: Record<string, unknown> | null;
  ledger_events: Array<Record<string, unknown>>;
  orders: Array<Record<string, unknown>>;
  trades: Array<Record<string, unknown>>;
  invariants: Record<string, number>;
};

export type InvariantsResponse = {
  running_runs_per_user_violations: number;
  orphan_rows: number;
  duplicate_logical_time: number;
  negative_cash: number;
  invalid_qty: number;
  stale_running_runs: number;
};

export type DeleteUserResponse = {
  user_id: string;
  deleted: Record<string, number>;
  audit_id: number;
};

export type HardResetResponse = {
  user_id: string;
  deleted: Record<string, number>;
  audit_id: number;
};

export type SupportTicketSummary = {
  ticket_id: string;
  name: string;
  email: string;
  subject: string;
  message: string;
  status: string;
  created_at?: string | null;
  updated_at?: string | null;
};

export type SupportTicketsResponse = {
  page: number;
  page_size: number;
  total: number;
  tickets: SupportTicketSummary[];
};
