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

export const microsoft365Manifest: IntegrationManifest = {
  id: "microsoft-365",
  version: "1.0.0",
  name: "Microsoft 365",
  description: "Microsoft Graph integration — send Outlook mail, browse SharePoint/OneDrive, post Teams notifications.",
  category: "documents",
  vendor: "Microsoft",
  source: "builtin",
  homepage: "https://www.microsoft.com/microsoft-365",
  docsUrl: "https://learn.microsoft.com/en-us/graph/",
  icon: "Microsoft",
  tags: ["m365", "graph", "outlook", "sharepoint", "teams", "oauth"],
  connection: {
    baseUrl: "https://graph.microsoft.com/v1.0",
    allowedHosts: ["graph.microsoft.com", "login.microsoftonline.com"],
    auth: {
      type: "oauth2",
      flow: "authorization_code",
      authUrl: "https://login.microsoftonline.com/{{config.tenantId}}/oauth2/v2.0/authorize",
      tokenUrl: "https://login.microsoftonline.com/{{config.tenantId}}/oauth2/v2.0/token",
      scopes: ["offline_access", "User.Read", "Mail.Send", "Files.Read.All", "Sites.Read.All"],
      scopeSeparator: " ",
      tokenPlacement: "header",
      tokenParamName: "Authorization",
      tokenPrefix: "Bearer ",
      pkce: true,
      extraAuthParams: { response_mode: "query", prompt: "consent" },
      extraTokenParams: {},
      tokenRequestStyle: "form",
    },
    fields: [
      {
        key: "tenantId",
        label: "Tenant ID",
        type: "string",
        required: true,
        default: "common",
        helpText: "Azure tenant ID, or 'common' / 'organizations' for multi-tenant apps.",
      },
    ],
    test: { method: "GET", path: "/me", expectStatus: [200], headers: {} },
  },
  capabilities: {
    actions: [
      {
        id: "send_mail",
        name: "Send email",
        description: "Send an email through Outlook on behalf of the connected user.",
        input: [
          { name: "to", type: "string", description: "Recipient email", required: true },
          { name: "subject", type: "string", description: "Subject", required: true },
          { name: "html", type: "string", description: "HTML body", required: true },
          { name: "cc", type: "string", description: "Optional cc", required: false },
        ],
        output: { select: "$" },
        request: {
          method: "POST",
          path: "/me/sendMail",
          query: {},
          headers: {},
          body: {
            message: {
              subject: "{{input.subject}}",
              body: { contentType: "HTML", content: "{{input.html}}" },
              toRecipients: [{ emailAddress: { address: "{{input.to}}" } }],
              ccRecipients: [{ emailAddress: { address: "{{input.cc}}" } }],
            },
            saveToSentItems: true,
          },
          bodyEncoding: "json",
          timeoutMs: 30_000,
          retry: { maxAttempts: 3, initialDelayMs: 500, backoff: "exponential", retryOnStatus: [408, 429, 500, 502, 503, 504] },
        },
        mutates: true,
        requiresConfirmation: true,
        tags: ["email"],
      },
      {
        id: "list_sharepoint_sites",
        name: "List SharePoint sites",
        description: "List SharePoint sites the connected user can access.",
        input: [{ name: "search", type: "string", description: "Search filter", required: false }],
        output: { select: "$.value" },
        request: {
          method: "GET",
          path: "/sites",
          query: { search: "{{input.search}}" },
          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: ["sharepoint"],
      },
    ],
    triggers: [],
    syncs: [],
  },
  ui: {
    sections: [
      { id: "tenant", title: "Azure tenant", description: "Configure the Azure AD tenant before connecting.", fields: [] },
    ],
  },
};
