import { INodeProperties } from 'n8n-workflow';

const resource = 'opportunity';

enum EOpportunityOperation {
  CREATE = 'create',
  DELETE = 'delete',
  FIND_OR_CREATE = 'findOrCreate',
  GET = 'get',
  GET_ALL = 'getAll',
  UPDATE = 'update',
  UPDATE_OR_CREATE = 'updateOrCreate',
  UPDATE_STATUS = 'updateStatus',
  UPDATE_STAGE = 'updateStage',
}

export const opportunityOperations: INodeProperties[] = [
  {
    displayName: 'Operation',
    name: 'operation',
    type: 'options',
    noDataExpression: true,
    displayOptions: {
      show: {
        resource: ['opportunity'],
      },
    },
    options: [
      {
        name: 'Create',
        value: EOpportunityOperation.CREATE,
        description: 'Create an opportunity',
        action: 'Create an opportunity',
      },
      {
        name: 'Delete',
        value: EOpportunityOperation.DELETE,
        description: 'Delete an opportunity',
        action: 'Delete an opportunity',
      },
      {
        name: 'Find or Create',
        value: EOpportunityOperation.FIND_OR_CREATE,
        description: 'Find or create an opportunity',
        action: 'Find or create an opportunity',
      },
      {
        name: 'Get',
        value: EOpportunityOperation.GET,
        description: 'Get an opportunity',
        action: 'Get an opportunity',
      },
      {
        name: 'Get Many',
        value: EOpportunityOperation.GET_ALL,
        description: 'Get many opportunities',
        action: 'Get many opportunities',
      },
      {
        name: 'Update',
        value: EOpportunityOperation.UPDATE,
        description: 'Update an opportunity',
        action: 'Update an opportunity',
      },
      {
        name: 'Update or Create',
        value: EOpportunityOperation.UPDATE_OR_CREATE,
        description: 'Update or create an opportunity',
        action: 'Update or create an opportunity',
      },
      {
        name: 'Update Stage',
        value: EOpportunityOperation.UPDATE_STAGE,
        description: 'Move opportunity to a different stage',
        action: 'Move opportunity to a different stage',
      },
      {
        name: 'Update Status',
        value: EOpportunityOperation.UPDATE_STATUS,
        description: 'Update opportunity status',
        action: 'Update opportunity status',
      },
    ],
    default: 'getAll',
  },
];

export const opportunityFields: INodeProperties[] = [
  {
    displayName: 'Opportunity ID',
    name: 'opportunityId',
    type: 'string',
    default: '',
    required: true,
    displayOptions: {
      show: {
        resource: [resource],
        operation: ['get', 'update', 'delete', 'updateStage', 'updateStatus'],
      },
    },
  },
  {
    displayName: 'Name',
    name: 'name',
    type: 'string',
    default: '',
    required: true,
    displayOptions: {
      show: {
        resource: [resource],
        operation: ['create'],
      },
    },
  },
  {
    displayName: 'Pipeline ID',
    name: 'pipelineId',
    type: 'string',
    default: '',
    displayOptions: {
      show: {
        resource: [resource],
        operation: ['create'],
      },
    },
  },
  { // pipeline stage id
    displayName: 'Pipeline Stage ID',
    name: 'pipelineStageId',
    type: 'string',
    default: '',
    required: true,
    displayOptions: {
      show: {
        resource: [resource],
        operation: ['create', 'updateStage'],
      },
    },
  },
  { // status
    displayName: 'Status',
    name: 'status',
    type: 'options',
    options: [
      {
        name: 'Open',
        value: 'open',
      },
      {
        name: 'Won',
        value: 'won',
      },
      {
        name: 'Lost',
        value: 'lost',
      },
    ],
    default: 'open',
    displayOptions: {
      show: {
        resource: [resource],
        operation: ['create', 'updateStatus'],
      },
    },
  },
  { // contact id
    displayName: 'Contact ID',
    name: 'contactId',
    type: 'string',
    default: '',
    displayOptions: {
      show: {
        resource: [resource],
        operation: [EOpportunityOperation.CREATE, EOpportunityOperation.UPDATE_OR_CREATE, EOpportunityOperation.FIND_OR_CREATE],
      },
    },
  },
  { // limit
    displayName: 'Limit',
    name: 'limit',
    type: 'number',
    typeOptions: {
      minValue: 1,
    },
    default: 50,
    description: 'Max number of results to return',

    displayOptions: {
      show: {
        resource: [resource],
        operation: ['getAll'],
      },
    },
  },
  { // return all
    displayName: 'Return All',
    name: 'returnAll',
    type: 'boolean',
    default: false,
    description: 'Whether to return all results or only up to a given limit',
    displayOptions: {
      show: {
        resource: [resource],
        operation: ['getAll'],
      },
    },
  },
  { // filters
    displayName: 'Filters',
    name: 'filters',
    type: 'collection',
    placeholder: 'Add Filter',
    default: {},
    displayOptions: {
      show: {
        resource: ['opportunity'],
        operation: ['getAll'],
      },
    },
    options: [
      {
        displayName: 'Assigned To',
        name: 'assigned_to',
        type: 'string',
        default: '',
        description: 'Filter by assigned user ID',
      },
      {
        displayName: 'Campaign ID',
        name: 'campaignId',
        type: 'string',
        default: '',
        description: 'Filter by campaign ID',
      },
      {
        displayName: 'Contact ID',
        name: 'contact_id',
        type: 'string',
        default: '',
        description: 'Filter by contact ID',
      },
      {
        displayName: 'Country',
        name: 'country',
        type: 'string',
        default: '',
      },
      {
        displayName: 'End Date',
        name: 'endDate',
        type: 'string',
        default: '',
        description: 'Format: mm-dd-yyyy',
      },
      {
        displayName: 'Include Calendar Events',
        name: 'getCalendarEvents',
        type: 'boolean',
        default: false,
        description: 'Whether to include calendar events in contact',
      },
      {
        displayName: 'Include Notes',
        name: 'getNotes',
        type: 'boolean',
        default: false,
        description: 'Whether to include notes in contact',
      },
      {
        displayName: 'Include Tasks',
        name: 'getTasks',
        type: 'boolean',
        default: false,
        description: 'Whether to include tasks in contact',
      },
      {
        displayName: 'Limit',
        name: 'limit',
        type: 'number',
        typeOptions: {
          minValue: 1,
        },
        default: 50,
        description: 'Max number of results to return',
      },
      {
        displayName: 'Page',
        name: 'page',
        type: 'number',
        default: 1,
      },
      {
        displayName: 'Pipeline ID',
        name: 'pipeline_id',
        type: 'string',
        default: '',
      },
      {
        displayName: 'Pipeline Stage ID',
        name: 'pipeline_stage_id',
        type: 'string',
        default: '',
      },
      {
        displayName: 'Search Query',
        name: 'q',
        type: 'string',
        default: '',
        description: 'Search term (e.g., email, name)',
      },
      {
        displayName: 'Start Date',
        name: 'date',
        type: 'string',
        default: '',
        description: 'Format: mm-dd-yyyy',
      },
      {
        displayName: 'Status',
        name: 'status',
        type: 'options',
        options: [
          {
            name: 'Abandoned',
            value: 'abandoned',
          },
          {
            name: 'All',
            value: 'all',
          },
          {
            name: 'Lost',
            value: 'lost',
          },
          {
            name: 'Open',
            value: 'open',
          },
          {
            name: 'Won',
            value: 'won',
          },
        ],
        default: 'all',
      },
    ],
  },
  { // additional fields
    displayName: 'Additional Fields',
    name: 'additionalFields',
    type: 'collection',
    placeholder: 'Add Field',
    default: {},
    displayOptions: {
      show: {
        resource: ['opportunity'],
        operation: [
          EOpportunityOperation.CREATE,
          EOpportunityOperation.UPDATE,
          EOpportunityOperation.UPDATE_OR_CREATE,
          EOpportunityOperation.FIND_OR_CREATE
        ],
      },
    },
    options: [
      { // contact id
        displayName: 'Contact ID',
        name: 'contactId',
        type: 'string',
        default: '',
      },
      {
        displayName: 'Custom Fields',
        name: 'customFields',
        placeholder: 'Add Custom Field',
        type: 'fixedCollection',
        typeOptions: {
          multipleValues: true,
        },
        default: {},
        options: [
          {
            name: 'values',
            displayName: 'Values',
            values: [
              {
                displayName: 'Field Name or ID',
                name: 'id',
                type: 'options',
                typeOptions: {
                  loadOptionsMethod: 'getOpportunityCustomFields',
                },
                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: 'Monetary Value',
        name: 'monetaryValue',
        type: 'number',
        default: 0,
        description: 'Monetary value of the opportunity',
      },
      {
        displayName: 'Name',
        name: 'name',
        type: 'string',
        default: '',
        description: 'Name of the opportunity',
      },
      {
        displayName: 'Pipeline ID',
        name: 'pipelineId',
        type: 'string',
        default: '',
        description: 'ID of the pipeline this opportunity belongs to',
      },
      {
        displayName: 'Pipeline Stage ID',
        name: 'pipelineStageId',
        type: 'string',
        default: '',
        description: 'ID of the pipeline stage this opportunity is in',
      },
    ],
  },
];
