/**
 * X (Twitter) Vendor Template
 */
import type { VendorTemplate } from '../types.js';

export const twitterTemplate: VendorTemplate = {
  id: 'twitter',
  name: 'X (Twitter)',
  serviceType: 'twitter',
  baseURL: 'https://api.x.com/2',
  docsURL: 'https://developer.x.com/en/docs/x-api',
  credentialsSetupURL: 'https://developer.x.com/en/portal/dashboard',
  category: 'communication',
  notes: 'X (formerly Twitter) API v2. OAuth 2.0 with PKCE for user-context actions, Bearer Token for app-only access.',

  authTemplates: [
    {
      id: 'oauth-user',
      name: 'OAuth 2.0 (User Context)',
      type: 'oauth',
      flow: 'authorization_code',
      description: 'User authorizes via X login - required for posting tweets, managing likes/follows, and accessing private data. Provide clientSecret for web apps; omit for native/desktop apps (secured via PKCE).',
      requiredFields: ['clientId', 'redirectUri'],
      optionalFields: ['clientSecret', 'scope'],
      defaults: {
        type: 'oauth',
        flow: 'authorization_code',
        authorizationUrl: 'https://x.com/i/oauth2/authorize',
        tokenUrl: 'https://api.x.com/2/oauth2/token',
        usePKCE: true,
      },
      scopes: [
        'tweet.read',
        'tweet.write',
        'tweet.moderate.write',
        'users.read',
        'follows.read',
        'follows.write',
        'like.read',
        'like.write',
        'bookmark.read',
        'bookmark.write',
        'list.read',
        'list.write',
        'block.read',
        'block.write',
        'mute.read',
        'mute.write',
        'space.read',
        'dm.read',
        'dm.write',
        'offline.access',
      ],
      scopeDescriptions: {
        'tweet.read': 'Read tweets and timelines',
        'tweet.write': 'Post and delete tweets',
        'tweet.moderate.write': 'Hide and unhide replies',
        'users.read': 'Read user profile information',
        'follows.read': 'Read following/followers lists',
        'follows.write': 'Follow and unfollow users',
        'like.read': 'Read liked tweets',
        'like.write': 'Like and unlike tweets',
        'bookmark.read': 'Read bookmarked tweets',
        'bookmark.write': 'Bookmark and remove bookmarks',
        'list.read': 'Read lists',
        'list.write': 'Create, edit, and delete lists',
        'block.read': 'Read blocked users',
        'block.write': 'Block and unblock users',
        'mute.read': 'Read muted users',
        'mute.write': 'Mute and unmute users',
        'space.read': 'Read Spaces information',
        'dm.read': 'Read direct messages',
        'dm.write': 'Send direct messages',
        'offline.access': 'Stay connected (refresh token)',
      },
      // X/Twitter requires `offline.access` (with a DOT, not underscore — a
      // common gotcha vs. OIDC `offline_access`) to issue a refresh_token.
      refreshStrategy: { kind: 'scope', scope: 'offline.access' },
    },
    {
      id: 'bearer-token',
      name: 'Bearer Token (App-Only)',
      type: 'api_key',
      description: 'App-only access using Bearer Token from developer portal. Can read public tweets, users, and spaces but cannot post or access private data.',
      requiredFields: ['apiKey'],
      defaults: {
        type: 'api_key',
        headerName: 'Authorization',
        headerPrefix: 'Bearer',
      },
    },
  ],
};
