import * as sdk from '@botpress/sdk'
import hitl from './bp_modules/hitl'

export const INTEGRATION_NAME = 'zendesk-messaging-hitl'

export default new sdk.IntegrationDefinition({
  name: INTEGRATION_NAME,
  version: '1.1.2',
  title: 'Zendesk Messaging HITL',
  description: 'This integration allows your bot to use Sunshine Conversations (Sunco) as a HITL Provider for Zendesk',
  icon: 'icon.svg',
  readme: 'hub.md',
  configuration: {
    identifier: {
      linkTemplateScript: 'linkTemplate.vrl',
      required: true,
    },
    schema: sdk.z.object({}),
  },
  identifier: {
    extractScript: 'extract.vrl',
  },
  states: {
    credentials: {
      type: 'integration',
      schema: sdk.z.object({
        token: sdk.z
          .string()
          .optional()
          .title('Token')
          .describe('The bearer token obtained after completing the OAuth flow'),
        appId: sdk.z.string().optional().title('App ID').describe('The registered app ID'),
        subdomain: sdk.z
          .string()
          .optional()
          .title('Subdomain')
          .describe('The subdomain of the authenticated app if there is one'),
      }),
    },
  },
  channels: {},
  events: {},
  user: {
    tags: {
      id: {
        title: 'User ID',
        description: 'The Sunshine Conversations user ID',
      },
      email: {
        title: 'Email',
        description: 'The email address of the Sunshine Conversations user',
      },
    },
  },
  entities: {
    hitlConversation: {
      title: 'HITL Conversation',
      description: 'A support request',
      schema: sdk.z.object({
        priority: sdk.z
          .enum(['Low', 'Normal', 'High', 'Urgent'])
          .title('Priority')
          .describe('Priority of the conversation. Leave empty for default priority.')
          .optional(),
        originSourceType: sdk.z
          .enum([
            'android',
            'ios',
            'web',
            'sdk',
            'apple',
            'googlercs',
            'instagram',
            'kakao',
            'line',
            'mailgun',
            'messagebird',
            'messenger',
            'slackconnect',
            'telegram',
            'twilio',
            'twitter',
            'viber',
            'wechat',
            'whatsapp',
          ])
          .title('Origin Source Type')
          .describe(
            'The channel where this conversation originated from. Leave empty to default to SUNSHINE CONVERSATIONS API.'
          )
          .optional(),
        organizationId: sdk.z
          .string()
          .title('Organization Id')
          .describe('The organization ID to assign the ticket to.')
          .optional(),
        brandId: sdk.z.string().title('Brand Id').describe('The brand ID to assign the ticket to.').optional(),
        groupId: sdk.z.string().title('Group Id').describe('The group ID to assign the ticket to.').optional(),
        assigneeId: sdk.z.string().title('Assignee Id').describe('The assignee ID to assign the ticket to.').optional(),
        tags: sdk.z.array(sdk.z.string()).title('Tags').describe('Tags to add to the ticket.').optional(),
        customTicketFields: sdk.z
          .array(
            sdk.z.object({
              id: sdk.z.string().min(1).title('Custom Field ID').describe('The ID of the custom field in Zendesk'),
              value: sdk.z
                .string()
                .min(1)
                .title('Custom Field Value')
                .describe('The value to set for this custom field'),
            })
          )
          .title('Custom Ticket Fields')
          .describe(
            'Custom ticket fields to set on the ticket. The id should be the ticket field ID, and the value should be the field value. Example: { "40033266756891": "value" }'
          )
          .optional(),
        additionalMetadata: sdk.z
          .array(
            sdk.z.object({
              key: sdk.z.string().min(1).title('Metadata Key').describe('The metadata key to add'),
              value: sdk.z.string().min(1).title('Metadata Value').describe('The value to set for this metadata key'),
            })
          )
          .title('Additional Metadata')
          .describe('Additional metadata fields to add directly to the metadata object.')
          .optional(),
      }),
    },
  },
  attributes: {
    category: 'Customer Support',
    repo: 'botpress',
  },
  secrets: {
    CLIENT_ID: {
      description: 'Botpress SunCo OAuth Client ID',
    },
    CLIENT_SECRET: {
      description: 'Botpress SunCo OAuth Client Secret',
    },
    MARKETPLACE_BOT_NAME: {
      description: 'The name of the marketplace bot',
    },
    MARKETPLACE_ORG_ID: {
      description: 'The ID of the marketplace organization',
    },
    MARKETPLACE_BOT_ID: {
      description: 'The bot ID for the Zendesk marketplace',
    },
  },
}).extend(hitl, (self) => ({
  entities: { hitlSession: self.entities.hitlConversation },
  channels: {
    hitl: {
      title: 'Zendesk Messaging',
      description: 'Zendesk Messaging HITL',
      conversation: {
        tags: {
          id: { title: 'Sunco Conversation Id', description: 'Sunco Conversation Id' },
        },
      },
      message: {
        tags: {
          id: {
            title: 'Sunco Message ID',
            description: 'The ID of the message in Sunco',
          },
        },
      },
    },
  },
}))
