import { RB2BIcon } from '@/components/icons'
import type { BlockConfig, BlockMeta } from '@/blocks/types'
import { AuthMode, IntegrationType } from '@/blocks/types'
import type { Rb2bResponse } from '@/tools/rb2b/types'

const EMAIL_OPERATIONS = [
  'hem_to_business_profile',
  'hem_to_best_linkedin',
  'hem_to_linkedin',
  'hem_to_maid',
]

const LINKEDIN_OPERATIONS = [
  'linkedin_to_business_profile',
  'linkedin_to_best_personal_email',
  'linkedin_to_personal_email',
  'linkedin_to_hashed_emails',
  'linkedin_to_mobile_phone',
]

const IP_OPERATIONS = ['ip_to_hem', 'ip_to_maid', 'ip_to_company']

export const RB2BBlock: BlockConfig<Rb2bResponse> = {
  type: 'rb2b',
  name: 'RB2B',
  description: 'Identify and enrich website visitors',
  authMode: AuthMode.ApiKey,
  longDescription:
    'Resolve IP addresses, hashed emails, and LinkedIn profiles into person-level identity and B2B enrichment data using the RB2B API. Convert IPs to hashed emails, MAIDs, and company domains; enrich emails into LinkedIn profiles, business profiles, and mobile IDs; and look up emails or phone numbers from LinkedIn. Requires an RB2B API key.',
  category: 'tools',
  integrationType: IntegrationType.Sales,
  docsLink: 'https://docs.sim.ai/integrations/rb2b',
  bgColor: '#51FF00',
  icon: RB2BIcon,
  subBlocks: [
    {
      id: 'operation',
      title: 'Operation',
      type: 'dropdown',
      options: [
        { label: 'Credit Check', id: 'credit_check' },
        { label: 'IP to HEM', id: 'ip_to_hem' },
        { label: 'IP to MAID', id: 'ip_to_maid' },
        { label: 'IP to Company', id: 'ip_to_company' },
        { label: 'Email/HEM to Business Profile', id: 'hem_to_business_profile' },
        { label: 'Email/HEM to Best LinkedIn URL', id: 'hem_to_best_linkedin' },
        { label: 'Email/HEM to LinkedIn Slug', id: 'hem_to_linkedin' },
        { label: 'Email/HEM to MAID', id: 'hem_to_maid' },
        { label: 'Email to Last Active Date', id: 'email_to_activity' },
        { label: 'LinkedIn to Business Profile', id: 'linkedin_to_business_profile' },
        { label: 'LinkedIn to Best Personal Email', id: 'linkedin_to_best_personal_email' },
        { label: 'LinkedIn to Personal Email', id: 'linkedin_to_personal_email' },
        { label: 'LinkedIn to Hashed Emails', id: 'linkedin_to_hashed_emails' },
        { label: 'LinkedIn to Mobile Phone', id: 'linkedin_to_mobile_phone' },
        { label: 'LinkedIn Slug Search', id: 'linkedin_slug_search' },
      ],
      value: () => 'ip_to_hem',
    },
    {
      id: 'ip_address',
      title: 'IP Address',
      type: 'short-input',
      placeholder: 'e.g. 162.192.6.240',
      condition: { field: 'operation', value: IP_OPERATIONS },
      required: { field: 'operation', value: IP_OPERATIONS },
    },
    {
      id: 'user_agent',
      title: 'User Agent',
      type: 'short-input',
      placeholder: 'Optional user agent string',
      condition: { field: 'operation', value: ['ip_to_hem', 'ip_to_maid'] },
    },
    {
      id: 'include_sha256',
      title: 'Include SHA-256 Hashes',
      type: 'switch',
      condition: { field: 'operation', value: 'ip_to_hem' },
    },
    {
      id: 'email',
      title: 'Email or MD5 Hash',
      type: 'short-input',
      placeholder: 'jane@example.com or an MD5 hash',
      condition: { field: 'operation', value: EMAIL_OPERATIONS },
      required: { field: 'operation', value: EMAIL_OPERATIONS },
    },
    {
      id: 'emailAddress',
      title: 'Email Address',
      type: 'short-input',
      placeholder: 'jane@example.com',
      condition: { field: 'operation', value: 'email_to_activity' },
      required: { field: 'operation', value: 'email_to_activity' },
    },
    {
      id: 'linkedin_slug',
      title: 'LinkedIn Slug or URL',
      type: 'short-input',
      placeholder: 'e.g. firstname-lastname or full profile URL',
      condition: { field: 'operation', value: LINKEDIN_OPERATIONS },
      required: { field: 'operation', value: LINKEDIN_OPERATIONS },
    },
    {
      id: 'first_name',
      title: 'First Name',
      type: 'short-input',
      placeholder: 'Jane',
      condition: { field: 'operation', value: 'linkedin_slug_search' },
      required: { field: 'operation', value: 'linkedin_slug_search' },
    },
    {
      id: 'last_name',
      title: 'Last Name',
      type: 'short-input',
      placeholder: 'Doe',
      condition: { field: 'operation', value: 'linkedin_slug_search' },
      required: { field: 'operation', value: 'linkedin_slug_search' },
    },
    {
      id: 'company_domain',
      title: 'Company Domain',
      type: 'short-input',
      placeholder: 'example.com',
      condition: { field: 'operation', value: 'linkedin_slug_search' },
      required: { field: 'operation', value: 'linkedin_slug_search' },
    },
    {
      id: 'apiKey',
      title: 'API Key',
      type: 'short-input',
      placeholder: 'Enter your RB2B API key',
      password: true,
      required: true,
    },
  ],
  tools: {
    access: [
      'rb2b_credit_check',
      'rb2b_ip_to_hem',
      'rb2b_ip_to_maid',
      'rb2b_ip_to_company',
      'rb2b_hem_to_business_profile',
      'rb2b_hem_to_best_linkedin',
      'rb2b_hem_to_linkedin',
      'rb2b_hem_to_maid',
      'rb2b_email_to_activity',
      'rb2b_linkedin_to_business_profile',
      'rb2b_linkedin_to_best_personal_email',
      'rb2b_linkedin_to_personal_email',
      'rb2b_linkedin_to_hashed_emails',
      'rb2b_linkedin_to_mobile_phone',
      'rb2b_linkedin_slug_search',
    ],
    config: {
      tool: (params) => `rb2b_${params.operation}`,
      params: (params) => {
        const email = params.email ?? params.emailAddress
        return email !== undefined ? { email } : {}
      },
    },
  },
  inputs: {
    operation: { type: 'string', description: 'Operation to perform' },
    apiKey: { type: 'string', description: 'RB2B API key' },
    ip_address: { type: 'string', description: 'IP address to resolve' },
    user_agent: { type: 'string', description: 'Optional user agent string' },
    include_sha256: { type: 'boolean', description: 'Include SHA-256 hashes (IP to HEM)' },
    email: { type: 'string', description: 'Email address or MD5 hash (HEM operations)' },
    emailAddress: { type: 'string', description: 'Email address (Email to Last Active Date)' },
    linkedin_slug: { type: 'string', description: 'LinkedIn profile slug or URL' },
    first_name: { type: 'string', description: 'First name (LinkedIn Slug Search)' },
    last_name: { type: 'string', description: 'Last name (LinkedIn Slug Search)' },
    company_domain: { type: 'string', description: 'Company domain (LinkedIn Slug Search)' },
  },
  outputs: {
    credits_remaining: { type: 'number', description: 'API credits remaining (Credit Check)' },
    results: {
      type: 'json',
      description: 'Result list for IP/MAID/company/activity lookups',
    },
    match_count: { type: 'number', description: 'Number of matches (Email to Last Active Date)' },
    credits_charged: { type: 'number', description: 'Credits charged for the request' },
    credits_exhausted: { type: 'boolean', description: 'Whether the account is out of credits' },
    linkedin_url: { type: 'string', description: 'LinkedIn profile URL' },
    linkedin_slug: { type: 'string', description: 'LinkedIn slug' },
    email: { type: 'string', description: 'Best personal email' },
    emails: { type: 'array', description: 'Personal email addresses' },
    mobile_phone: { type: 'string', description: 'Mobile phone number' },
    business_md5_array: { type: 'array', description: 'MD5 hashes of business emails' },
    business_sha256_array: { type: 'array', description: 'SHA-256 hashes of business emails' },
    personal_md5_array: { type: 'array', description: 'MD5 hashes of personal emails' },
    personal_sha256_array: { type: 'array', description: 'SHA-256 hashes of personal emails' },
    first_name: { type: 'string', description: 'First name (business profile)' },
    last_name: { type: 'string', description: 'Last name (business profile)' },
    full_name: { type: 'string', description: 'Full name (LinkedIn business profile)' },
    headline: { type: 'string', description: 'LinkedIn headline' },
    title: { type: 'string', description: 'Job title' },
    seniority: { type: 'string', description: 'Seniority level' },
    country: { type: 'string', description: 'Country' },
    current_industry: { type: 'string', description: 'Current industry' },
    functional_area: { type: 'json', description: 'Functional area(s)' },
    current_company: { type: 'string', description: 'Current company name' },
    current_company_url: { type: 'string', description: 'Current company website' },
    current_company_linkedinurl: { type: 'string', description: 'Current company LinkedIn URL' },
    company: { type: 'json', description: 'Company details (LinkedIn business profile)' },
    business_email: { type: 'string', description: 'Business email address' },
    personal_email: { type: 'string', description: 'Personal email address' },
    personal_emails: { type: 'array', description: 'Personal emails (business profile)' },
    linkedinurl: { type: 'string', description: 'LinkedIn profile URL (business profile)' },
    link_email: { type: 'string', description: 'Linked business email (business profile)' },
    work_email_confirmed: { type: 'string', description: 'Whether the work email is confirmed' },
    company_employee_count: { type: 'string', description: 'Company employee count' },
    company_employee_range: { type: 'string', description: 'Company employee range band' },
    company_revenue_range: { type: 'string', description: 'Company revenue range band' },
    md5: { type: 'string', description: 'MD5 hash of the resolved email' },
  },
}

export const RB2BBlockMeta = {
  tags: ['enrichment', 'sales-engagement', 'identity'],
  url: 'https://rb2b.com',
  templates: [
    {
      icon: RB2BIcon,
      title: 'Website visitor de-anonymizer',
      prompt:
        'Build a workflow that takes the IP addresses of anonymous website visitors, uses RB2B to resolve each IP to a hashed email and company domain, and writes the identified visitors into a table for the sales team.',
      modules: ['tables', 'agent', 'workflows'],
      category: 'sales',
      tags: ['sales', 'identity', 'enrichment'],
    },
    {
      icon: RB2BIcon,
      title: 'Visitor IP to LinkedIn profile',
      prompt:
        'Create a workflow that resolves a visitor IP to a hashed email with RB2B, then enriches that hashed email into a LinkedIn profile and business profile so reps know exactly who visited.',
      modules: ['tables', 'agent', 'workflows'],
      category: 'sales',
      tags: ['sales', 'identity', 'research'],
    },
    {
      icon: RB2BIcon,
      title: 'Hashed email enrichment pipeline',
      prompt:
        'Build a workflow that reads hashed emails from a table, uses RB2B to enrich each into a full business profile with name, title, seniority, and company details, and writes the enriched records back to the row.',
      modules: ['tables', 'agent', 'workflows'],
      category: 'sales',
      tags: ['sales', 'enrichment', 'research'],
    },
    {
      icon: RB2BIcon,
      title: 'LinkedIn to mobile phone finder',
      prompt:
        "Create a workflow that takes a list of LinkedIn profile slugs, uses RB2B to look up each prospect's mobile phone and best personal email, and writes a ready-to-contact table for outbound calling.",
      modules: ['tables', 'agent', 'workflows'],
      category: 'sales',
      tags: ['sales', 'enrichment', 'research'],
    },
    {
      icon: RB2BIcon,
      title: 'Intent-to-CRM identity sync',
      prompt:
        'Build a workflow that resolves visitor IPs to company domains with RB2B, enriches the matched person into a business profile, and creates or updates the matching contact and company in HubSpot.',
      modules: ['agent', 'workflows'],
      category: 'sales',
      tags: ['sales', 'identity', 'crm'],
      alsoIntegrations: ['hubspot'],
    },
    {
      icon: RB2BIcon,
      title: 'High-intent visitor Slack alerts',
      prompt:
        'Create a workflow that reads a list of website visitor IPs, uses RB2B to resolve each person and their company, and posts an alert with the identified LinkedIn profile and company to the sales Slack channel.',
      modules: ['agent', 'workflows'],
      category: 'sales',
      tags: ['sales', 'identity', 'automation'],
      alsoIntegrations: ['slack'],
    },
    {
      icon: RB2BIcon,
      title: 'LinkedIn slug resolver',
      prompt:
        'Build a workflow that takes a first name, last name, and company domain, uses RB2B LinkedIn slug search to resolve the matching profile, and enriches it into a business profile written to a prospect table.',
      modules: ['tables', 'agent', 'workflows'],
      category: 'sales',
      tags: ['sales', 'research', 'enrichment'],
    },
    {
      icon: RB2BIcon,
      title: 'Account engagement freshness sweep',
      prompt:
        'Create a scheduled workflow that runs my list of prospect emails through RB2B email-to-last-active-date lookups, flags recently active contacts, and logs the engagement snapshot to a table for prioritized follow-up.',
      modules: ['scheduled', 'tables', 'agent', 'workflows'],
      category: 'sales',
      tags: ['sales', 'enrichment', 'automation'],
    },
  ],
  skills: [
    {
      name: 'identify-website-visitor',
      description: 'Resolve a visitor IP into a person and company profile for sales follow-up.',
      content:
        '# Identify Website Visitor\n\nTurn an anonymous visit into a named, enrichable lead.\n\n## Steps\n1. Take the visitor ip_address and run ip_to_company to resolve the firmographic match.\n2. Run ip_to_hem to obtain the hashed email identifier for the person.\n3. Run hem_to_business_profile and hem_to_best_linkedin to build a full contact profile.\n4. Score the lead by company fit and route high-intent matches to sales.\n\n## Output\nReturn the resolved company, person name, title, and LinkedIn. Flag whether the match clears your fit threshold.',
    },
    {
      name: 'enrich-linkedin-contact',
      description: 'Enrich a LinkedIn profile with business email and phone for outreach.',
      content:
        '# Enrich LinkedIn Contact\n\nTurn a LinkedIn profile into reachable contact details.\n\n## Steps\n1. Provide the LinkedIn URL or slug; use linkedin_slug_search first if you only have a name.\n2. Run linkedin_to_business_profile to capture role and company.\n3. Run linkedin_to_best_personal_email and linkedin_to_mobile_phone for direct contact points.\n4. Push the enriched record into the CRM or an outreach sequence.\n\n## Output\nReturn the contact name, company, best email, and phone. Note any field that could not be resolved.',
    },
    {
      name: 'check-credit-balance',
      description: 'Check the RB2B credit balance before running a batch of enrichment lookups.',
      content:
        '# Check Credit Balance\n\nVerify enrichment credits remain before a batch run.\n\n## Steps\n1. Run credit_check to read the current balance.\n2. Estimate the credits needed for the planned lookups.\n3. If the balance is insufficient, alert the team rather than starting a partial run.\n\n## Output\nReturn the remaining credit balance and whether the planned batch can proceed.',
    },
  ],
} as const satisfies BlockMeta
