import { z } from 'zod'
import type { Tool } from '../../../../types'
import { createIntegrationTool } from '../../../tool'
import { EmailSchema } from '../../../../../../../platform/utils/validation'

/**
 * Create a tool that verifies email addresses via MillionVerifier
 *
 * @param credentialName - Name of the MillionVerifier credential to use
 * @returns Tool instance
 *
 * @example
 * ```typescript
 * const tool = createMillionVerifierVerifyEmailTool('elevasis-millionverifier')
 * const result = await tool.execute({
 *   email: 'john@example.com'
 * }, context)
 * // result.result: 'ok' | 'catch_all' | 'unknown' | 'error' | 'disposable' | 'invalid'
 * // result.quality: 'good' | 'bad' | 'risky' | ''
 * ```
 */
export function createMillionVerifierVerifyEmailTool(credentialName: string): Tool {
  return createIntegrationTool({
    name: 'millionverifier_verify_email',
    description: `Verify email address deliverability using MillionVerifier.

Use this tool to validate if an email address is real and can receive emails before
adding it to a campaign. MillionVerifier is highly accurate and fast (160 req/s).

Result codes:
- ok (1): Email is valid and deliverable
- catch_all (2): Domain accepts all email (cannot confirm individual mailbox) - FREE
- unknown (3): Result could not be determined - FREE
- error (4): Verification error
- disposable (5): Disposable/temporary email address
- invalid (6): Email address is invalid

Quality signals:
- good: Safe to send
- risky: May bounce or flag spam filter
- bad: Do not send

Credits are only deducted for ok, invalid, and disposable results.

Returns full verification details including free/role detection and suggested correction.`,
    inputSchema: z.object({
      email: EmailSchema.describe('Email address to verify')
    }),
    outputSchema: z.object({
      email: z.string().describe('Verified email address'),
      quality: z.enum(['', 'good', 'bad', 'risky']).describe('Quality signal for sending decision'),
      result: z.enum(['ok', 'catch_all', 'unknown', 'error', 'disposable', 'invalid']).describe('Verification result'),
      resultCode: z
        .number()
        .describe('Numeric result code (1=ok, 2=catch_all, 3=unknown, 4=error, 5=disposable, 6=invalid)'),
      subresult: z.string().describe('Detailed sub-result (e.g., ok, no_mailbox, dns_no_mx)'),
      free: z.boolean().describe('Whether this is a free email provider (Gmail, Yahoo, etc.)'),
      role: z.boolean().describe('Whether this is a role-based address (info@, support@, etc.)'),
      didYouMean: z.string().describe('Suggested correction if email looks like a typo (empty if none)'),
      credits: z.number().describe('Remaining credits after this verification'),
      executionTime: z.number().describe('Time taken to verify in milliseconds'),
      error: z.string().describe('Error message when result is error (empty otherwise)'),
      liveMode: z.boolean().describe('Whether the verification was performed in live mode')
    }),
    integration: 'millionverifier' as const,
    method: 'verifyEmail' as const,
    credentialName
  })
}

/**
 * Create a tool that checks remaining MillionVerifier credits
 *
 * @param credentialName - Name of the MillionVerifier credential to use
 * @returns Tool instance
 *
 * @example
 * ```typescript
 * const tool = createMillionVerifierCheckCreditsTool('elevasis-millionverifier')
 * const result = await tool.execute({}, context)
 * // result.credits: 5000
 * ```
 */
export function createMillionVerifierCheckCreditsTool(credentialName: string): Tool {
  return createIntegrationTool({
    name: 'millionverifier_check_credits',
    description: `Check remaining MillionVerifier verification credits.

Use this tool to monitor credit balance before running large verification batches.

Returns current credits, bulk credits, renewing credits, and plan tier.`,
    inputSchema: z.object({}),
    outputSchema: z.object({
      credits: z.number().describe('Remaining single-verification credits'),
      bulkCredits: z.number().describe('Remaining bulk verification credits'),
      renewingCredits: z.number().describe('Credits that will renew on next billing cycle'),
      plan: z.number().describe('Current plan tier')
    }),
    integration: 'millionverifier' as const,
    method: 'checkCredits' as const,
    credentialName
  })
}
