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

/**
 * Create Tomba email finder tool
 *
 * Finds a specific person's work email by first name, last name, and domain.
 *
 * @param credentialName - Name of credential in credentials table
 * @returns Tool that finds a person's email via Tomba
 *
 * @example
 * const emailFinder = createTombaEmailFinderTool('elevasis-tomba')
 */
export function createTombaEmailFinderTool(credentialName: string): Tool {
  return createIntegrationTool({
    name: 'tomba_email_finder',
    description: `Find a person's work email address using their name and company domain via Tomba.

USE CASES:
- Find email for a specific person when you have their name and company domain
- Discover a person's professional email with verification score

REQUIRED PARAMETERS:
- domain: Company domain (e.g., "stripe.com")
- firstName: Person's first name
- lastName: Person's last name

OUTPUT:
{
  "email": "john.doe@stripe.com",
  "firstName": "John",
  "lastName": "Doe",
  "fullName": "John Doe",
  "position": "Engineer",
  "company": "Stripe",
  "country": "US",
  "score": 90,
  "verificationDate": "2025-01-15",
  "verificationStatus": "valid",
  "sources": [...]
}`,
    inputSchema: z.object({
      domain: z.string().describe('Company domain (e.g., "stripe.com")'),
      firstName: z.string().describe("Person's first name"),
      lastName: z.string().describe("Person's last name")
    }),
    outputSchema: z.object({
      email: z.string().describe('Found email address (empty string if not found)'),
      firstName: z.string().describe("Person's first name"),
      lastName: z.string().describe("Person's last name"),
      fullName: z.string().describe("Person's full name"),
      position: z.string().describe("Person's job title or position"),
      company: z.string().describe('Company name'),
      country: z.string().describe('Country code'),
      score: z.number().describe('Confidence score (0-100)'),
      verificationDate: z.string().describe('Date email was last verified'),
      verificationStatus: z.string().describe('Verification result (e.g., "valid")'),
      sources: z
        .array(
          z.object({
            uri: z.string(),
            extractedOn: z.string(),
            lastSeenOn: z.string(),
            stillOnPage: z.boolean()
          })
        )
        .describe('Sources where the email was found')
    }),
    integration: 'tomba' as const,
    method: 'emailFinder' as const,
    credentialName
  })
}

/**
 * Create Tomba domain search tool
 *
 * Finds all email addresses at a company domain.
 *
 * @param credentialName - Name of credential in credentials table
 * @returns Tool that finds all emails at a domain via Tomba
 *
 * @example
 * const domainSearch = createTombaDomainSearchTool('elevasis-tomba')
 */
export function createTombaDomainSearchTool(credentialName: string): Tool {
  return createIntegrationTool({
    name: 'tomba_domain_search',
    description: `Find all email addresses at a company domain using Tomba.

USE CASES:
- Discover all known emails at a company
- Filter emails by department (e.g., "management", "finance")
- Get enriched data per email including position and verification

REQUIRED PARAMETERS:
- domain: Company domain (e.g., "stripe.com")

OPTIONAL PARAMETERS:
- limit: Maximum number of emails to return
- department: Filter by department (e.g., "management", "finance", "sales")

OUTPUT:
{
  "organizationName": "Stripe",
  "domain": "stripe.com",
  "emails": [
    {
      "email": "john.doe@stripe.com",
      "firstName": "John",
      "lastName": "Doe",
      "fullName": "John Doe",
      "position": "Engineer",
      "department": "engineering",
      "verificationDate": "2025-01-15",
      "verificationStatus": "valid",
      "sources": [...]
    }
  ]
}`,
    inputSchema: z.object({
      domain: z.string().describe('Company domain (e.g., "stripe.com")'),
      limit: z.number().optional().describe('Maximum number of emails to return'),
      department: z.string().optional().describe('Filter by department (e.g., "management", "finance", "sales")')
    }),
    outputSchema: z.object({
      organizationName: z.string().describe('Organization name'),
      domain: z.string().describe('Company domain'),
      emails: z
        .array(
          z.object({
            email: z.string(),
            firstName: z.string(),
            lastName: z.string(),
            fullName: z.string(),
            position: z.string(),
            department: z.string(),
            verificationDate: z.string(),
            verificationStatus: z.string(),
            sources: z.array(
              z.object({
                uri: z.string(),
                extractedOn: z.string(),
                lastSeenOn: z.string(),
                stillOnPage: z.boolean()
              })
            )
          })
        )
        .describe('List of email records found at the domain')
    }),
    integration: 'tomba' as const,
    method: 'domainSearch' as const,
    credentialName
  })
}

/**
 * Create Tomba email verifier tool
 *
 * Verifies whether an email address is deliverable.
 *
 * @param credentialName - Name of credential in credentials table
 * @returns Tool that verifies email addresses via Tomba
 *
 * @example
 * const emailVerifier = createTombaEmailVerifierTool('elevasis-tomba')
 */
export function createTombaEmailVerifierTool(credentialName: string): Tool {
  return createIntegrationTool({
    name: 'tomba_email_verifier',
    description: `Verify if an email address is valid and deliverable using Tomba.

USE CASES:
- Verify an email before sending outreach
- Check MX records and SMTP server availability
- Detect disposable or webmail addresses

REQUIRED PARAMETERS:
- email: Email address to verify

OUTPUT:
{
  "email": "john.doe@stripe.com",
  "result": "deliverable",
  "status": "valid",
  "score": 95,
  "mxRecords": true,
  "smtpServer": true,
  "smtpCheck": true,
  "acceptAll": false,
  "block": false,
  "gibberish": false,
  "disposable": false,
  "webmail": false,
  "regex": true
}`,
    inputSchema: z.object({
      email: z.string().describe('Email address to verify')
    }),
    outputSchema: z.object({
      email: z.string().describe('Email address that was verified'),
      result: z.string().describe('Deliverability result (e.g., "deliverable", "undeliverable")'),
      status: z.string().describe('Verification status (e.g., "valid", "invalid")'),
      score: z.number().describe('Confidence score (0-100)'),
      mxRecords: z.boolean().describe('Whether MX records exist for the domain'),
      smtpServer: z.boolean().describe('Whether SMTP server is reachable'),
      smtpCheck: z.boolean().describe('Whether SMTP check passed'),
      acceptAll: z.boolean().describe('Whether the server accepts all emails (catch-all)'),
      block: z.boolean().describe('Whether the email is blocked'),
      gibberish: z.boolean().describe('Whether the email looks like gibberish'),
      disposable: z.boolean().describe('Whether it is a disposable email address'),
      webmail: z.boolean().describe('Whether it is a webmail address (Gmail, Yahoo, etc.)'),
      regex: z.boolean().describe('Whether the email passes regex validation')
    }),
    integration: 'tomba' as const,
    method: 'emailVerifier' as const,
    credentialName
  })
}
