/**
 * Shared Attio param/result types (browser-safe)
 *
 * These types define the public interface for Attio operations -- used by both
 * the server-side adapter and the SDK typed wrappers. They contain zero Node.js
 * dependencies and are safe to import in any environment.
 *
 * Server-internal types (API response shapes, credentials) remain in
 * server/adapters/attio/fetch/utils/types.ts
 */

// ============================================================================
// Record ID
// ============================================================================

/**
 * Record ID structure returned by Attio API
 */
export interface RecordId {
  workspace_id: string
  object_id: string
  record_id: string
}

// ============================================================================
// Record Operation Params & Results
// ============================================================================

/**
 * Create record parameters
 */
export interface CreateRecordParams {
  object: string // Object type: 'people', 'companies', 'deals', or custom slug
  values: Record<string, unknown> // Attribute slug/ID -> value mapping
}

/**
 * Create record result
 */
export interface CreateRecordResult {
  recordId: string // Just the record UUID (extracted from full ID structure)
  success: boolean
}

/**
 * Update record parameters
 */
export interface UpdateRecordParams {
  object: string
  recordId: string
  values: Record<string, unknown>
}

/**
 * Update record result
 */
export interface UpdateRecordResult {
  recordId: string
  success: boolean
}

/**
 * Filter expression (recursive type for nested filters)
 */
export type FilterExpression =
  | {
      [attribute: string]:
        | {
            [field: string]: {
              [operator: string]: unknown
            }
          }
        | FilterExpression
    }
  | {
      $and?: FilterExpression[]
      $or?: FilterExpression[]
    }

/**
 * Sort criteria for query results
 */
export interface SortCriteria {
  attribute: string // Attribute slug to sort by
  direction: 'asc' | 'desc' // Sort direction
  field?: string // Optional nested field path
}

/**
 * Query records parameters
 */
export interface QueryRecordsParams {
  object: string
  filter?: FilterExpression
  sorts?: SortCriteria[]
  limit?: number // Default: 500
  offset?: number // Default: 0
}

/**
 * Query records result
 */
export interface QueryRecordsResult {
  records: Array<{
    id: string // Record ID (extracted from full ID structure)
    values: Record<string, unknown> // Attribute values
    createdAt: string // ISO timestamp
    webUrl: string // Link to record in Attio UI
  }>
  hasMore: boolean // True if more records available
}

/**
 * Get record parameters
 */
export interface GetRecordParams {
  object: string
  recordId: string
}

/**
 * Get record result
 */
export interface GetRecordResult {
  id: string
  values: Record<string, unknown>
  createdAt: string
  webUrl: string
}

/**
 * Delete record parameters
 */
export interface DeleteRecordParams {
  object: string
  recordId: string
}

/**
 * Delete record result
 */
export interface DeleteRecordResult {
  recordId: string
  success: boolean
}

// ============================================================================
// Schema/Object Types
// ============================================================================

/**
 * Attio object (table) in workspace
 */
export interface AttioObject {
  id: string
  slug: string
  singularNoun: string
  pluralNoun: string
  createdAt: string
}

/**
 * Supported attribute types in Attio
 */
export type AttioAttributeType =
  | 'text'
  | 'number'
  | 'currency'
  | 'select'
  | 'checkbox'
  | 'date'
  | 'timestamp'
  | 'rating'
  | 'email-address'
  | 'phone-number'
  | 'domain'
  | 'location'
  | 'record-reference'
  | 'actor-reference'
  | 'personal-email-address'
  | 'interaction'

/**
 * Select option for select/multi-select attributes
 */
export interface SelectOption {
  id?: string // Omit for new options
  title: string
  isArchived: boolean
}

/**
 * Attribute configuration (type-specific)
 */
export interface AttributeConfig {
  options?: SelectOption[]
  targetObject?: string  // For record-reference
  currencyCode?: string  // For currency
  maxValue?: number      // For rating
}

/**
 * Attio attribute (field) on an object
 */
export interface AttioAttribute {
  id: string
  slug: string
  title: string
  description?: string
  type: AttioAttributeType
  isSystemAttribute: boolean
  isRequired: boolean
  isUnique: boolean
  isMultiselect: boolean
  isArchived: boolean
  config?: AttributeConfig
}

// ============================================================================
// Schema Operation Parameters & Results
// ============================================================================

/**
 * List objects parameters (no params needed)
 */
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface ListObjectsParams {}

/**
 * List objects result
 */
export interface ListObjectsResult {
  objects: AttioObject[]
}

/**
 * List attributes parameters
 */
export interface ListAttributesParams {
  objectSlug: string
}

/**
 * List attributes result
 */
export interface ListAttributesResult {
  attributes: AttioAttribute[]
}

/**
 * Create attribute parameters
 */
export interface CreateAttributeParams {
  objectSlug: string
  title: string
  apiSlug: string
  type: AttioAttributeType
  description?: string
  isRequired?: boolean
  isUnique?: boolean
  isMultiselect?: boolean
  config?: AttributeConfig
}

/**
 * Create attribute result
 */
export interface CreateAttributeResult {
  attributeId: string
  slug: string
  success: boolean
}

/**
 * Update attribute parameters
 */
export interface UpdateAttributeParams {
  objectSlug: string
  attributeSlug: string
  title?: string
  description?: string
  isRequired?: boolean
  isArchived?: boolean
  config?: AttributeConfig
}

/**
 * Update attribute result
 */
export interface UpdateAttributeResult {
  attributeId: string
  slug: string
  success: boolean
}

// ============================================================================
// Note Types
// ============================================================================

/**
 * Note format options
 */
export type NoteFormat = 'plaintext' | 'markdown'

/**
 * Create note parameters
 */
export interface CreateNoteParams {
  parentObject: string // Object slug: 'people', 'companies', etc.
  parentRecordId: string // Record ID to attach note to
  title: string // Note title (plaintext only)
  format: NoteFormat // Content format
  content: string // Note body
  createdAt?: string // Optional ISO timestamp override
}

/**
 * Create note result
 */
export interface CreateNoteResult {
  noteId: string
  success: boolean
}

/**
 * List notes parameters
 */
export interface ListNotesParams {
  parentObject: string // Object slug: 'people', 'companies', 'deals', etc.
  parentRecordId: string // Record ID to list notes for
}

/**
 * List notes result
 */
export interface ListNotesResult {
  notes: Array<{ noteId: string; title: string; createdAt: string }>
  success: boolean
}

/**
 * Delete note parameters
 */
export interface DeleteNoteParams {
  noteId: string
}

/**
 * Delete note result
 */
export interface DeleteNoteResult {
  noteId: string
  success: boolean
}
