export interface ConnectionInfo {
  provider: string;
  provider_username: string | null;
  provider_avatar: string | null;
}

export interface UserInfo {
  id: string; // UUID
  username: string;
  first_name: string | null;
  last_name: string | null;
  email: string | null;
  /** True when the account has a local password (can log in without OAuth). */
  has_password: boolean;
  connections: ConnectionInfo[];
  /** Site-level role: "admin" | "user" */
  role: "admin" | "user";
  /** Account status: "active" | "banned" */
  status: "active" | "banned";
}

/** User record returned by the admin user list endpoint. */
export interface AdminUser {
  id: string; // UUID
  username: string;
  email: string | null;
  role: "admin" | "user";
  status: "active" | "banned";
  created_at: string; // ISO datetime
}

export type MapRole = "owner" | "editor" | "viewer";

/** Map visibility / editability level. */
export type PublicAccess = "private" | "public_view" | "public_edit";

export interface MapPermission {
  map_id: string;
  user_id: string; // UUID
  username: string;
  role: MapRole;
}

/** Core map record (returned by create, get, update). */
export interface GridMap {
  id: string;
  name: string;
  public_access: PublicAccess;
  owner_id: string; // UUID
  colors: string[];
  created_at: Date;
  updated_at: Date;
}

/**
 * Extended map record returned by the list endpoint.
 * Includes owner username, the caller's role, and a favorite flag.
 */
export interface ListedMap extends GridMap {
  owner_username: string;
  /** Null when the map is in the list only via a favorite (no explicit permission). */
  user_role: MapRole | null;
  is_favorited: boolean;
}

export interface GridCell {
  map_id: string;
  x: number;
  y: number;
  color: string;
}

export interface GridToken {
  id: string;
  map_id: string;
  x: number;
  y: number;
  label: string;
  color: string;
}

export interface MapState {
  map: GridMap;
  cells: GridCell[];
  tokens: GridToken[];
}

export interface MapAccessRequest {
  id: string; // UUID
  map_id: string;
  user_id: string; // UUID
  username: string;
  requested_role: "editor" | "viewer";
  status: "pending" | "approved" | "denied";
  created_at: string;
  updated_at: string;
}

// ── Discord / Audio ──────────────────────────────────────────────────────────

export interface DiscordGuild {
  id: string;
  name: string;
}

export interface TrackInfo {
  title: string;
  url: string;
}

export interface AudioStatus {
  voice_channel: string | null;
  is_paused: boolean;
  current_track: TrackInfo | null;
  queue: TrackInfo[];
}

export type Tool = "pan" | "zoom" | "draw" | "token";

export type ClientMessage =
  | { type: "paint_cell"; x: number; y: number; color: string }
  | {
      type: "paint_cells";
      cells: Array<{ x: number; y: number; color: string }>;
    }
  | { type: "erase_cell"; x: number; y: number }
  | { type: "add_token"; x: number; y: number; label: string; color: string }
  | { type: "move_token"; id: string; x: number; y: number }
  | { type: "delete_token"; id: string }
  | { type: "update_colors"; colors: string[] };

export type ServerMessage =
  | { type: "state"; cells: GridCell[]; tokens: GridToken[]; colors: string[] }
  | { type: "cell_painted"; x: number; y: number; color: string }
  | {
      type: "cells_batch_painted";
      cells: Array<{ x: number; y: number; color: string }>;
    }
  | { type: "cell_erased"; x: number; y: number }
  | {
      type: "token_added";
      id: string;
      x: number;
      y: number;
      label: string;
      color: string;
    }
  | { type: "token_moved"; id: string; x: number; y: number }
  | { type: "token_deleted"; id: string }
  | { type: "colors_updated"; colors: string[] }
  | { type: "error"; message: string };
