export interface PermissionGroup {
  name: string;
  description: string;
  actions: string[];
}

export const PERMISSION_GROUPS: Record<string, PermissionGroup> = {
  gmail: {
    name: "Gmail",
    description: "Access to Gmail operations",
    actions: [
      "gmail:read_emails",
      "gmail:send_email",
      "gmail:list_labels",
      "gmail:search_emails",
    ],
  },
  calendar: {
    name: "Google Calendar",
    description: "Access to Google Calendar operations",
    actions: [
      "calendar:list_events",
      "calendar:create_event",
      "calendar:update_event",
      "calendar:delete_event",
    ],
  },
  drive: {
    name: "Google Drive",
    description: "Access to Google Drive operations",
    actions: [
      "drive:list_files",
      "drive:read_file",
      "drive:create_file",
      "drive:delete_file",
    ],
  },
  googleAds: {
    name: "Google Ads",
    description: "Access to Google Ads operations",
    actions: [
      // Read - Campaigns
      "googleAds:list_campaigns",
      "googleAds:get_campaign_performance",
      // Read - Ad Groups
      "googleAds:list_ad_groups",
      "googleAds:get_ad_group_performance",
      // Read - Ads
      "googleAds:list_ads",
      "googleAds:get_ad_performance",
      // Read - Keywords
      "googleAds:list_keywords",
      "googleAds:get_keyword_performance",
      "googleAds:get_search_terms_report",
      // Read - Negative Keywords
      "googleAds:list_negative_keywords",
      // Read - Account
      "googleAds:get_account_overview",
      // Read - Audiences
      "googleAds:list_audiences",
      "googleAds:get_audience_performance",
      // Read - Conversions
      "googleAds:list_conversions",
      "googleAds:get_conversion_performance",
      // Read - Extensions
      "googleAds:list_extensions",
      // Read - Budgets & Bidding
      "googleAds:list_budgets",
      "googleAds:get_budget_details",
      "googleAds:list_bid_strategies",
      "googleAds:get_bid_strategy_performance",
      // Read - Recommendations
      "googleAds:list_recommendations",
      // Read - Change History
      "googleAds:get_change_history",
      // Read - Labels
      "googleAds:list_labels",
      // Read - Assets
      "googleAds:list_assets",
      "googleAds:list_asset_groups",
      // Read - Geo & Device Performance
      "googleAds:get_geo_performance",
      "googleAds:get_device_performance",
      // Write - Campaigns
      "googleAds:create_campaign",
      "googleAds:update_campaign",
      "googleAds:pause_campaign",
      "googleAds:enable_campaign",
      // Write - Ad Groups
      "googleAds:create_ad_group",
      "googleAds:update_ad_group",
      "googleAds:pause_ad_group",
      // Write - Ads
      "googleAds:create_ad",
      "googleAds:update_ad",
      "googleAds:pause_ad",
      // Write - Keywords
      "googleAds:add_keyword",
      "googleAds:remove_keyword",
      "googleAds:update_keyword_bid",
      // Write - Negative Keywords
      "googleAds:add_negative_keyword",
      "googleAds:remove_negative_keyword",
      // Write - Budgets
      "googleAds:update_budget",
      // Write - Recommendations
      "googleAds:apply_recommendation",
      "googleAds:dismiss_recommendation",
      // Write - Labels
      "googleAds:create_label",
      "googleAds:assign_label",
      // Write - Extensions
      "googleAds:create_sitelink",
      "googleAds:create_callout",
      "googleAds:update_sitelink",
      "googleAds:update_callout",
      "googleAds:remove_extension",
    ],
  },
  searchConsole: {
    name: "Google Search Console",
    description: "Access to Google Search Console operations",
    actions: [
      "searchConsole:list_sites",
      "searchConsole:get_site",
      "searchConsole:add_site",
      "searchConsole:delete_site",
      "searchConsole:query_analytics",
      "searchConsole:inspect_url",
      "searchConsole:list_sitemaps",
      "searchConsole:get_sitemap",
      "searchConsole:submit_sitemap",
      "searchConsole:delete_sitemap",
    ],
  },
  youtube: {
    name: "YouTube",
    description: "Access to YouTube Data API operations",
    actions: [
      "youtube:list_channels",
      "youtube:get_channel",
      "youtube:list_videos",
      "youtube:get_video",
      "youtube:upload_video",
      "youtube:update_video",
      "youtube:delete_video",
      "youtube:list_playlists",
      "youtube:get_playlist",
      "youtube:create_playlist",
      "youtube:update_playlist",
      "youtube:delete_playlist",
      "youtube:list_playlist_items",
      "youtube:add_playlist_item",
      "youtube:remove_playlist_item",
      "youtube:search",
      "youtube:list_comments",
      "youtube:add_comment",
      "youtube:delete_comment",
      "youtube:list_subscriptions",
      "youtube:get_analytics",
      "youtube:update_channel",
      "youtube:list_captions",
      "youtube:download_caption",
      "youtube:delete_caption",
      "youtube:list_channel_sections",
      "youtube:create_channel_section",
      "youtube:update_channel_section",
      "youtube:delete_channel_section",
      "youtube:list_comment_replies",
      "youtube:reply_to_comment",
      "youtube:update_comment",
      "youtube:set_comment_moderation",
      "youtube:list_languages",
      "youtube:list_regions",
      "youtube:list_members",
      "youtube:list_membership_levels",
      "youtube:update_playlist_item",
      "youtube:subscribe",
      "youtube:unsubscribe",
      "youtube:list_categories",
      "youtube:rate_video",
      "youtube:get_rating",
      "youtube:unset_watermark",
    ],
  },
  openRouter: {
    name: "OpenRouter",
    description: "Access to OpenRouter AI API",
    actions: [
      "openRouter:chat_completion",
      "openRouter:get_generation",
      "openRouter:list_models",
      "openRouter:get_model_endpoints",
      "openRouter:get_key_info",
      "openRouter:get_credits",
      "openRouter:get_activity",
      "openRouter:list_providers",
      "openRouter:create_embeddings",
    ],
  },
  linkedin: {
    name: "LinkedIn",
    description: "Access to LinkedIn operations",
    actions: [
      "linkedin:get_profile",
      "linkedin:create_post",
      "linkedin:delete_post",
      "linkedin:get_post",
      "linkedin:like_post",
      "linkedin:unlike_post",
      "linkedin:comment_on_post",
      "linkedin:get_post_comments",
    ],
  },
  twitter: {
    name: "Twitter / X",
    description: "Access to Twitter / X API",
    actions: [
      "twitter:search_tweets",
      "twitter:get_tweet",
      "twitter:post_tweet",
      "twitter:delete_tweet",
      "twitter:get_me",
      "twitter:get_user",
      "twitter:get_user_tweets",
      "twitter:get_user_mentions",
      "twitter:like_tweet",
      "twitter:unlike_tweet",
      "twitter:retweet",
      "twitter:unretweet",
      "twitter:get_followers",
      "twitter:get_following",
      "twitter:follow_user",
      "twitter:unfollow_user",
      "twitter:mute_user",
      "twitter:unmute_user",
      "twitter:block_user",
      "twitter:unblock_user",
      "twitter:get_bookmarks",
      "twitter:bookmark_tweet",
      "twitter:unbookmark_tweet",
      "twitter:send_dm",
      "twitter:get_dm_events",
    ],
  },
  slack: {
    name: "Slack",
    description: "Access to Slack operations",
    actions: [
      "slack:list_channels",
      "slack:post_message",
      "slack:get_channel_history",
      "slack:get_user_info",
      "slack:add_reaction",
      "slack:remove_reaction",
      "slack:list_users",
      "slack:upload_file",
    ],
  },
  notion: {
    name: "Notion",
    description: "Access to Notion operations",
    actions: [
      "notion:search",
      "notion:get_page",
      "notion:create_page",
      "notion:update_page",
      "notion:get_database",
      "notion:query_database",
      "notion:create_database_item",
      "notion:get_block_children",
      "notion:append_block_children",
      "notion:delete_block",
      "notion:list_users",
    ],
  },
  hubspot: {
    name: "HubSpot",
    description: "Access to HubSpot CRM operations",
    actions: [
      "hubspot:list_contacts",
      "hubspot:get_contact",
      "hubspot:create_contact",
      "hubspot:update_contact",
      "hubspot:search_contacts",
      "hubspot:list_deals",
      "hubspot:get_deal",
      "hubspot:create_deal",
      "hubspot:update_deal",
      "hubspot:list_companies",
      "hubspot:get_company",
      "hubspot:create_company",
      "hubspot:update_company",
      "hubspot:search_companies",
    ],
  },
  github: {
    name: "GitHub",
    description: "Access to GitHub operations",
    actions: [
      "github:list_repos",
      "github:get_repo",
      "github:list_issues",
      "github:get_issue",
      "github:create_issue",
      "github:update_issue",
      "github:list_pull_requests",
      "github:get_pull_request",
      "github:create_pull_request",
      "github:list_commits",
      "github:get_authenticated_user",
      "github:search_repos",
      "github:search_issues",
    ],
  },
  jira: {
    name: "Jira",
    description: "Access to Jira project management",
    actions: [
      "jira:list_projects",
      "jira:get_project",
      "jira:search_issues",
      "jira:get_issue",
      "jira:create_issue",
      "jira:update_issue",
      "jira:add_comment",
      "jira:list_sprints",
      "jira:get_transitions",
      "jira:transition_issue",
    ],
  },
  salesforce: {
    name: "Salesforce",
    description: "Access to Salesforce CRM operations",
    actions: [
      "salesforce:query",
      "salesforce:get_record",
      "salesforce:create_record",
      "salesforce:update_record",
      "salesforce:delete_record",
      "salesforce:describe_object",
      "salesforce:list_objects",
      "salesforce:search",
    ],
  },
  metaAds: {
    name: "Meta Ads",
    description: "Access to Facebook & Instagram Ads",
    actions: [
      "metaAds:list_ad_accounts",
      "metaAds:get_ad_account",
      "metaAds:list_campaigns",
      "metaAds:get_campaign",
      "metaAds:get_campaign_insights",
      "metaAds:list_adsets",
      "metaAds:get_adset",
      "metaAds:get_adset_insights",
      "metaAds:list_ads",
      "metaAds:get_ad",
      "metaAds:get_ad_insights",
      "metaAds:get_account_insights",
      "metaAds:update_campaign_status",
      "metaAds:update_adset_status",
    ],
  },
  twitterAds: {
    name: "Twitter / X Ads",
    description: "Access to Twitter / X Ads API",
    actions: [
      "twitterAds:list_accounts",
      "twitterAds:get_account",
      "twitterAds:list_campaigns",
      "twitterAds:get_campaign",
      "twitterAds:list_line_items",
      "twitterAds:get_line_item",
      "twitterAds:list_promoted_tweets",
      "twitterAds:get_campaign_stats",
      "twitterAds:update_campaign_status",
    ],
  },
  telegram: {
    name: "Telegram",
    description: "Access to Telegram Bot API",
    actions: [
      "telegram:send_message",
      "telegram:get_updates",
      "telegram:get_chat",
      "telegram:get_chat_members_count",
      "telegram:send_photo",
      "telegram:send_document",
      "telegram:pin_message",
      "telegram:unpin_message",
    ],
  },
  semrush: {
    name: "SEMrush",
    description: "Access to SEMrush SEO analytics",
    actions: [
      "semrush:domain_overview",
      "semrush:domain_organic",
      "semrush:domain_organic_keywords",
      "semrush:keyword_overview",
      "semrush:keyword_difficulty",
      "semrush:backlinks_overview",
    ],
  },
  ahrefs: {
    name: "Ahrefs",
    description: "Access to Ahrefs SEO analytics",
    actions: [
      "ahrefs:domain_rating",
      "ahrefs:backlinks",
      "ahrefs:organic_keywords",
      "ahrefs:referring_domains",
      "ahrefs:subscription_info",
    ],
  },
  stripe: {
    name: "Stripe",
    description: "Access to Stripe payment operations",
    actions: [
      "stripe:list_customers",
      "stripe:get_customer",
      "stripe:create_customer",
      "stripe:list_subscriptions",
      "stripe:get_subscription",
      "stripe:list_invoices",
      "stripe:get_invoice",
      "stripe:get_balance",
      "stripe:list_charges",
      "stripe:list_payment_intents",
    ],
  },
  airtable: {
    name: "Airtable",
    description: "Access to Airtable operations",
    actions: [
      "airtable:list_bases",
      "airtable:get_base_schema",
      "airtable:list_records",
      "airtable:get_record",
      "airtable:create_record",
      "airtable:update_record",
      "airtable:delete_record",
    ],
  },
  calendly: {
    name: "Calendly",
    description: "Access to Calendly scheduling",
    actions: [
      "calendly:get_current_user",
      "calendly:list_event_types",
      "calendly:list_scheduled_events",
      "calendly:get_event",
      "calendly:list_invitees",
      "calendly:cancel_event",
    ],
  },
  threads: {
    name: "Threads",
    description: "Access to Threads (by Meta) operations",
    actions: [
      "threads:get_profile",
      "threads:get_threads",
      "threads:get_thread",
      "threads:publish_thread",
      "threads:delete_thread",
      "threads:get_replies",
      "threads:get_conversation",
      "threads:reply_to_thread",
      "threads:repost_thread",
      "threads:get_thread_insights",
      "threads:get_user_insights",
      "threads:get_publishing_limit",
      "threads:lookup_profile",
    ],
  },
  googleTagManager: {
    name: "Google Tag Manager",
    description: "Access to Google Tag Manager API operations",
    actions: [
      // Accounts
      "googleTagManager:list_accounts",
      "googleTagManager:get_account",
      "googleTagManager:update_account",
      // Containers
      "googleTagManager:list_containers",
      "googleTagManager:get_container",
      "googleTagManager:create_container",
      "googleTagManager:update_container",
      "googleTagManager:delete_container",
      "googleTagManager:get_container_snippet",
      // Workspaces
      "googleTagManager:list_workspaces",
      "googleTagManager:get_workspace",
      "googleTagManager:create_workspace",
      "googleTagManager:update_workspace",
      "googleTagManager:delete_workspace",
      "googleTagManager:get_workspace_status",
      "googleTagManager:quick_preview_workspace",
      "googleTagManager:sync_workspace",
      "googleTagManager:resolve_workspace_conflict",
      "googleTagManager:create_version_from_workspace",
      // Tags
      "googleTagManager:list_tags",
      "googleTagManager:get_tag",
      "googleTagManager:create_tag",
      "googleTagManager:update_tag",
      "googleTagManager:delete_tag",
      "googleTagManager:revert_tag",
      // Triggers
      "googleTagManager:list_triggers",
      "googleTagManager:get_trigger",
      "googleTagManager:create_trigger",
      "googleTagManager:update_trigger",
      "googleTagManager:delete_trigger",
      "googleTagManager:revert_trigger",
      // Variables
      "googleTagManager:list_variables",
      "googleTagManager:get_variable",
      "googleTagManager:create_variable",
      "googleTagManager:update_variable",
      "googleTagManager:delete_variable",
      "googleTagManager:revert_variable",
      // Built-in Variables
      "googleTagManager:list_built_in_variables",
      "googleTagManager:enable_built_in_variables",
      "googleTagManager:disable_built_in_variables",
      "googleTagManager:revert_built_in_variable",
      // Folders
      "googleTagManager:list_folders",
      "googleTagManager:get_folder",
      "googleTagManager:create_folder",
      "googleTagManager:update_folder",
      "googleTagManager:delete_folder",
      "googleTagManager:get_folder_entities",
      "googleTagManager:move_entities_to_folder",
      // Versions
      "googleTagManager:list_version_headers",
      "googleTagManager:get_latest_version_header",
      "googleTagManager:get_version",
      "googleTagManager:get_live_version",
      "googleTagManager:update_version",
      "googleTagManager:delete_version",
      "googleTagManager:undelete_version",
      "googleTagManager:publish_version",
      "googleTagManager:set_latest_version",
      // Environments
      "googleTagManager:list_environments",
      "googleTagManager:get_environment",
      "googleTagManager:create_environment",
      "googleTagManager:update_environment",
      "googleTagManager:delete_environment",
      "googleTagManager:reauthorize_environment",
      // User Permissions
      "googleTagManager:list_user_permissions",
      "googleTagManager:get_user_permission",
      "googleTagManager:create_user_permission",
      "googleTagManager:update_user_permission",
      "googleTagManager:delete_user_permission",
    ],
  },
  email: {
    name: "Email (IMAP/SMTP)",
    description: "Access to email via IMAP/SMTP",
    actions: [
      "email:list_mailboxes",
      "email:list_messages",
      "email:read_message",
      "email:search_messages",
      "email:send_message",
      "email:reply_message",
      "email:move_message",
      "email:delete_message",
      "email:mark_read",
    ],
  },
};

export const ALL_ACTIONS = Object.values(PERMISSION_GROUPS).flatMap(
  (g) => g.actions
);

export function getActionGroup(action: string): string | null {
  for (const [key, group] of Object.entries(PERMISSION_GROUPS)) {
    if (group.actions.includes(action)) return key;
  }
  return null;
}
