/**
 * Todoist API Type Definitions
 *
 * These types are based on the Todoist REST API v2 documentation.
 * @see https://developer.todoist.com/rest/v2/
 */

// ============================================================================
// Authentication Types
// ============================================================================

export interface TodoistAuthConfig {
  /** Todoist API Token (Bearer token) */
  apiToken: string;
}

// ============================================================================
// Task Types
// ============================================================================

export interface TodoistTask {
  id: string;
  assigner_id: string | null;
  assignee_id: string | null;
  project_id: string;
  section_id: string | null;
  parent_id: string | null;
  order: number;
  content: string;
  description: string;
  is_completed: boolean;
  labels: string[];
  priority: 1 | 2 | 3 | 4;
  comment_count: number;
  creator_id: string;
  created_at: string;
  due: TodoistDue | null;
  url: string;
  duration: TodoistDuration | null;
}

export interface TodoistDue {
  date: string;
  string: string;
  lang: string;
  is_recurring: boolean;
  datetime?: string;
  timezone?: string;
}

export interface TodoistDuration {
  amount: number;
  unit: 'minute' | 'day';
}

export interface TodoistCreateTaskRequest {
  /** Task content (required) */
  content: string;
  /** Task description */
  description?: string;
  /** Project ID */
  project_id?: string;
  /** Section ID */
  section_id?: string;
  /** Parent task ID (for subtasks) */
  parent_id?: string;
  /** Task order */
  order?: number;
  /** Labels (array of label names) */
  labels?: string[];
  /** Priority (1-4, where 4 is highest) */
  priority?: 1 | 2 | 3 | 4;
  /** Due date string (e.g., "tomorrow", "2024-01-15") */
  due_string?: string;
  /** Due date (YYYY-MM-DD) */
  due_date?: string;
  /** Due datetime (RFC 3339) */
  due_datetime?: string;
  /** Due language */
  due_lang?: string;
  /** Assignee ID */
  assignee_id?: string;
  /** Duration */
  duration?: number;
  /** Duration unit */
  duration_unit?: 'minute' | 'day';
}

export interface TodoistUpdateTaskRequest {
  /** Task content */
  content?: string;
  /** Task description */
  description?: string;
  /** Labels */
  labels?: string[];
  /** Priority (1-4) */
  priority?: 1 | 2 | 3 | 4;
  /** Due date string */
  due_string?: string;
  /** Due date (YYYY-MM-DD) */
  due_date?: string;
  /** Due datetime (RFC 3339) */
  due_datetime?: string;
  /** Due language */
  due_lang?: string;
  /** Assignee ID */
  assignee_id?: string | null;
  /** Duration */
  duration?: number | null;
  /** Duration unit */
  duration_unit?: 'minute' | 'day';
}

export interface TodoistGetTasksRequest {
  /** Filter by project ID */
  project_id?: string;
  /** Filter by section ID */
  section_id?: string;
  /** Filter by label name */
  label?: string;
  /** Filter expression (e.g., "today", "overdue") */
  filter?: string;
  /** Language for filter */
  lang?: string;
  /** Filter by task IDs */
  ids?: string[];
}

// ============================================================================
// Project Types
// ============================================================================

export interface TodoistProject {
  id: string;
  name: string;
  comment_count: number;
  order: number;
  color: string;
  is_shared: boolean;
  is_favorite: boolean;
  is_inbox_project: boolean;
  is_team_inbox: boolean;
  view_style: 'list' | 'board';
  url: string;
  parent_id: string | null;
}

export interface TodoistCreateProjectRequest {
  /** Project name (required) */
  name: string;
  /** Parent project ID */
  parent_id?: string;
  /** Project color */
  color?: string;
  /** Is favorite */
  is_favorite?: boolean;
  /** View style */
  view_style?: 'list' | 'board';
}

export interface TodoistUpdateProjectRequest {
  /** Project name */
  name?: string;
  /** Project color */
  color?: string;
  /** Is favorite */
  is_favorite?: boolean;
  /** View style */
  view_style?: 'list' | 'board';
}

// ============================================================================
// Section Types
// ============================================================================

export interface TodoistSection {
  id: string;
  project_id: string;
  order: number;
  name: string;
}

export interface TodoistCreateSectionRequest {
  /** Section name (required) */
  name: string;
  /** Project ID (required) */
  project_id: string;
  /** Section order */
  order?: number;
}

// ============================================================================
// Label Types
// ============================================================================

export interface TodoistLabel {
  id: string;
  name: string;
  color: string;
  order: number;
  is_favorite: boolean;
}

export interface TodoistCreateLabelRequest {
  /** Label name (required) */
  name: string;
  /** Label color */
  color?: string;
  /** Label order */
  order?: number;
  /** Is favorite */
  is_favorite?: boolean;
}

// ============================================================================
// Comment Types
// ============================================================================

export interface TodoistComment {
  id: string;
  task_id: string | null;
  project_id: string | null;
  posted_at: string;
  content: string;
  attachment: TodoistAttachment | null;
}

export interface TodoistAttachment {
  file_name: string;
  file_type: string;
  file_url: string;
  resource_type: string;
}

export interface TodoistCreateCommentRequest {
  /** Comment content (required) */
  content: string;
  /** Task ID (either task_id or project_id required) */
  task_id?: string;
  /** Project ID (either task_id or project_id required) */
  project_id?: string;
  /** Attachment */
  attachment?: {
    file_name: string;
    file_type: string;
    file_url: string;
    resource_type: string;
  };
}

// ============================================================================
// Sync API Types
// ============================================================================

export interface TodoistSyncRequest {
  /** Sync token (use '*' for full sync) */
  sync_token: string;
  /** Resource types to sync */
  resource_types: string[];
}

export interface TodoistSyncResponse {
  sync_token: string;
  full_sync: boolean;
  items?: TodoistTask[];
  projects?: TodoistProject[];
  sections?: TodoistSection[];
  labels?: TodoistLabel[];
  notes?: TodoistComment[];
}

// ============================================================================
// API Response Types
// ============================================================================

export interface TodoistApiError {
  error_code: number;
  error: string;
  error_extra?: Record<string, unknown>;
  http_code: number;
}

// ============================================================================
// Color Types
// ============================================================================

export type TodoistColor =
  | 'berry_red'
  | 'red'
  | 'orange'
  | 'yellow'
  | 'olive_green'
  | 'lime_green'
  | 'green'
  | 'mint_green'
  | 'teal'
  | 'sky_blue'
  | 'light_blue'
  | 'blue'
  | 'grape'
  | 'violet'
  | 'lavender'
  | 'magenta'
  | 'salmon'
  | 'charcoal'
  | 'grey'
  | 'taupe';
