import type {
  ConnectorAuthStrategy,
  ConnectorAvailability,
  ConnectorCapability,
  ConnectorCapabilityDefinition,
  ConnectorProvider,
  ConnectorProviderDefinition,
  ConnectorUiMetadata,
} from '../oauth-connection.types'

type ProviderSeed = {
  provider: ConnectorProvider
  displayName: string
  description: string
  authStrategy: ConnectorAuthStrategy
  category: ConnectorUiMetadata['category']
  capability: ConnectorCapability
  capabilityName: string
  capabilityDescription: string
  requiredScopes: readonly string[]
  operations: readonly string[]
  availability: ConnectorAvailability
  priorityRuntime?: boolean
}

function capability(seed: ProviderSeed): ConnectorCapabilityDefinition {
  return {
    provider: seed.provider,
    capability: seed.capability,
    displayName: seed.capabilityName,
    description: seed.capabilityDescription,
    requiredScopes: seed.requiredScopes,
    optionalScopes: [],
    supportedOperations: seed.operations,
    operations: seed.operations.map((operation) => ({
      operation,
      displayName: operation.split('_').map((p) => p[0]?.toUpperCase() + p.slice(1)).join(' '),
      description: `${seed.displayName}: ${operation.replace(/_/g, ' ')}`,
      availability: seed.priorityRuntime ? 'experimental' : seed.availability,
      outputShape: operation.startsWith('list') || operation.startsWith('query') ? 'array' : 'object',
      requiredScopes: seed.requiredScopes,
    })),
  }
}

function provider(seed: ProviderSeed): ConnectorProviderDefinition {
  return {
    provider: seed.provider,
    displayName: seed.displayName,
    description: seed.description,
    authStrategy: seed.authStrategy,
    capabilities: [capability(seed)],
    availability: seed.availability,
    revocationBehavior: 'Soft-revoke the LenserFight connection and stop runtime credential resolution.',
    tokenRefreshBehavior: seed.authStrategy === 'oauth2' ? 'Refresh access tokens server-side when the provider issues refresh tokens.' : null,
    secretStorageRequirements: 'Store provider secrets in Supabase Vault and return only sanitized metadata to clients.',
    rateLimit: { requestsPerMinute: seed.priorityRuntime ? 60 : 30 },
    executionSafety: {
      serverSideOnly: true,
      allowAiPromptUseByDefault: false,
      masksSecretsInLogs: true,
      requiresAllowlist: seed.provider === 'custom_http',
      responseSizeLimitBytes: seed.provider === 'custom_http' ? 262_144 : undefined,
    },
    docs: {
      docsSlug: seed.provider,
      examples: [`${seed.displayName} workflow connector example`],
    },
    ui: {
      category: seed.category,
      availability: seed.availability,
      connectLabel: seed.authStrategy === 'webhook' ? 'Configure' : `Connect ${seed.displayName}`,
    },
    testFixtures: {
      fixtureKind: seed.priorityRuntime ? 'mock_adapter' : 'metadata_only',
      notes: seed.priorityRuntime
        ? 'Use deterministic mocked runtime adapters; no real external API calls in normal tests.'
        : 'Metadata-only provider in this phase; unavailable runtime operations are tested as explicit errors.',
    },
  }
}

const SEEDS: readonly ProviderSeed[] = [
  {
    provider: 'notion',
    displayName: 'Notion',
    description: 'Read databases, query databases, create pages, and append page blocks.',
    authStrategy: 'oauth2',
    category: 'productivity',
    capability: 'database',
    capabilityName: 'Notion Databases and Pages',
    capabilityDescription: 'Read and write Notion database/page content.',
    requiredScopes: ['notion:read', 'notion:write'],
    operations: ['read_database', 'query_database', 'create_page', 'append_block'],
    availability: 'experimental',
    priorityRuntime: true,
  },
  {
    provider: 'asana',
    displayName: 'Asana',
    description: 'Create tasks, update tasks, and list project tasks.',
    authStrategy: 'oauth2',
    category: 'productivity',
    capability: 'tasks',
    capabilityName: 'Asana Tasks',
    capabilityDescription: 'Manage Asana project tasks.',
    requiredScopes: ['asana:tasks:read', 'asana:tasks:write'],
    operations: ['create_task', 'update_task', 'list_project_tasks'],
    availability: 'experimental',
    priorityRuntime: true,
  },
  {
    provider: 'monday',
    displayName: 'Monday.com',
    description: 'Create items, update items, and list board items.',
    authStrategy: 'api_key',
    category: 'productivity',
    capability: 'boards',
    capabilityName: 'Monday Boards',
    capabilityDescription: 'Manage Monday.com board items.',
    requiredScopes: ['monday:boards:read', 'monday:boards:write'],
    operations: ['create_item', 'update_item', 'list_board_items'],
    availability: 'experimental',
    priorityRuntime: true,
  },
  {
    provider: 'zapier',
    displayName: 'Zapier',
    description: 'Trigger Zapier webhook or action endpoints.',
    authStrategy: 'webhook',
    category: 'automation',
    capability: 'webhooks',
    capabilityName: 'Zapier Actions',
    capabilityDescription: 'Trigger a configured Zapier endpoint.',
    requiredScopes: ['zapier:webhook:trigger'],
    operations: ['trigger_webhook'],
    availability: 'experimental',
    priorityRuntime: true,
  },
  {
    provider: 'slack',
    displayName: 'Slack',
    description: 'Send messages and list channels when scopes allow.',
    authStrategy: 'oauth2',
    category: 'communication',
    capability: 'chat',
    capabilityName: 'Slack Chat',
    capabilityDescription: 'Send Slack messages and inspect channels.',
    requiredScopes: ['chat:write'],
    operations: ['send_message', 'list_channels'],
    availability: 'experimental',
    priorityRuntime: true,
  },
  {
    provider: 'github',
    displayName: 'GitHub',
    description: 'Create issues, comment on issues, and read repository metadata.',
    authStrategy: 'oauth2',
    category: 'developer',
    capability: 'repos',
    capabilityName: 'GitHub Repositories',
    capabilityDescription: 'Read repository metadata and create issue artifacts.',
    requiredScopes: ['repo'],
    operations: ['create_issue', 'comment_on_issue', 'read_repository_metadata'],
    availability: 'experimental',
    priorityRuntime: true,
  },
  {
    provider: 'jira',
    displayName: 'Jira',
    description: 'Create issues, update issues, and read project metadata.',
    authStrategy: 'oauth2',
    category: 'productivity',
    capability: 'issues',
    capabilityName: 'Jira Issues',
    capabilityDescription: 'Manage Jira issue records.',
    requiredScopes: ['read:jira-work', 'write:jira-work'],
    operations: ['create_issue', 'update_issue', 'read_project_metadata'],
    availability: 'experimental',
    priorityRuntime: true,
  },
  {
    provider: 'linear',
    displayName: 'Linear',
    description: 'Create issues, update issues, and list teams.',
    authStrategy: 'oauth2',
    category: 'productivity',
    capability: 'issues',
    capabilityName: 'Linear Issues',
    capabilityDescription: 'Manage Linear issue records.',
    requiredScopes: ['linear:read', 'linear:write'],
    operations: ['create_issue', 'update_issue', 'list_teams'],
    availability: 'experimental',
    priorityRuntime: true,
  },
  {
    provider: 'custom_http',
    displayName: 'Webhook / Custom HTTP',
    description: 'Send signed outbound HTTPS requests through strict SSRF controls.',
    authStrategy: 'webhook',
    category: 'custom',
    capability: 'http',
    capabilityName: 'Custom HTTP',
    capabilityDescription: 'Send allowlisted outbound webhook requests.',
    requiredScopes: ['custom_http:send'],
    operations: ['send_request'],
    availability: 'experimental',
    priorityRuntime: true,
  },
  {
    provider: 'gitlab',
    displayName: 'GitLab',
    description: 'Metadata foundation for GitLab repositories and issues.',
    authStrategy: 'oauth2',
    category: 'developer',
    capability: 'repos',
    capabilityName: 'GitLab Repositories',
    capabilityDescription: 'Repository and issue metadata.',
    requiredScopes: ['read_api'],
    operations: ['read_project_metadata'],
    availability: 'metadata_only',
  },
  {
    provider: 'trello',
    displayName: 'Trello',
    description: 'Metadata foundation for Trello boards and cards.',
    authStrategy: 'api_key',
    category: 'productivity',
    capability: 'boards',
    capabilityName: 'Trello Boards',
    capabilityDescription: 'Board and card metadata.',
    requiredScopes: ['trello:read'],
    operations: ['list_board_cards'],
    availability: 'metadata_only',
  },
  {
    provider: 'airtable',
    displayName: 'Airtable',
    description: 'Metadata foundation for Airtable records.',
    authStrategy: 'oauth2',
    category: 'productivity',
    capability: 'records',
    capabilityName: 'Airtable Records',
    capabilityDescription: 'Read and write Airtable records.',
    requiredScopes: ['data.records:read'],
    operations: ['list_records'],
    availability: 'metadata_only',
  },
  {
    provider: 'hubspot',
    displayName: 'HubSpot',
    description: 'Metadata foundation for HubSpot CRM objects.',
    authStrategy: 'oauth2',
    category: 'crm',
    capability: 'crm',
    capabilityName: 'HubSpot CRM',
    capabilityDescription: 'CRM object metadata.',
    requiredScopes: ['crm.objects.contacts.read'],
    operations: ['read_contact_metadata'],
    availability: 'metadata_only',
  },
  {
    provider: 'salesforce',
    displayName: 'Salesforce',
    description: 'Metadata foundation for Salesforce CRM records.',
    authStrategy: 'oauth2',
    category: 'crm',
    capability: 'crm',
    capabilityName: 'Salesforce CRM',
    capabilityDescription: 'CRM object metadata.',
    requiredScopes: ['api'],
    operations: ['read_object_metadata'],
    availability: 'metadata_only',
  },
  {
    provider: 'discord',
    displayName: 'Discord',
    description: 'Metadata foundation for Discord notifications.',
    authStrategy: 'webhook',
    category: 'communication',
    capability: 'messages',
    capabilityName: 'Discord Messages',
    capabilityDescription: 'Discord message delivery metadata.',
    requiredScopes: ['discord:webhook:send'],
    operations: ['send_message'],
    availability: 'metadata_only',
  },
  {
    provider: 'microsoft_teams',
    displayName: 'Microsoft Teams',
    description: 'Metadata foundation for Teams messages.',
    authStrategy: 'oauth2',
    category: 'communication',
    capability: 'channels',
    capabilityName: 'Teams Channels',
    capabilityDescription: 'Teams channel message metadata.',
    requiredScopes: ['ChannelMessage.Send'],
    operations: ['send_message'],
    availability: 'metadata_only',
  },
  {
    provider: 'microsoft_outlook',
    displayName: 'Microsoft Outlook',
    description: 'Metadata foundation for Outlook mail and calendar.',
    authStrategy: 'oauth2',
    category: 'productivity',
    capability: 'messages',
    capabilityName: 'Outlook Mail',
    capabilityDescription: 'Outlook message metadata.',
    requiredScopes: ['Mail.Send'],
    operations: ['send_message'],
    availability: 'metadata_only',
  },
  {
    provider: 'microsoft_onedrive',
    displayName: 'Microsoft OneDrive',
    description: 'Metadata foundation for OneDrive files.',
    authStrategy: 'oauth2',
    category: 'storage',
    capability: 'files',
    capabilityName: 'OneDrive Files',
    capabilityDescription: 'OneDrive file metadata.',
    requiredScopes: ['Files.ReadWrite'],
    operations: ['list_files'],
    availability: 'metadata_only',
  },
  {
    provider: 'microsoft_excel',
    displayName: 'Microsoft Excel Online',
    description: 'Metadata foundation for Excel workbook ranges.',
    authStrategy: 'oauth2',
    category: 'productivity',
    capability: 'sheets',
    capabilityName: 'Excel Workbooks',
    capabilityDescription: 'Excel workbook range metadata.',
    requiredScopes: ['Files.ReadWrite'],
    operations: ['read_range'],
    availability: 'metadata_only',
  },
  {
    provider: 'dropbox',
    displayName: 'Dropbox',
    description: 'Metadata foundation for Dropbox files.',
    authStrategy: 'oauth2',
    category: 'storage',
    capability: 'files',
    capabilityName: 'Dropbox Files',
    capabilityDescription: 'Dropbox file metadata.',
    requiredScopes: ['files.metadata.read'],
    operations: ['list_files'],
    availability: 'metadata_only',
  },
  {
    provider: 'box',
    displayName: 'Box',
    description: 'Metadata foundation for Box files.',
    authStrategy: 'oauth2',
    category: 'storage',
    capability: 'files',
    capabilityName: 'Box Files',
    capabilityDescription: 'Box file metadata.',
    requiredScopes: ['root_readwrite'],
    operations: ['list_files'],
    availability: 'metadata_only',
  },
  {
    provider: 'calendly',
    displayName: 'Calendly',
    description: 'Metadata foundation for Calendly events.',
    authStrategy: 'oauth2',
    category: 'productivity',
    capability: 'events',
    capabilityName: 'Calendly Events',
    capabilityDescription: 'Calendly event metadata.',
    requiredScopes: ['calendly:read'],
    operations: ['list_events'],
    availability: 'metadata_only',
  },
  {
    provider: 'clickup',
    displayName: 'ClickUp',
    description: 'Metadata foundation for ClickUp tasks.',
    authStrategy: 'oauth2',
    category: 'productivity',
    capability: 'tasks',
    capabilityName: 'ClickUp Tasks',
    capabilityDescription: 'ClickUp task metadata.',
    requiredScopes: ['task:read'],
    operations: ['list_tasks'],
    availability: 'metadata_only',
  },
  {
    provider: 'todoist',
    displayName: 'Todoist',
    description: 'Metadata foundation for Todoist tasks.',
    authStrategy: 'oauth2',
    category: 'productivity',
    capability: 'tasks',
    capabilityName: 'Todoist Tasks',
    capabilityDescription: 'Todoist task metadata.',
    requiredScopes: ['data:read'],
    operations: ['list_tasks'],
    availability: 'metadata_only',
  },
]

export const automationProviders = SEEDS.map(provider)
