import { INodeProperties } from 'n8n-workflow';

enum EOperation {
  getAll = 'getAll',
  create = 'create',
  get = 'get',
  update = 'update',
  delete = 'delete',
  findOrCreate = 'findOrCreate',
  updateOrCreate = 'updateOrCreate',

  find = 'find',

  addTag = 'addTag',
  removeTag = 'removeTag',
  addToSequence = 'addToSequence',
  removeFromSequence = 'removeFromSequence',
}

const resource = 'contact';


export const contactOperations: INodeProperties[] = [
  {
    displayName: 'Operation',
    name: 'operation',
    type: 'options',
    noDataExpression: true,
    displayOptions: {
      show: {
        resource: [resource],
      },
    },
    options: [
      {
        name: 'Add Tag', // ✅ or ❌
        value: EOperation.addTag,
        description: 'Add a tag to a contact',
        action: 'Add a tag to a contact',
      },
      {
        name: 'Add to Sequence', // ✅ or ❌
        value: EOperation.addToSequence,
        description: 'Add contact to a sequence',
        action: 'Add contact to a sequence',
      },
      {
        name: 'Create', // ✅ or ❌
        value: EOperation.create,
        description: 'Create a contact',
        action: 'Create a contact',
      },
      {
        name: 'Delete', // ✅ or ❌
        value: EOperation.delete,
        description: 'Delete a contact',
        action: 'Delete a contact',
      },
      {
        name: 'Find', // ✅ or ❌
        value: EOperation.find,
        description: 'Find a contact',
        action: 'Find a contact',
      },
      {
        name: 'Find or Create', // ✅ or ❌
        value: EOperation.findOrCreate,
        description: 'Find or create a contact',
        action: 'Find or create a contact',
      },
      {
        name: 'Get', // ✅ or ❌
        value: EOperation.get,
        description: 'Get a contact',
        action: 'Get a contact',
      },
      {
        name: 'Get Many', // ✅ or ❌
        value: EOperation.getAll,
        description: 'Get many contacts',
        action: 'Get many contacts',
      },
      {
        name: 'Remove From Sequence', // ✅ or ❌
        value: EOperation.removeFromSequence,
        description: 'Remove contact from a sequence',
        action: 'Remove contact from a sequence',
      },
      {
        name: 'Remove Tag', // ✅ or ❌
        value: EOperation.removeTag,
        description: 'Remove a tag from a contact',
        action: 'Remove a tag from a contact',
      },
      {
        name: 'Update', // ✅ or ❌
        value: EOperation.update,
        description: 'Update a contact',
        action: 'Update a contact',
      },
      {
        name: 'Update or Create', // ✅ or ❌
        value: EOperation.updateOrCreate,
        description: 'Update or create a contact',
        action: 'Update or create a contact',
      },
    ],
    default: 'findOrCreate',
  },
];

export const contactFields: INodeProperties[] = [
  {
    displayName: `Contact ID`,
    name: `contactId`,
    type: 'string',
    required: true,
    displayOptions: {
      show: {
        resource: [resource],
        operation: [EOperation.update, EOperation.delete, EOperation.get],
      },
    },
    default: '',
    description: 'The ID of the contact',
  },
  { // email
    displayName: 'Email',
    name: 'email',
    type: 'string',
    placeholder: 'name@email.com',
    default: '',
    displayOptions: {
      show: {
        resource: [resource],
        operation: [
          EOperation.create,
          EOperation.update,
          EOperation.findOrCreate,
          EOperation.updateOrCreate,
          EOperation.find,
        ],
      },
    },
  },
  { // phone
    displayName: 'Phone',
    name: 'phone',
    type: 'string',
    default: '',
    displayOptions: {
      show: {
        resource: [resource],
        operation: [
          EOperation.create,
          EOperation.update,
          EOperation.findOrCreate,
          EOperation.updateOrCreate,
        ],
      },
    },
  },
  { // tags
    displayName: 'Tags',
    name: 'tags',
    type: 'string',
    default: '',
    displayOptions: {
      show: {
        resource: [resource],
        operation: [EOperation.addTag, EOperation.removeTag],
      },
    },
  },
  { // limit
    displayName: 'Limit',
    name: 'limit',
    type: 'number',
    displayOptions: {
      show: {
        resource: [resource],
        operation: [EOperation.getAll],
        returnAll: [false],
      },
    },
    typeOptions: {
      minValue: 1,
    },
    default: 50,
    description: 'Max number of results to return',
  },
  { // sequence
    displayName: 'Sequence ID',
    name: 'sequenceId',
    type: 'string',
    default: '',
    displayOptions: {
      show: {
        resource: [resource],
        operation: [EOperation.addToSequence, EOperation.removeFromSequence],
      },
    },
  },
  { // return all
    displayName: 'Return All',
    name: 'returnAll',
    type: 'boolean',
    displayOptions: {
      show: {
        resource: [resource],
        operation: [EOperation.getAll],
      },
    },
    default: false,
    description: 'Whether to return all results or only up to a given limit',
  },
  { // filters
    displayName: 'Filters',
    name: 'filters',
    type: 'collection',
    placeholder: 'Add Filter',
    default: {},
    displayOptions: {
      show: {
        resource: [resource],
        operation: [EOperation.getAll],
      },
    },
    options: [
      {
        displayName: 'Created After',
        name: 'createdAfter',
        type: 'dateTime',
        default: '',
        description: 'Filter results created after specified date',
      },
      {
        displayName: 'Created Before',
        name: 'createdBefore',
        type: 'dateTime',
        default: '',
        description: 'Filter results created before specified date',
      },
      {
        displayName: 'Updated After',
        name: 'updatedAfter',
        type: 'dateTime',
        default: '',
        description: 'Filter results updated after specified date',
      },
      {
        displayName: 'Updated Before',
        name: 'updatedBefore',
        type: 'dateTime',
        default: '',
        description: 'Filter results updated before specified date',
      },
    ],
  },
  { // additional fields
    displayName: 'Additional Fields',
    name: 'additionalFields',
    type: 'collection',
    placeholder: 'Add Field',
    default: {},
    displayOptions: {
      show: {
        resource: [resource],
        operation: [
          EOperation.create,
          EOperation.update,
          EOperation.updateOrCreate,
          EOperation.findOrCreate,
        ],
      },
    },
    options: [
      {
        displayName: 'Address',
        name: 'address1',
        type: 'string',
        default: '',
        description: 'Street address of the contact',
      },
      {
        displayName: 'City',
        name: 'city',
        type: 'string',
        default: '',
        description: 'City of the contact',
      },
      {
        displayName: 'Custom Fields',
        name: 'customFields',
        placeholder: 'Add Custom Field',
        type: 'fixedCollection',
        typeOptions: {
          multipleValues: true,
        },
        default: {},
        options: [
          {
            displayName: 'Values',
            name: 'values',
            values: [
              {
                displayName: 'Field Name or ID',
                name: 'id',
                type: 'options',
                typeOptions: {
                  loadOptionsMethod: 'getContactCustomFields',
                },
                default: '',
                description: 'Name or ID of the custom field. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.',
              },
              {
                displayName: 'Field Value',
                name: 'field_value',
                type: 'string',
                default: '',
                description: 'Value of the custom field',
              },
            ],
          },
        ],
      },
      {
        displayName: 'Description',
        name: 'description',
        type: 'string',
        typeOptions: {
          rows: 4,
        },
        default: '',
        description: 'Description of the contact',
      },
      {
        displayName: 'Do Not Disturb',
        name: 'dnd',
        type: 'boolean',
        default: false,
        description: 'Whether the contact is marked as do not disturb',
      },
      {
        displayName: 'Email',
        name: 'email',
        type: 'string',
        placeholder: 'name@email.com',
        default: '',
        description: 'Email of the contact',
      },
      {
        displayName: 'First Name',
        name: 'firstName',
        type: 'string',
        default: '',
        description: 'First name of the contact',
      },
      {
        displayName: 'Last Name',
        name: 'lastName',
        type: 'string',
        default: '',
        description: 'Last name of the contact',
      },
      {
        displayName: 'Name',
        name: 'name',
        type: 'string',
        default: '',
        description: 'Name of the contact',
      },
      {
        displayName: 'Phone',
        name: 'phone',
        type: 'string',
        default: '',
        description: 'Phone number of the contact',
      },
      {
        displayName: 'Postal Code',
        name: 'postalCode',
        type: 'string',
        default: '',
        description: 'Postal code of the contact',
      },
      {
        displayName: 'State',
        name: 'state',
        type: 'string',
        default: '',
        description: 'State of the contact',
      },
      {
        displayName: 'Timezone',
        name: 'timezone',
        type: 'string',
        default: '',
        description: 'Timezone of the contact',
      },
      {
        displayName: 'Website',
        name: 'website',
        type: 'string',
        default: '',
        description: 'Website of the contact',
      },
    ],
  },
];
