// Autotask API Types

export interface AutotaskConfig {
  apiUrl: string;
  username: string;
  password: string;
  apiIntegrationCode: string;
}

export type AutotaskHeaders = {
  Authorization: string;
  ApiIntegrationcode: string;
  'Content-Type': string;
  Accept: string;
  ImpersonationResourceId?: string;
}

export interface QueryFilter {
  op: 'eq' | 'noteq' | 'gt' | 'lt' | 'gte' | 'lte' | 'contains' | 'beginsWith' | 'endsWith' | 'exist';
  field: string;
  value?: string | number | boolean;
}

export interface QueryParams {
  filter?: QueryFilter[];
  maxRecords?: number;
  includeFields?: string[];
  excludeFields?: string[];
}

export interface Resource {
  id: number;
  // Name fields
  firstName: string;
  lastName: string;
  middleInitial?: string;
  namePrefix?: string;
  nameSuffix?: string;
  
  // Contact information
  email?: string;
  emailAddress?: string;
  emailAddress2?: string;
  emailAddress3?: string;
  officePhone?: string;
  mobilePhone?: string;
  homePhone?: string;
  officeExtension?: string;
  
  // System fields
  userName?: string;
  title?: string;
  isActive?: boolean;
  
  // Employment details
  resourceType?: number;
  payrollIdentifier?: string;
  payrollType?: number;
  accountingReferenceID?: string;
  internalCost?: number;
  hireDate?: string;
  
  // Location and availability
  locationID?: number;
  travelAvailabilityPct?: number;
  defaultServiceDeskRoleID?: number;
  
  // System preferences
  emailTypeCode?: number;
  numberFormat?: string;
  timeFormat?: string;
  dateFormat?: string;
  
  // Demographics and security
  gender?: number;
  licenseType?: number;
  securityLevel?: number;
  
  // Ratings
  surveyResourceRating?: number;
}

export interface Ticket {
  id: number;
  ticketNumber: string;
  title: string;
  description?: string;
  status: number;
  priority: number;
  assignedResourceID?: number;
  companyID: number;
  companyLocationID?: number;
  contactID?: number;
  createDate: string;
  dueDateTime?: string;
  lastActivityDate?: string;
  completedDate?: string;
  queueID?: number;
  issueType?: number;
  subIssueType?: number;
}

export interface Task {
  id: number;
  title: string;
  description?: string;
  status: number;
  priority: number;
  assignedResourceID?: number;
  projectID?: number;
  phaseID?: number;
  createDateTime: string;
  startDateTime?: string;
  endDateTime?: string;
  completedDateTime?: string;
  percentComplete?: number;
  estimatedHours?: number;
  actualHours?: number;
}

export interface Company {
  id: number;
  companyName: string;
  companyNumber?: string;
  isActive: boolean;
  phone?: string;
  alternatePhone1?: string;
  alternatePhone2?: string;
  fax?: string;
  webAddress?: string;
  address1?: string;
  address2?: string;
  city?: string;
  state?: string;
  postalCode?: string;
  countryID?: number;
  additionalAddressInformation?: string;
  companyType?: number;
  companyCategoryID?: number;
  classification?: string;
  territoryID?: number;
  marketSegmentID?: number;
  competitorID?: number;
  parentCompanyID?: number;
  ownerResourceID?: number;
  billingAddress1?: string;
  billingAddress2?: string;
  billToCity?: string;
  billToState?: string;
  billToZipCode?: string;
  billToCountryID?: number;
  billToAddressToUse?: number;
  billToAttention?: string;
  billToAdditionalAddressInformation?: string;
  billToCompanyLocationID?: number;
  taxID?: string;
  isTaxExempt?: boolean;
  taxRegionID?: number;
  currencyID?: number;
  invoiceMethod?: number;
  invoiceTemplateID?: number;
  invoiceEmailMessageID?: number;
  invoiceNonContractItemsToParentCompany?: boolean;
  quoteTemplateID?: number;
  quoteEmailMessageID?: number;
  purchaseOrderTemplateID?: number;
  keyAccountIcon?: number;
  isClientPortalActive?: boolean;
  isTaskFireActive?: boolean;
  isEnabledForComanaged?: boolean;
  sicCode?: string;
  stockMarket?: string;
  stockSymbol?: string;
  surveyCompanyRating?: number;
  assetValue?: number;
  apiVendorID?: number;
  lastActivityDate?: string;
  lastTrackedModifiedDateTime?: string;
  createDate?: string;
  createdByResourceID?: number;
  impersonatorCreatorResourceID?: number;
  userDefinedFields?: Array<{ name: string; value: string | null }>;
}

export interface TicketNote {
  id: number;
  ticketID: number;
  title?: string;
  description?: string;
  noteType?: number;
  publish?: number;
  creatorResourceID?: number;
  creatorType?: number;
  lastActivityDate?: string;
  createDateTime?: string;
}

export interface ConfigurationItem {
  id: number;
  companyID: number;
  companyLocationID?: number;
  contactID?: number;
  contractID?: number;
  contractServiceID?: number;
  createDate: string;
  createdByPersonType?: number;
  createdByResourceID?: number;
  dattoAvailableKilobytes?: number;
  dattoDeviceMemoryMegabytes?: number;
  dattoHostname?: string;
  dattoInternalIP?: string;
  dattoKernelVersionID?: number;
  dattoLastCheckInDateTime?: string;
  dattoNumberOfCPUs?: number;
  dattoNumberOfLogicalProcessors?: number;
  dattoOSVersionID?: number;
  dattoProtectedKilobytes?: number;
  dattoRemoteIP?: string;
  dattoSerialNumber?: string;
  dattoUDF?: string;
  dattoUsedKilobytes?: number;
  dattoZfsPoolZpoolVersionID?: number;
  deviceNetworkingID?: string;
  dnsServer1?: string;
  dnsServer2?: string;
  installDate?: string;
  installedByContactID?: number;
  installedByID?: number;
  installedProductCategoryID?: number;
  isActive: boolean;
  lastActivityPersonResourceID?: number;
  lastActivityPersonType?: number;
  lastModifiedTime?: string;
  location?: string;
  macAddress?: string;
  modelNumber?: string;
  notes?: string;
  numberOfUsers?: number;
  parentConfigurationItemID?: number;
  productID?: number;
  referenceNumber?: string;
  referenceTitle: string;
  rmmDeviceAuditAntivirusStatusID?: number;
  rmmDeviceAuditArchitectureID?: number;
  rmmDeviceAuditBackupStatusID?: number;
  rmmDeviceAuditDescription?: string;
  rmmDeviceAuditDeviceTypeID?: number;
  rmmDeviceAuditDisplayAdaptorID?: number;
  rmmDeviceAuditDomainID?: number;
  rmmDeviceAuditHostname?: string;
  rmmDeviceAuditIPAddress?: string;
  rmmDeviceAuditLastUser?: string;
  rmmDeviceAuditMacAddress?: string;
  rmmDeviceAuditManufacturerID?: number;
  rmmDeviceAuditMemoryBytes?: number;
  rmmDeviceAuditMissingPatchCount?: number;
  rmmDeviceAuditMobileNetworkOperatorID?: number;
  rmmDeviceAuditMobileNumber?: string;
  rmmDeviceAuditModelID?: number;
  rmmDeviceAuditMotherboardID?: number;
  rmmDeviceAuditOperatingSystemID?: number;
  rmmDeviceAuditPatchStatusID?: number;
  rmmDeviceAuditProcessorID?: number;
  rmmDeviceAuditServicePackID?: number;
  rmmDeviceAuditSNMPContact?: string;
  rmmDeviceAuditSNMPLocation?: string;
  rmmDeviceAuditSNMPName?: string;
  rmmDeviceAuditSoftwareStatusID?: number;
  rmmDeviceAuditStorageBytes?: number;
  rmmDeviceID?: string;
  rmmDeviceUID?: string;
  rmmOpenAlertCount?: number;
  serialNumber?: string;
  serviceBundleID?: number;
  serviceID?: number;
  serviceLevelAgreementID?: number;
  setupFee?: number;
  sourceProductID?: number;
  type?: number;
  configurationItemType?: number; // API field name (camelCase)
  configuration_item_type?: number; // Database field name (snake_case)
  vendorID?: number;
  vendorName?: string;
  warrantyExpirationDate?: string;
}

export interface Attachment {
  id: number;
  attachmentType: 'FILE_ATTACHMENT' | 'FILE_LINK' | 'URL' | 'NOTE';
  fullPath: string;
  title: string;
  publish: 1 | 2; // 1 = All Autotask Users, 2 = Internal Users Only
  data?: string; // Base64 encoded file data
  contentType?: string;
  createDate?: string;
  creatorResourceID?: number;
}

export interface PicklistValue {
  value: number | string;
  label: string;
  isDefaultValue?: boolean;
  sortOrder?: number;
  isActive?: boolean;
  isSystem?: boolean;
  parentValue?: number | string;
}

export interface EntityField {
  name: string;
  dataType: string;
  length?: number;
  isRequired?: boolean;
  isReadOnly?: boolean;
  isQueryable?: boolean;
  isReference?: boolean;
  referenceEntityType?: string;
  picklistValues?: PicklistValue[];
}

export interface AutotaskTimeEntry {
  id: number;
  resourceID: number;
  ticketID?: number;
  taskID?: number;
  contractID?: number;
  contractServiceID?: number;
  contractServiceBundleID?: number;
  dateWorked: string; // ISO date string
  hoursWorked: number;
  hoursToBill?: number; // Read-only: actual hours that will be billed (may differ from hoursWorked due to contract caps)
  summaryNotes?: string;
  internalNotes?: string;
  isInternalNotesVisibleToComanaged?: boolean;
  timeEntryType?: number; // Read-only
  startDateTime?: string;
  endDateTime?: string;
  offsetHours?: number;
  isNonBillable?: boolean; // True if this entry is non-billable
  showOnInvoice?: boolean;
  billingCodeID?: number;
  internalBillingCodeID?: number;
  billingApprovalLevelMostRecent?: number; // Read-only
  billingApprovalResourceID?: number;
  billingApprovalDateTime?: string;
  roleID?: number;
  createDateTime?: string;
  creatorUserID?: number;
  lastModifiedDateTime?: string;
  lastModifiedUserID?: number;
  impersonatorCreatorResourceID?: number;
  impersonatorUpdaterResourceID?: number;
}

export interface ApiResponse<T> {
  item?: T;
  items?: T[];
  pageDetails?: {
    count: number;
    requestCount: number;
    prevPageUrl?: string;
    nextPageUrl?: string;
  };
}

export interface ApiError {
  message: string;
  errors?: Array<{
    message: string;
    field?: string;
  }>;
}

export enum TicketStatus {
  New = 1,
  InProgress = 8,
  Waiting = 9,
  Complete = 5,
}

export enum TaskStatus {
  New = 1,
  InProgress = 11,
  Waiting = 12,
  Complete = 5,
}

export enum Priority {
  Critical = 1,
  High = 2,
  Medium = 3,
  Low = 4,
}
