import {
  IAuthenticateGeneric,
  ICredentialTestRequest,
  ICredentialType,
  INodeProperties,
} from 'n8n-workflow';

export class GoHighLevelOAuth2Api implements ICredentialType {
  name = 'goHighLevelOAuth2Api';
  extends = ['oAuth2Api'];
  displayName = 'GoHighLevel OAuth2 API';
  documentationUrl = 'https://highlevel.stoplight.io/docs/integrations/docs/Authentication/oauth2.md';
  properties: INodeProperties[] = [
    {
      displayName: 'Grant Type',
      name: 'grantType',
      type: 'hidden',
      default: 'authorizationCode',
    },
    {
      displayName: 'Authorization URL',
      name: 'authUrl',
      type: 'options',
      default: 'https://marketplace.leadconnectorhq.com/oauth/chooselocation',
      required: true,
      options: [
        {
          name: 'White-Label',
          value: 'https://marketplace.leadconnectorhq.com/oauth/chooselocation',
        },
        {
          name: 'Standard',
          value: 'https://marketplace.gohighlevel.com/oauth/chooselocation',
        },
      ],
    },
    {
      displayName: 'Scope',
      name: 'scope',
      type: 'string',
      hint: "Separate scopes by space, scopes needed for node: 'locations.readonly contacts.readonly contacts.write opportunities.readonly opportunities.write users.readonly'",
      default: "contacts.readonly contacts.write locations.write locations.readonly funnels/redirect.readonly funnels/page.readonly funnels/funnel.readonly funnels/pagecount.readonly funnels/redirect.write products.readonly products.write products/prices.readonly products/prices.write users.readonly users.write opportunities.readonly opportunities.write locations/customFields.readonly locations/customFields.write",
      required: true,
    },
    {
      displayName: 'Access Token URL',
      name: 'accessTokenUrl',
      type: 'hidden',
      default: 'https://services.leadconnectorhq.com/oauth/token',
    },
    {
      displayName: 'Auth URI Query Parameters',
      name: 'authQueryParameters',
      type: 'hidden',
      default: '',
    },
    {
      displayName: 'Authentication',
      name: 'authentication',
      type: 'hidden',
      default: 'body',
    },
  ];

  authenticate: IAuthenticateGeneric = {
    type: 'generic',
    properties: {
      headers: {
        Authorization: '=Bearer {{$credentials.accessToken}}',
        Version: '2021-07-28',
        Location: '={{$credentials.locationId}}',
      },
    },
  };

  test: ICredentialTestRequest = {
    request: {
      baseURL: 'https://services.leadconnectorhq.com',
      url: '/locations/{{$credentials.locationId}}',
      method: 'GET',
    },
  };
}
