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

export const salesforceManifest: IntegrationManifest = {
  id: "salesforce",
  version: "1.0.0",
  name: "Salesforce",
  description: "Sync Salesforce accounts and opportunities. Push won quotes as opportunity updates.",
  category: "crm",
  vendor: "Salesforce",
  source: "builtin",
  homepage: "https://www.salesforce.com",
  docsUrl: "https://developer.salesforce.com/docs/apis",
  icon: "Salesforce",
  tags: ["crm", "sales", "oauth"],
  connection: {
    baseUrl: "{{config.instanceUrl}}",
    allowedHosts: ["*.salesforce.com", "*.force.com", "login.salesforce.com", "test.salesforce.com"],
    auth: {
      type: "oauth2",
      flow: "authorization_code",
      authUrl: "https://login.salesforce.com/services/oauth2/authorize",
      tokenUrl: "https://login.salesforce.com/services/oauth2/token",
      scopes: ["api", "refresh_token", "offline_access"],
      scopeSeparator: " ",
      tokenPlacement: "header",
      tokenParamName: "Authorization",
      tokenPrefix: "Bearer ",
      pkce: true,
      extraAuthParams: {},
      extraTokenParams: {},
      tokenRequestStyle: "form",
    },
    fields: [
      {
        key: "instanceUrl",
        label: "Instance URL",
        type: "url",
        required: true,
        helpText: "Returned by Salesforce on the OAuth callback (e.g. https://yourorg.my.salesforce.com).",
      },
      {
        key: "apiVersion",
        label: "API version",
        type: "string",
        default: "v59.0",
      },
    ],
    test: { method: "GET", path: "/services/data/{{config.apiVersion}}/sobjects", expectStatus: [200], headers: {} },
  },
  capabilities: {
    actions: [
      {
        id: "find_account_by_name",
        name: "Find account by name",
        description: "Look up a Salesforce account by exact name.",
        input: [{ name: "name", type: "string", description: "Account name", required: true }],
        output: { select: "$.records[0]" },
        request: {
          method: "GET",
          path: "/services/data/{{config.apiVersion}}/query",
          query: { q: "SELECT Id, Name, Phone, Website FROM Account WHERE Name = '{{input.name}}' LIMIT 1" },
          headers: {},
          body: undefined,
          bodyEncoding: "none",
          timeoutMs: 30_000,
          retry: { maxAttempts: 3, initialDelayMs: 500, backoff: "exponential", retryOnStatus: [408, 429, 500, 502, 503, 504] },
        },
        mutates: false,
        requiresConfirmation: false,
        tags: ["read"],
      },
      {
        id: "create_opportunity",
        name: "Create opportunity",
        description: "Create a Salesforce opportunity.",
        input: [
          { name: "accountId", type: "string", description: "Account Id", required: true },
          { name: "name", type: "string", description: "Opportunity name", required: true },
          { name: "amount", type: "number", description: "Amount", required: true },
          { name: "closeDate", type: "string", description: "Close date (YYYY-MM-DD)", required: true },
          { name: "stage", type: "string", description: "Stage name", required: false, default: "Qualification" },
        ],
        output: { select: "$", map: { id: "$.id" } },
        request: {
          method: "POST",
          path: "/services/data/{{config.apiVersion}}/sobjects/Opportunity",
          query: {},
          headers: {},
          body: {
            AccountId: "{{input.accountId}}",
            Name: "{{input.name}}",
            Amount: "{{input.amount}}",
            CloseDate: "{{input.closeDate}}",
            StageName: "{{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: ["write"],
      },
    ],
    triggers: [],
    syncs: [],
  },
  ui: { sections: [] },
};
