// Wrike API Types - Complete coverage

export interface WrikeApiResponse<T> {
  kind: string;
  data: T[];
}

export interface WrikeError {
  errorDescription: string;
  error: string;
}

// Base Types
export interface WrikeDate {
  start?: string;
  due?: string;
  duration?: number;
  workOnWeekends?: boolean;
}

export interface WrikeCustomField {
  id: string;
  value: string | number | boolean | string[];
}

export interface WrikeMetadata {
  key: string;
  value: string;
}

// Task Types
export interface WrikeTask {
  id: string;
  accountId: string;
  title: string;
  description?: string;
  briefDescription?: string;
  parentIds: string[];
  superParentIds: string[];
  sharedIds: string[];
  responsibleIds: string[];
  status: string;
  importance: 'High' | 'Normal' | 'Low';
  createdDate: string;
  updatedDate: string;
  dates?: WrikeDate;
  scope: 'RbTask' | 'WsTask';
  authorIds: string[];
  customStatusId?: string;
  hasAttachments: boolean;
  attachmentCount?: number;
  permalink: string;
  priority?: string;
  followedByMe: boolean;
  followerIds: string[];
  recurrent?: boolean;
  superTaskIds: string[];
  subTaskIds: string[];
  dependencyIds: string[];
  metadata: WrikeMetadata[];
  customFields: WrikeCustomField[];
  effortAllocation?: {
    allocatedMinutes: number;
    mode: string;
  };
  billingType?: string;
  effectiveValueType?: string;
}

// Folder/Project Types
export interface WrikeFolder {
  id: string;
  accountId: string;
  title: string;
  color?: string;
  childIds: string[];
  superParentIds: string[];
  scope: 'RbFolder' | 'WsFolder';
  project?: WrikeProject;
  metadata: WrikeMetadata[];
  hasAttachments: boolean;
  attachmentCount?: number;
  description?: string;
  briefDescription?: string;
  customFields: WrikeCustomField[];
  customColumnIds?: string[];
  sharedIds: string[];
  parentIds: string[];
  permalink: string;
}

export interface WrikeProject {
  authorId: string;
  ownerIds: string[];
  status: 'Green' | 'Yellow' | 'Red' | 'Completed' | 'OnHold' | 'Cancelled';
  customStatusId?: string;
  startDate?: string;
  endDate?: string;
  createdDate: string;
  completedDate?: string;
  contractType?: 'Billable' | 'NonBillable';
}

// Space Types
export interface WrikeSpace {
  id: string;
  title: string;
  avatarUrl?: string;
  accessType: 'Personal' | 'Private' | 'Public';
  archived: boolean;
  memberIds: string[];
  guestRoleId?: string;
  defaultProjectWorkflowId?: string;
  defaultTaskWorkflowId?: string;
}

// Comment Types
export interface WrikeComment {
  id: string;
  authorId: string;
  text: string;
  createdDate: string;
  updatedDate?: string;
  taskId?: string;
  folderId?: string;
  type?: 'Comment' | 'Attachment';
}

// Attachment Types
export interface WrikeAttachment {
  id: string;
  authorId: string;
  name: string;
  createdDate: string;
  version: number;
  type: string;
  contentType?: string;
  size: number;
  taskId?: string;
  folderId?: string;
  commentId?: string;
  url?: string;
  previewUrl?: string;
}

// Timelog Types
export interface WrikeTimelog {
  id: string;
  taskId: string;
  userId: string;
  categoryId?: string;
  hours: number;
  createdDate: string;
  updatedDate: string;
  trackedDate: string;
  comment?: string;
  billable?: boolean;
}

// Contact/User Types
export interface WrikeContact {
  id: string;
  firstName: string;
  lastName: string;
  type: 'Person' | 'Group';
  profiles: WrikeProfile[];
  avatarUrl?: string;
  timezone?: string;
  locale?: string;
  deleted: boolean;
  me?: boolean;
  memberIds?: string[];
  metadata: WrikeMetadata[];
  myTeam?: boolean;
  title?: string;
  companyName?: string;
  phone?: string;
  location?: string;
}

export interface WrikeProfile {
  accountId: string;
  email: string;
  role: 'User' | 'Collaborator' | 'Owner';
  external: boolean;
  admin: boolean;
  owner: boolean;
}

// Group Types
export interface WrikeGroup {
  id: string;
  accountId: string;
  title: string;
  memberIds: string[];
  childIds: string[];
  parentIds: string[];
  avatarUrl?: string;
  myTeam: boolean;
  metadata: WrikeMetadata[];
}

// Workflow Types
export interface WrikeWorkflow {
  id: string;
  name: string;
  standard: boolean;
  hidden: boolean;
  customStatuses: WrikeCustomStatus[];
}

export interface WrikeCustomStatus {
  id: string;
  name: string;
  standardName: boolean;
  color: string;
  standard: boolean;
  group: 'Active' | 'Completed' | 'Deferred' | 'Cancelled';
  hidden: boolean;
}

// Custom Field Types
export interface WrikeCustomFieldDefinition {
  id: string;
  accountId: string;
  title: string;
  type: 'Text' | 'DropDown' | 'Numeric' | 'Currency' | 'Percentage' | 'Date' | 'Duration' | 'Checkbox' | 'Contacts' | 'Multiple';
  sharedIds: string[];
  settings?: {
    inheritanceType?: 'All' | 'None';
    decimalPlaces?: number;
    useThousandsSeparator?: boolean;
    currency?: string;
    aggregation?: string;
    values?: string[];
    allowOtherValues?: boolean;
    readOnly?: boolean;
  };
}

// Approval Types
export interface WrikeApproval {
  id: string;
  authorId: string;
  title: string;
  description?: string;
  status: 'Pending' | 'Approved' | 'Rejected' | 'Cancelled';
  dueDate?: string;
  finishedDate?: string;
  decisionMakerIds: string[];
  approverIds: string[];
  taskIds: string[];
  folderIds: string[];
  decisions: WrikeApprovalDecision[];
}

export interface WrikeApprovalDecision {
  id: string;
  approverId: string;
  decision: 'Approved' | 'Rejected';
  comment?: string;
  updatedDate: string;
}

// Work Schedule Types
export interface WrikeWorkSchedule {
  id: string;
  name: string;
  startTime: string;
  endTime: string;
  workDays: number[];
  userId?: string;
  exceptDates?: string[];
}

export interface WrikeWorkScheduleException {
  id: string;
  workScheduleId: string;
  fromDate: string;
  toDate: string;
  isWorking: boolean;
}

// Webhook Types
export interface WrikeWebhook {
  id: string;
  accountId: string;
  hookUrl: string;
  folderId?: string;
  taskId?: string;
  commentId?: string;
  attachmentId?: string;
  timelogId?: string;
}

// Blueprint/Template Types
export interface WrikeBlueprint {
  id: string;
  title: string;
  description?: string;
  scope: 'Personal' | 'Account';
  source?: {
    type: 'Folder' | 'Project';
    id: string;
  };
}

// Audit Log Types
export interface WrikeAuditLogEntry {
  id: string;
  operation: string;
  userId: string;
  userEmail: string;
  eventDate: string;
  ipAddress?: string;
  objectType?: string;
  objectId?: string;
  objectName?: string;
  details?: Record<string, unknown>;
}

// Dependency Types
export interface WrikeDependency {
  id: string;
  predecessorId: string;
  successorId: string;
  relationType: 'StartToStart' | 'StartToFinish' | 'FinishToStart' | 'FinishToFinish';
}

// Invite Types
export interface WrikeInvitation {
  id: string;
  accountId: string;
  firstName: string;
  lastName: string;
  email: string;
  status: 'Pending' | 'Accepted' | 'Declined' | 'Cancelled';
  inviterUserId: string;
  invitationDate: string;
  resolvedDate?: string;
  role: string;
  external: boolean;
}

// Data Export Types
export interface WrikeDataExport {
  id: string;
  status: 'InProgress' | 'Completed' | 'Failed';
  type: 'Account' | 'Space';
  completedDate?: string;
  resources?: {
    type: string;
    url: string;
  }[];
}

// Account Types
export interface WrikeAccount {
  id: string;
  name: string;
  dateFormat: string;
  firstDayOfWeek: string;
  workDays: string[];
  rootFolderId: string;
  recycleBinId: string;
  createdDate: string;
  subscription?: {
    type: string;
    suspended: boolean;
    userLimit?: number;
  };
  metadata: WrikeMetadata[];
  customFields: string[];
  joinedDate?: string;
}

// Color Types
export type WrikeColor = 'None' | 'Person1' | 'Person2' | 'Person3' | 'Person4' | 'Person5' | 'Person6' | 'Person7';

// Query Parameter Types
export interface WrikeQueryParams {
  fields?: string[];
  descendants?: boolean;
  metadata?: string;
  customFields?: string[];
  updatedDate?: { start?: string; end?: string };
  createdDate?: { start?: string; end?: string };
  completedDate?: { start?: string; end?: string };
  scheduledDate?: { start?: string; end?: string };
  dueDate?: { start?: string; end?: string };
  trackedDate?: { start?: string; end?: string };
  status?: string;
  importance?: string;
  sortField?: string;
  sortOrder?: 'Asc' | 'Desc';
  limit?: number;
  pageSize?: number;
  nextPageToken?: string;
  type?: string;
  deleted?: boolean;
  contractors?: string[];
  authors?: string[];
  responsibles?: string[];
  followers?: string[];
  statuses?: string[];
  permalink?: string;
  customStatus?: string[];
  project?: boolean;
  subTasks?: boolean;
  plainText?: boolean;
  withArchived?: boolean;
  test?: boolean;
  versions?: boolean;
  me?: boolean;
  billable?: boolean;
  categoryId?: string;
  [key: string]: unknown;
}

// Request Body Types
export interface CreateTaskRequest {
  title: string;
  description?: string;
  status?: string;
  importance?: 'High' | 'Normal' | 'Low';
  dates?: {
    start?: string;
    due?: string;
    duration?: number;
    type?: 'Backlog' | 'Milestone' | 'Planned';
  };
  shareds?: string[];
  parents?: string[];
  responsibles?: string[];
  followers?: string[];
  follow?: boolean;
  priorityBefore?: string;
  priorityAfter?: string;
  superTasks?: string[];
  metadata?: Array<{ key: string; value: string }>;
  customFields?: Array<{ id: string; value: string | number | string[] }>;
  customStatus?: string;
  effortAllocation?: {
    allocatedMinutes: number;
    mode: 'FullTime' | 'None';
  };
}

export interface UpdateTaskRequest {
  title?: string;
  description?: string;
  status?: string;
  importance?: 'High' | 'Normal' | 'Low';
  dates?: {
    start?: string;
    due?: string;
    duration?: number;
    type?: 'Backlog' | 'Milestone' | 'Planned';
  };
  addParents?: string[];
  removeParents?: string[];
  addShareds?: string[];
  removeShareds?: string[];
  addResponsibles?: string[];
  removeResponsibles?: string[];
  addFollowers?: string[];
  removeFollowers?: string[];
  addSuperTasks?: string[];
  removeSuperTasks?: string[];
  metadata?: Array<{ key: string; value: string }>;
  customFields?: Array<{ id: string; value: string | number | string[] }>;
  customStatus?: string;
  priorityBefore?: string;
  priorityAfter?: string;
  restore?: boolean;
  effortAllocation?: {
    allocatedMinutes: number;
    mode: 'FullTime' | 'None';
  };
}

export interface CreateFolderRequest {
  title: string;
  description?: string;
  shareds?: string[];
  metadata?: Array<{ key: string; value: string }>;
  customFields?: Array<{ id: string; value: string | number | string[] }>;
  project?: {
    status?: 'Green' | 'Yellow' | 'Red' | 'Completed' | 'OnHold' | 'Cancelled';
    ownerIds?: string[];
    startDate?: string;
    endDate?: string;
    contractType?: 'Billable' | 'NonBillable';
    customStatusId?: string;
  };
}

export interface CreateCommentRequest {
  text: string;
  plainText?: boolean;
}

export interface CreateTimelogRequest {
  hours: number;
  trackedDate: string;
  comment?: string;
  categoryId?: string;
  billable?: boolean;
}

export interface CreateWebhookRequest {
  hookUrl: string;
  folderId?: string;
  taskId?: string;
}
