import { z } from 'zod'
import { createIntegrationTool } from '../../../tool'
import type { Tool } from '../../../../types'

/**
 * Create Anymailfinder find company email tool
 *
 * Discovers all email addresses at a company domain.
 *
 * @param credentialName - Name of credential in credentials table
 * @returns Tool that finds company emails via Anymailfinder
 *
 * @example
 * const findCompanyEmail = createAnymailfinderFindCompanyEmailTool('anymailfinder-elevasis')
 */
export function createAnymailfinderFindCompanyEmailTool(credentialName: string): Tool {
  return createIntegrationTool({
    name: 'anymailfinder_find_company_email',
    description: `Find all email addresses at a company domain using Anymailfinder.

USE CASES:
- Discover all emails at a domain (up to 20)
- Filter by email type (personal, generic, or all)

REQUIRED PARAMETERS (at least one):
- domain: Company domain (e.g., "example.com")
- company_name: Company name (e.g., "Example Inc")

OPTIONAL PARAMETERS:
- email_type: "personal", "generic", or "all" (default: "all")

OUTPUT:
{
  "email_status": "valid",
  "emails": ["john@example.com", "info@example.com"],
  "valid_emails": ["john@example.com"]
}`,
    inputSchema: z.object({
      domain: z.string().optional().describe('Company domain (e.g., "example.com")'),
      company_name: z.string().optional().describe('Company name (e.g., "Example Inc")'),
      email_type: z.enum(['personal', 'generic', 'all']).optional().describe('Filter by email type (default: "all")')
    }),
    outputSchema: z.object({
      email_status: z.string().describe('Overall status of the email search'),
      emails: z.array(z.string()).describe('All emails found at the domain'),
      valid_emails: z.array(z.string()).describe('Subset of emails that passed validation')
    }),
    integration: 'anymailfinder' as const,
    method: 'findCompanyEmail' as const,
    credentialName
  })
}

/**
 * Create Anymailfinder find person email tool
 *
 * Finds an individual's work email by name and company.
 *
 * @param credentialName - Name of credential in credentials table
 * @returns Tool that finds a person's email via Anymailfinder
 *
 * @example
 * const findPersonEmail = createAnymailfinderFindPersonEmailTool('anymailfinder-elevasis')
 */
export function createAnymailfinderFindPersonEmailTool(credentialName: string): Tool {
  return createIntegrationTool({
    name: 'anymailfinder_find_person_email',
    description: `Find an individual's email address using their name and company domain.

USE CASES:
- Find email for a specific person when you have their name and company domain
- Verify a person's work email

REQUIRED PARAMETERS:
- domain or company_name (at least one)
- full_name or (first_name + last_name)

OPTIONAL PARAMETERS:
- domain: Company domain
- company_name: Company name
- full_name: Person's full name
- first_name: First name
- last_name: Last name

OUTPUT:
{
  "email": "john@example.com",
  "email_status": "valid",
  "valid_email": "john@example.com"
}`,
    inputSchema: z.object({
      domain: z.string().optional().describe('Company domain'),
      company_name: z.string().optional().describe('Company name'),
      full_name: z.string().optional().describe("Person's full name"),
      first_name: z.string().optional().describe('First name'),
      last_name: z.string().optional().describe('Last name')
    }),
    outputSchema: z.object({
      email: z.string().describe('Found email address (empty string if not found)'),
      email_status: z.string().describe('Validation status of the email'),
      valid_email: z.string().describe('Email if valid, empty string otherwise')
    }),
    integration: 'anymailfinder' as const,
    method: 'findPersonEmail' as const,
    credentialName
  })
}

/**
 * Create Anymailfinder find decision maker email tool
 *
 * Finds a decision maker's email at a company by role category.
 *
 * @param credentialName - Name of credential in credentials table
 * @returns Tool that finds decision maker emails via Anymailfinder
 *
 * @example
 * const findDecisionMaker = createAnymailfinderFindDecisionMakerEmailTool('anymailfinder-elevasis')
 */
export function createAnymailfinderFindDecisionMakerEmailTool(credentialName: string): Tool {
  return createIntegrationTool({
    name: 'anymailfinder_find_decision_maker_email',
    description: `Find a decision maker's email at a company by role category.

USE CASES:
- Find the owner/CEO email at a business
- Find operations manager email
- Find key decision makers when you don't have specific names

REQUIRED PARAMETERS:
- domain or company_name (at least one)
- decision_maker_category: Array of role categories

CATEGORIES: "ceo" (Owner/President/Founder), "operations", "marketing", "sales", "finance", "hr", "engineering", "it", "logistics", "buyer"

OUTPUT:
{
  "email": "owner@example.com",
  "email_status": "valid",
  "valid_email": "owner@example.com",
  "person_full_name": "Jane Smith",
  "person_job_title": "Owner",
  "person_linkedin_url": "https://linkedin.com/in/...",
  "decision_maker_category": "ceo"
}`,
    inputSchema: z.object({
      domain: z.string().optional().describe('Company domain'),
      company_name: z.string().optional().describe('Company name'),
      decision_maker_category: z
        .array(z.string())
        .describe(
          'Role categories to search: "ceo", "operations", "marketing", "sales", "finance", "hr", "engineering", "it", "logistics", "buyer"'
        )
    }),
    outputSchema: z.object({
      email: z.string().describe('Found email address (empty string if not found)'),
      email_status: z.string().describe('Validation status of the email'),
      valid_email: z.string().describe('Email if valid, empty string otherwise'),
      person_full_name: z.string().describe("Decision maker's full name"),
      person_job_title: z.string().describe("Decision maker's job title"),
      person_linkedin_url: z.string().describe("Decision maker's LinkedIn URL"),
      decision_maker_category: z.string().describe('Matched role category')
    }),
    integration: 'anymailfinder' as const,
    method: 'findDecisionMakerEmail' as const,
    credentialName
  })
}

/**
 * Create Anymailfinder verify email tool
 *
 * Verifies whether an email address is deliverable.
 *
 * @param credentialName - Name of credential in credentials table
 * @returns Tool that verifies email addresses via Anymailfinder
 *
 * @example
 * const verifyEmail = createAnymailfinderVerifyEmailTool('anymailfinder-elevasis')
 */
export function createAnymailfinderVerifyEmailTool(credentialName: string): Tool {
  return createIntegrationTool({
    name: 'anymailfinder_verify_email',
    description: `Verify if an email address is valid using Anymailfinder.

USE CASES:
- Verify an email before sending outreach
- Check if a found email is deliverable

REQUIRED PARAMETERS:
- email: Email address to verify

OUTPUT:
{
  "email_status": "valid"
}

Status values: "valid", "risky", "not_found", "blacklisted"`,
    inputSchema: z.object({
      email: z.string().describe('Email address to verify')
    }),
    outputSchema: z.object({
      email_status: z.string().describe('Verification result: "valid", "risky", "not_found", or "blacklisted"')
    }),
    integration: 'anymailfinder' as const,
    method: 'verifyEmail' as const,
    credentialName
  })
}
