import type { IntegrationManifest } from "../manifest/schema.js";

export const hubspotManifest: IntegrationManifest = {
  id: "hubspot",
  version: "1.0.0",
  name: "HubSpot",
  description: "Sync HubSpot contacts and companies with Bidwright customers, push won quotes as deals.",
  category: "crm",
  vendor: "HubSpot",
  source: "builtin",
  homepage: "https://www.hubspot.com",
  docsUrl: "https://developers.hubspot.com",
  icon: "HubSpot",
  tags: ["crm", "sales", "oauth"],
  connection: {
    baseUrl: "https://api.hubapi.com",
    allowedHosts: ["api.hubapi.com", "app.hubspot.com"],
    auth: {
      type: "oauth2",
      flow: "authorization_code",
      authUrl: "https://app.hubspot.com/oauth/authorize",
      tokenUrl: "https://api.hubapi.com/oauth/v1/token",
      scopes: ["crm.objects.contacts.read", "crm.objects.contacts.write", "crm.objects.companies.read", "crm.objects.deals.read", "crm.objects.deals.write"],
      scopeSeparator: " ",
      tokenPlacement: "header",
      tokenParamName: "Authorization",
      tokenPrefix: "Bearer ",
      pkce: false,
      extraAuthParams: {},
      extraTokenParams: {},
      tokenRequestStyle: "form",
    },
    fields: [],
    test: { method: "GET", path: "/account-info/v3/details", expectStatus: [200], headers: {} },
  },
  capabilities: {
    actions: [
      {
        id: "create_contact",
        name: "Create contact",
        description: "Create a HubSpot contact.",
        input: [
          { name: "email", type: "string", description: "Contact email", required: true },
          { name: "firstName", type: "string", description: "First name", required: false },
          { name: "lastName", type: "string", description: "Last name", required: false },
          { name: "company", type: "string", description: "Company name", required: false },
          { name: "phone", type: "string", description: "Phone", required: false },
        ],
        output: { select: "$", map: { id: "$.id", createdAt: "$.createdAt" } },
        request: {
          method: "POST",
          path: "/crm/v3/objects/contacts",
          query: {},
          headers: {},
          body: {
            properties: {
              email: "{{input.email}}",
              firstname: "{{input.firstName}}",
              lastname: "{{input.lastName}}",
              company: "{{input.company}}",
              phone: "{{input.phone}}",
            },
          },
          bodyEncoding: "json",
          timeoutMs: 30_000,
          retry: { maxAttempts: 3, initialDelayMs: 500, backoff: "exponential", retryOnStatus: [408, 429, 500, 502, 503, 504] },
        },
        mutates: true,
        requiresConfirmation: false,
        tags: ["crm", "contact"],
      },
      {
        id: "find_contact_by_email",
        name: "Find contact by email",
        description: "Look up a HubSpot contact by email address.",
        input: [{ name: "email", type: "string", description: "Email to search", required: true }],
        output: { select: "$.results[0]" },
        request: {
          method: "POST",
          path: "/crm/v3/objects/contacts/search",
          query: {},
          headers: {},
          body: {
            filterGroups: [
              { filters: [{ propertyName: "email", operator: "EQ", value: "{{input.email}}" }] },
            ],
            limit: 1,
          },
          bodyEncoding: "json",
          timeoutMs: 30_000,
          retry: { maxAttempts: 3, initialDelayMs: 500, backoff: "exponential", retryOnStatus: [408, 429, 500, 502, 503, 504] },
        },
        mutates: false,
        requiresConfirmation: false,
        tags: ["crm", "contact", "read"],
      },
      {
        id: "create_deal",
        name: "Create deal",
        description: "Create a HubSpot deal — typically called when a Bidwright quote is awarded.",
        input: [
          { name: "name", type: "string", description: "Deal name", required: true },
          { name: "amount", type: "number", description: "Deal amount", required: true },
          { name: "stage", type: "string", description: "Pipeline stage id", required: false, default: "appointmentscheduled" },
        ],
        output: { select: "$", map: { id: "$.id" } },
        request: {
          method: "POST",
          path: "/crm/v3/objects/deals",
          query: {},
          headers: {},
          body: {
            properties: {
              dealname: "{{input.name}}",
              amount: "{{input.amount}}",
              dealstage: "{{input.stage}}",
            },
          },
          bodyEncoding: "json",
          timeoutMs: 30_000,
          retry: { maxAttempts: 3, initialDelayMs: 500, backoff: "exponential", retryOnStatus: [408, 429, 500, 502, 503, 504] },
        },
        mutates: true,
        requiresConfirmation: true,
        tags: ["crm", "deal"],
      },
    ],
    triggers: [],
    syncs: [
      {
        id: "contacts",
        name: "Contacts",
        description: "Pull HubSpot contacts into ExternalRecord for mapping to Bidwright customers.",
        direction: "pull",
        schedule: "0 */6 * * *",
        request: {
          method: "GET",
          path: "/crm/v3/objects/contacts",
          query: { limit: "100", after: "{{cursor}}" },
          headers: {},
          body: undefined,
          bodyEncoding: "none",
          timeoutMs: 30_000,
          retry: { maxAttempts: 3, initialDelayMs: 500, backoff: "exponential", retryOnStatus: [408, 429, 500, 502, 503, 504] },
        },
        cursor: { fromResponse: "$.paging.next.after" },
        recordsPath: "$.results",
        externalIdPath: "$.id",
        mapping: {
          email: "$.properties.email",
          firstName: "$.properties.firstname",
          lastName: "$.properties.lastname",
          company: "$.properties.company",
        },
        target: "Customer",
      },
    ],
  },
  ui: { sections: [] },
};
