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

export const procoreManifest: IntegrationManifest = {
  id: "procore",
  version: "1.0.0",
  name: "Procore",
  description: "Sync Procore projects, push RFIs and change orders, pull daily logs and drawings.",
  category: "construction",
  vendor: "Procore Technologies",
  source: "builtin",
  homepage: "https://www.procore.com",
  docsUrl: "https://developers.procore.com",
  icon: "Procore",
  tags: ["construction", "pm", "oauth"],
  connection: {
    baseUrl: "{{config.regionUrl}}",
    allowedHosts: [
      "api.procore.com",
      "sandbox.procore.com",
      "api-monthly.procore.com",
      "login.procore.com",
      "login-sandbox.procore.com",
    ],
    auth: {
      type: "oauth2",
      flow: "authorization_code",
      authUrl: "https://login.procore.com/oauth/authorize",
      tokenUrl: "https://login.procore.com/oauth/token",
      scopes: [],
      scopeSeparator: " ",
      tokenPlacement: "header",
      tokenParamName: "Authorization",
      tokenPrefix: "Bearer ",
      pkce: false,
      extraAuthParams: {},
      extraTokenParams: {},
      tokenRequestStyle: "form",
    },
    fields: [
      {
        key: "regionUrl",
        label: "API base URL",
        type: "select",
        required: true,
        default: "https://api.procore.com",
        options: [
          { value: "https://api.procore.com",         label: "US production" },
          { value: "https://sandbox.procore.com",     label: "Sandbox" },
          { value: "https://api-monthly.procore.com", label: "US monthly (legacy)" },
        ],
      },
      {
        key: "companyId",
        label: "Default Company ID",
        type: "string",
        required: false,
        helpText: "Optional default Procore company id used by actions when not supplied.",
      },
    ],
    test: { method: "GET", path: "/rest/v1.0/companies", expectStatus: [200], headers: {} },
  },
  capabilities: {
    actions: [
      {
        id: "list_projects",
        name: "List projects",
        description: "List Procore projects for a company.",
        input: [{ name: "companyId", type: "string", description: "Procore company id", required: false }],
        output: { select: "$" },
        request: {
          method: "GET",
          path: "/rest/v1.0/projects",
          query: { company_id: "{{input.companyId}}" },
          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_rfi",
        name: "Create RFI",
        description: "Create an RFI on a Procore project.",
        input: [
          { name: "projectId", type: "string", description: "Procore project id", required: true },
          { name: "subject", type: "string", description: "RFI subject", required: true },
          { name: "questionBody", type: "string", description: "RFI question body", required: true },
        ],
        output: { select: "$", map: { id: "$.id", number: "$.number" } },
        request: {
          method: "POST",
          path: "/rest/v1.0/projects/{{input.projectId}}/rfis",
          query: {},
          headers: {},
          body: {
            rfi: {
              subject: "{{input.subject}}",
              question: { body: "{{input.questionBody}}" },
            },
          },
          bodyEncoding: "json",
          timeoutMs: 30_000,
          retry: { maxAttempts: 3, initialDelayMs: 500, backoff: "exponential", retryOnStatus: [408, 429, 500, 502, 503, 504] },
        },
        mutates: true,
        requiresConfirmation: true,
        tags: ["rfi", "write"],
      },
    ],
    triggers: [],
    syncs: [
      {
        id: "projects",
        name: "Projects",
        description: "Pull Procore projects into ExternalRecord for cross-link with Bidwright projects.",
        direction: "pull",
        schedule: "0 */6 * * *",
        request: {
          method: "GET",
          path: "/rest/v1.0/projects",
          query: { company_id: "{{config.companyId}}", per_page: "100", page: "{{cursor}}" },
          headers: {},
          body: undefined,
          bodyEncoding: "none",
          timeoutMs: 30_000,
          retry: { maxAttempts: 3, initialDelayMs: 500, backoff: "exponential", retryOnStatus: [408, 429, 500, 502, 503, 504] },
        },
        cursor: { fromHeader: "X-Next-Page", requestParam: "page" },
        recordsPath: "$",
        externalIdPath: "$.id",
        mapping: {
          name: "$.name",
          number: "$.project_number",
          address: "$.address",
        },
        target: null,
      },
    ],
  },
  ui: { sections: [] },
};
