import { LinearIcon } from '@/components/icons'
import {
  buildLinearV2SubBlocks,
  linearSetupInstructions,
  userOutputs,
} from '@/triggers/linear/utils'
import type { TriggerConfig } from '@/triggers/types'

export const linearWebhookTrigger: TriggerConfig = {
  id: 'linear_webhook',
  name: 'Linear Webhook',
  provider: 'linear',
  description:
    'Trigger workflow from Linear events you select when creating the webhook in Linear (not guaranteed to be every model or event type).',
  version: '1.0.0',
  icon: LinearIcon,

  subBlocks: [
    {
      id: 'webhookUrlDisplay',
      title: 'Webhook URL',
      type: 'short-input',
      readOnly: true,
      showCopyButton: true,
      useWebhookUrl: true,
      placeholder: 'Webhook URL will be generated',
      mode: 'trigger',
      condition: {
        field: 'selectedTriggerId',
        value: 'linear_webhook',
      },
    },
    {
      id: 'webhookSecret',
      title: 'Webhook Secret',
      type: 'short-input',
      placeholder: 'Enter a strong secret',
      description: 'Validates that webhook deliveries originate from Linear.',
      password: true,
      required: false,
      mode: 'trigger',
      condition: {
        field: 'selectedTriggerId',
        value: 'linear_webhook',
      },
    },
    {
      id: 'triggerSave',
      title: '',
      type: 'trigger-save',
      hideFromPreview: true,
      mode: 'trigger',
      triggerId: 'linear_webhook',
      condition: {
        field: 'selectedTriggerId',
        value: 'linear_webhook',
      },
    },
    {
      id: 'triggerInstructions',
      title: 'Setup Instructions',
      hideFromPreview: true,
      type: 'text',
      defaultValue: linearSetupInstructions(
        'all events',
        'When you select resource types in Linear, you receive those data-change events only (see <a href="https://linear.app/developers/webhooks" target="_blank" rel="noopener noreferrer">Linear webhooks</a> for supported models). This is not every possible Linear event. Use the <code>type</code> and <code>action</code> fields in the payload to filter further.'
      ),
      mode: 'trigger',
      condition: {
        field: 'selectedTriggerId',
        value: 'linear_webhook',
      },
    },
  ],

  outputs: {
    action: {
      type: 'string',
      description: 'Action performed (create, update, remove)',
    },
    type: {
      type: 'string',
      description: 'Entity type (Issue, Comment, Project, Cycle, IssueLabel, ProjectUpdate, etc.)',
    },
    webhookId: {
      type: 'string',
      description: 'Webhook ID',
    },
    webhookTimestamp: {
      type: 'number',
      description: 'Webhook timestamp (milliseconds)',
    },
    organizationId: {
      type: 'string',
      description: 'Organization ID',
    },
    createdAt: {
      type: 'string',
      description: 'Event creation timestamp',
    },
    url: {
      type: 'string',
      description: 'URL of the subject entity in Linear (top-level webhook payload)',
    },
    actor: userOutputs,
    data: {
      type: 'object',
      description: 'Complete entity data object',
    },
    updatedFrom: {
      type: 'object',
      description: 'Previous values for changed fields (only present on update)',
    },
  },

  webhook: {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Linear-Event': 'Issue',
      'Linear-Delivery': '234d1a4e-b617-4388-90fe-adc3633d6b72',
      'Linear-Signature': '766e1d90a96e2f5ecec342a99c5552999dd95d49250171b902d703fd674f5086',
      'User-Agent': 'Linear-Webhook',
    },
  },
}

export const linearWebhookV2Trigger: TriggerConfig = {
  id: 'linear_webhook_v2',
  name: 'Linear Webhook',
  provider: 'linear',
  description:
    'Trigger workflow from Linear events you select when creating the webhook in Linear (not guaranteed to be every model or event type).',
  version: '2.0.0',
  icon: LinearIcon,
  subBlocks: buildLinearV2SubBlocks({
    triggerId: 'linear_webhook_v2',
    eventType: 'All Events',
    additionalNotes:
      'This webhook will receive all Linear events. Use the <code>type</code> and <code>action</code> fields in the payload to filter and handle different event types.',
  }),
  outputs: {
    action: {
      type: 'string',
      description: 'Action performed (create, update, remove)',
    },
    type: {
      type: 'string',
      description: 'Entity type (Issue, Comment, Project, Cycle, IssueLabel, ProjectUpdate, etc.)',
    },
    webhookId: {
      type: 'string',
      description: 'Webhook ID',
    },
    webhookTimestamp: {
      type: 'number',
      description: 'Webhook timestamp (milliseconds)',
    },
    organizationId: {
      type: 'string',
      description: 'Organization ID',
    },
    createdAt: {
      type: 'string',
      description: 'Event creation timestamp',
    },
    url: {
      type: 'string',
      description: 'URL of the subject entity in Linear (top-level webhook payload)',
    },
    actor: userOutputs,
    data: {
      type: 'object',
      description: 'Complete entity data object',
    },
    updatedFrom: {
      type: 'object',
      description: 'Previous values for changed fields (only present on update)',
    },
  },
  webhook: {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Linear-Event': 'Issue',
      'Linear-Delivery': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
      'Linear-Signature': 'sha256...',
      'User-Agent': 'Linear-Webhook',
    },
  },
}
