/**
 * Monday.com API Types
 * Complete type definitions for Monday.com GraphQL API
 */

// ============================================================================
// Board Types
// ============================================================================

export type BoardKind = "public" | "private" | "share";

export interface Board {
  id: string;
  name: string;
  description?: string;
  board_kind: BoardKind;
  state: "active" | "archived" | "deleted";
  workspace_id?: string;
  folder_id?: string;
  owner_id: string;
  permissions: string;
  created_at?: string;
  updated_at?: string;
  columns?: Column[];
  groups?: Group[];
  items?: Item[];
  items_count?: number;
  tags?: Tag[];
  views?: BoardView[];
}

export interface BoardView {
  id: string;
  name: string;
  type: string;
  settings_str?: string;
}

// ============================================================================
// Column Types
// ============================================================================

export type ColumnType =
  | "auto_number"
  | "board_relation"
  | "button"
  | "checkbox"
  | "color_picker"
  | "country"
  | "creation_log"
  | "date"
  | "dependency"
  | "doc"
  | "dropdown"
  | "email"
  | "file"
  | "formula"
  | "hour"
  | "item_id"
  | "label"
  | "last_updated"
  | "link"
  | "location"
  | "long_text"
  | "mirror"
  | "name"
  | "numbers"
  | "people"
  | "phone"
  | "progress"
  | "rating"
  | "status"
  | "subtasks"
  | "tags"
  | "team"
  | "text"
  | "time_tracking"
  | "timeline"
  | "vote"
  | "week"
  | "world_clock";

export interface Column {
  id: string;
  title: string;
  type: ColumnType;
  description?: string;
  settings_str?: string;
  archived?: boolean;
  width?: number;
}

// ============================================================================
// Column Value Types (typed per column type)
// ============================================================================

export type ColumnValue =
  | TextColumnValue
  | StatusColumnValue
  | NumbersColumnValue
  | DateColumnValue
  | PeopleColumnValue
  | TimelineColumnValue
  | DropdownColumnValue
  | CheckboxColumnValue
  | RatingColumnValue
  | LabelColumnValue
  | LinkColumnValue
  | EmailColumnValue
  | PhoneColumnValue
  | LongTextColumnValue
  | ColorPickerColumnValue
  | TagsColumnValue
  | HourColumnValue
  | WeekColumnValue
  | WorldClockColumnValue
  | FileColumnValue
  | BoardRelationColumnValue
  | MirrorColumnValue
  | FormulaColumnValue
  | AutoNumberColumnValue
  | CreationLogColumnValue
  | LastUpdatedColumnValue
  | DependencyColumnValue
  | GenericColumnValue;

export interface BaseColumnValue {
  id: string;
  column?: Column;
  text?: string;
  value?: string | null;
  type: ColumnType;
}

export interface TextColumnValue extends BaseColumnValue {
  type: "text";
  value: string | null;
}

export interface StatusColumnValue extends BaseColumnValue {
  type: "status";
  value: string | null; // JSON: { "index": number, "label": string }
}

export interface NumbersColumnValue extends BaseColumnValue {
  type: "numbers";
  value: string | null; // JSON: number as string
}

export interface DateColumnValue extends BaseColumnValue {
  type: "date";
  value: string | null; // JSON: { "date": "YYYY-MM-DD", "time": "HH:MM:SS" }
}

export interface PeopleColumnValue extends BaseColumnValue {
  type: "people";
  value: string | null; // JSON: { "personsAndTeams": [{ "id": number, "kind": "person" | "team" }] }
}

export interface TimelineColumnValue extends BaseColumnValue {
  type: "timeline";
  value: string | null; // JSON: { "from": "YYYY-MM-DD", "to": "YYYY-MM-DD" }
}

export interface DropdownColumnValue extends BaseColumnValue {
  type: "dropdown";
  value: string | null; // JSON: { "ids": [number] }
}

export interface CheckboxColumnValue extends BaseColumnValue {
  type: "checkbox";
  value: string | null; // JSON: { "checked": boolean }
}

export interface RatingColumnValue extends BaseColumnValue {
  type: "rating";
  value: string | null; // JSON: { "rating": number }
}

export interface LabelColumnValue extends BaseColumnValue {
  type: "label";
  value: string | null; // JSON: { "label": string }
}

export interface LinkColumnValue extends BaseColumnValue {
  type: "link";
  value: string | null; // JSON: { "url": string, "text": string }
}

export interface EmailColumnValue extends BaseColumnValue {
  type: "email";
  value: string | null; // JSON: { "email": string, "text": string }
}

export interface PhoneColumnValue extends BaseColumnValue {
  type: "phone";
  value: string | null; // JSON: { "phone": string, "countryShortName": string }
}

export interface LongTextColumnValue extends BaseColumnValue {
  type: "long_text";
  value: string | null; // JSON: { "text": string }
}

export interface ColorPickerColumnValue extends BaseColumnValue {
  type: "color_picker";
  value: string | null; // JSON: { "color": string }
}

export interface TagsColumnValue extends BaseColumnValue {
  type: "tags";
  value: string | null; // JSON: { "tag_ids": [number] }
}

export interface HourColumnValue extends BaseColumnValue {
  type: "hour";
  value: string | null; // JSON: { "hour": number, "minute": number }
}

export interface WeekColumnValue extends BaseColumnValue {
  type: "week";
  value: string | null; // JSON: { "week": { "startDate": "YYYY-MM-DD", "endDate": "YYYY-MM-DD" } }
}

export interface WorldClockColumnValue extends BaseColumnValue {
  type: "world_clock";
  value: string | null; // JSON: { "timezone": string }
}

export interface FileColumnValue extends BaseColumnValue {
  type: "file";
  value: string | null; // JSON: { "files": [{ "id": string, "name": string, "url": string }] }
}

export interface BoardRelationColumnValue extends BaseColumnValue {
  type: "board_relation";
  value: string | null; // JSON: { "linkedPulseIds": [{ "linkedPulseId": number }] }
}

export interface MirrorColumnValue extends BaseColumnValue {
  type: "mirror";
  value: string | null; // Mirrored value from connected board
}

export interface FormulaColumnValue extends BaseColumnValue {
  type: "formula";
  value: string | null; // Calculated formula result
}

export interface AutoNumberColumnValue extends BaseColumnValue {
  type: "auto_number";
  value: string | null; // JSON: number
}

export interface CreationLogColumnValue extends BaseColumnValue {
  type: "creation_log";
  value: string | null; // JSON: { "created_at": "ISO8601", "creator_id": number }
}

export interface LastUpdatedColumnValue extends BaseColumnValue {
  type: "last_updated";
  value: string | null; // JSON: { "updated_at": "ISO8601", "updater_id": number }
}

export interface DependencyColumnValue extends BaseColumnValue {
  type: "dependency";
  value: string | null; // JSON: { "linkedPulseIds": [number] }
}

export interface GenericColumnValue extends BaseColumnValue {
  type: ColumnType;
}

// ============================================================================
// Item Types
// ============================================================================

export interface Item {
  id: string;
  name: string;
  board?: Board;
  group?: Group;
  state: "active" | "archived" | "deleted";
  creator_id?: string;
  created_at?: string;
  updated_at?: string;
  column_values?: ColumnValue[];
  subitems?: SubItem[];
  parent_item?: Item;
  subscribers?: User[];
  updates?: Update[];
}

export interface SubItem {
  id: string;
  name: string;
  board?: Board;
  column_values?: ColumnValue[];
  created_at?: string;
  updated_at?: string;
  parent_item?: Item;
}

// ============================================================================
// Group Types
// ============================================================================

export interface Group {
  id: string;
  title: string;
  color?: string;
  position?: string;
  archived?: boolean;
  deleted?: boolean;
  items?: Item[];
}

// ============================================================================
// Update Types (Activity Feed)
// ============================================================================

export interface Update {
  id: string;
  body: string;
  creator_id?: string;
  creator?: User;
  created_at?: string;
  updated_at?: string;
  item_id?: string;
  text_body?: string;
  replies?: Update[];
  assets?: Asset[];
}

export interface Asset {
  id: string;
  name: string;
  url: string;
  public_url?: string;
  file_extension?: string;
  file_size?: number;
  uploaded_by?: User;
  created_at?: string;
}

// ============================================================================
// User, Team, Account Types
// ============================================================================

export interface User {
  id: string;
  name: string;
  email: string;
  url?: string;
  photo_original?: string;
  photo_thumb?: string;
  photo_tiny?: string;
  is_guest?: boolean;
  is_pending?: boolean;
  enabled?: boolean;
  created_at?: string;
  birthday?: string;
  country_code?: string;
  location?: string;
  time_zone_identifier?: string;
  title?: string;
  phone?: string;
  mobile_phone?: string;
  teams?: Team[];
  account?: Account;
}

export interface Team {
  id: string;
  name: string;
  picture_url?: string;
  users?: User[];
}

export interface Account {
  id: string;
  name: string;
  slug?: string;
  tier?: string;
  plan?: Plan;
  logo?: string;
  show_timeline_weekends?: boolean;
  first_day_of_the_week?: string;
}

export interface Plan {
  max_users: number;
  period?: string;
  tier?: string;
  version: number;
}

// ============================================================================
// Workspace & Folder Types
// ============================================================================

export interface Workspace {
  id: string;
  name: string;
  kind: "open" | "closed";
  description?: string;
  created_at?: string;
  owners_subscribers?: User[];
  teams_subscribers?: Team[];
  users_subscribers?: User[];
}

export interface Folder {
  id: string;
  name: string;
  color?: string;
  children?: Folder[];
  workspace_id?: string;
  parent_id?: string;
}

// ============================================================================
// Webhook Types
// ============================================================================

export interface Webhook {
  id: string;
  board_id: string;
  url: string;
  event:
    | "create_item"
    | "change_column_value"
    | "change_status_column_value"
    | "change_specific_column_value"
    | "create_update"
    | "delete_update"
    | "item_archived"
    | "item_deleted"
    | "item_moved_to_group"
    | "item_restored"
    | "subitem_created";
  config?: string;
}

// ============================================================================
// Automation Types
// ============================================================================

export interface Automation {
  id: string;
  name: string;
  enabled?: boolean;
}

// ============================================================================
// Notification Types
// ============================================================================

export interface Notification {
  id: string;
  text: string;
}

// ============================================================================
// Tag Types
// ============================================================================

export interface Tag {
  id: string;
  name: string;
  color?: string;
}

// ============================================================================
// Activity Log Types
// ============================================================================

export interface ActivityLog {
  id: string;
  account_id: string;
  user_id: string;
  event: string;
  data?: string;
  created_at: string;
  entity?: string;
}

// ============================================================================
// Complexity & Rate Limiting Types
// ============================================================================

export interface Complexity {
  /**
   * Query complexity cost
   */
  query: number;
  /**
   * Complexity before the query
   */
  before: number;
  /**
   * Complexity after the query
   */
  after: number;
  /**
   * Time to reset (Unix timestamp)
   */
  reset_in_x_seconds: number;
}

// ============================================================================
// GraphQL Response Wrapper Types
// ============================================================================

export interface GraphQLResponse<T = any> {
  data?: T;
  errors?: GraphQLError[];
  account_id?: string;
}

export interface GraphQLError {
  message: string;
  locations?: Array<{ line: number; column: number }>;
  path?: string[];
  extensions?: {
    code?: string;
    [key: string]: any;
  };
}

export interface MondayResponse<T = any> {
  data: T;
  complexity?: Complexity;
  account_id?: string;
}

// ============================================================================
// Pagination Types
// ============================================================================

export interface ItemsPage {
  cursor?: string;
  items: Item[];
}

export interface BoardsPage {
  cursor?: string;
  boards: Board[];
}

export interface PaginationOptions {
  limit?: number;
  page?: number;
  cursor?: string;
}

// ============================================================================
// Query Options
// ============================================================================

export interface GetBoardsOptions extends PaginationOptions {
  state?: "active" | "archived" | "deleted" | "all";
  board_kind?: BoardKind;
  workspace_ids?: string[];
}

export interface GetItemsOptions extends PaginationOptions {
  ids?: string[];
  newest_first?: boolean;
}

export interface CreateBoardOptions {
  board_name: string;
  board_kind: BoardKind;
  description?: string;
  workspace_id?: string;
  folder_id?: string;
  template_id?: string;
}

export interface CreateItemOptions {
  board_id: string;
  group_id?: string;
  item_name: string;
  column_values?: Record<string, any>;
  create_labels_if_missing?: boolean;
}

export interface ChangeColumnValueOptions {
  board_id: string;
  item_id: string;
  column_id: string;
  value: any;
}

export interface CreateGroupOptions {
  board_id: string;
  group_name: string;
  position_relative_method?: "before_at" | "after_at";
  relative_to?: string;
}

export interface CreateUpdateOptions {
  item_id: string;
  body: string;
  parent_id?: string;
}

export interface CreateWebhookOptions {
  board_id: string;
  url: string;
  event: Webhook["event"];
  config?: Record<string, any>;
}
