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

export const slackManifest: IntegrationManifest = {
  id: "slack",
  version: "1.0.0",
  name: "Slack",
  description: "Notify Slack channels on quote events (review complete, awarded, lost). Bidirectional messaging via bot token.",
  category: "comms",
  vendor: "Slack",
  source: "builtin",
  homepage: "https://slack.com",
  docsUrl: "https://api.slack.com",
  icon: "Slack",
  tags: ["chat", "comms", "notifications"],
  connection: {
    baseUrl: "https://slack.com/api",
    allowedHosts: ["slack.com", "hooks.slack.com"],
    auth: {
      type: "oauth2",
      flow: "authorization_code",
      authUrl: "https://slack.com/oauth/v2/authorize",
      tokenUrl: "https://slack.com/api/oauth.v2.access",
      scopes: ["chat:write", "channels:read", "channels:join", "groups:read"],
      scopeSeparator: ",",
      tokenPlacement: "header",
      tokenParamName: "Authorization",
      tokenPrefix: "Bearer ",
      pkce: false,
      extraAuthParams: {},
      extraTokenParams: {},
      tokenRequestStyle: "form",
    },
    fields: [],
    test: { method: "GET", path: "/auth.test", expectStatus: [200], headers: {} },
  },
  capabilities: {
    actions: [
      {
        id: "post_message",
        name: "Post message",
        description: "Post a message to a Slack channel.",
        input: [
          { name: "channel", type: "string", description: "Channel id or name (e.g. C0123 or #estimating)", required: true },
          { name: "text", type: "string", description: "Plain message text", required: true },
          { name: "blocks", type: "array", description: "Block Kit blocks (optional)", required: false },
        ],
        output: { select: "$", map: { ok: "$.ok", ts: "$.ts", channel: "$.channel" } },
        request: {
          method: "POST",
          path: "/chat.postMessage",
          query: {},
          headers: {},
          body: {
            channel: "{{input.channel}}",
            text: "{{input.text}}",
            blocks: "{{input.blocks}}",
          },
          bodyEncoding: "json",
          timeoutMs: 15_000,
          retry: { maxAttempts: 3, initialDelayMs: 500, backoff: "exponential", retryOnStatus: [408, 429, 500, 502, 503, 504] },
        },
        mutates: true,
        requiresConfirmation: false,
        tags: ["chat"],
      },
      {
        id: "list_channels",
        name: "List channels",
        description: "List channels the bot can post to.",
        input: [],
        output: { select: "$.channels" },
        request: {
          method: "GET",
          path: "/conversations.list",
          query: { types: "public_channel,private_channel", limit: "200" },
          headers: {},
          body: undefined,
          bodyEncoding: "none",
          timeoutMs: 15_000,
          retry: { maxAttempts: 3, initialDelayMs: 500, backoff: "exponential", retryOnStatus: [408, 429, 500, 502, 503, 504] },
        },
        mutates: false,
        requiresConfirmation: false,
        tags: ["chat", "read"],
      },
    ],
    triggers: [
      {
        id: "events",
        name: "Slack Events",
        description: "Receives Slack Events API callbacks (mentions, reactions, etc.) signed with the signing secret.",
        type: "webhook",
        verify: "hmac",
        signatureHeader: "X-Slack-Signature",
        timestampHeader: "X-Slack-Request-Timestamp",
        maxSkewSeconds: 300,
        externalIdPath: "$.event_id",
        output: { select: "$" },
      },
    ],
    syncs: [],
  },
  ui: { sections: [] },
};
