import { z } from 'zod';
import type { ToolDef, ToolPack } from '../../tool-registry.js';

const EmailTemplateCategory = z.enum([
  'ADVOCACY', 'NEWSLETTER', 'NOTIFICATION', 'WELCOME',
  'SHIFT_CONFIRMATION', 'CUSTOM',
]);

const emailTools: ToolDef[] = [
  {
    name: 'email_queue_status',
    description:
      'Get the BullMQ email queue status: active, waiting, completed, failed job counts. ' +
      'Useful for monitoring advocacy email delivery.',
    inputSchema: {},
    method: 'GET',
    path: '/api/email-queue/status',
    tier: 2,
  },
  {
    name: 'email_queue_jobs',
    description: 'List jobs in the email queue with their status and details.',
    inputSchema: {
      status: z.enum(['active', 'waiting', 'completed', 'failed']).optional()
        .describe('Filter by job status'),
      page: z.coerce.number().int().positive().default(1).describe('Page number'),
      limit: z.coerce.number().int().positive().max(50).default(20).describe('Items per page'),
    },
    method: 'GET',
    path: '/api/email-queue/jobs',
    tier: 2,
  },
  {
    name: 'email_templates_list',
    description:
      'List email templates. Filter by category (ADVOCACY, NEWSLETTER, NOTIFICATION, etc.) ' +
      'or active status.',
    inputSchema: {
      page: z.coerce.number().int().min(1).default(1).describe('Page number'),
      limit: z.coerce.number().int().min(1).max(100).default(20).describe('Items per page'),
      search: z.string().optional().describe('Search by template name'),
      category: EmailTemplateCategory.optional().describe('Filter by template category'),
      isActive: z.coerce.boolean().optional().describe('Filter by active status'),
    },
    method: 'GET',
    path: '/api/email-templates',
    tier: 2,
  },
  {
    name: 'email_templates_get',
    description: 'Get full details of an email template including HTML content, variables, and version history.',
    inputSchema: {
      id: z.string().describe('Email template ID'),
    },
    method: 'GET',
    path: '/api/email-templates/:id',
    tier: 2,
  },
  {
    name: 'email_templates_create',
    description:
      'Create a new email template with HTML and text content. ' +
      'Templates support variables ({{VARIABLE_NAME}}) for personalization.',
    inputSchema: {
      key: z.string().regex(/^[a-z0-9-]+$/).min(1).max(100)
        .describe('Unique template key (lowercase alphanumeric with hyphens)'),
      name: z.string().min(1).max(200).describe('Template display name'),
      description: z.string().max(1000).optional().describe('Template description'),
      category: EmailTemplateCategory.describe('Template category'),
      subjectLine: z.string().min(1).max(500).describe('Email subject line'),
      htmlContent: z.string().min(1).max(100000).describe('HTML email content'),
      textContent: z.string().min(1).max(50000).describe('Plain text fallback content'),
      isActive: z.boolean().default(true).describe('Whether template is active'),
    },
    method: 'POST',
    path: '/api/email-templates',
    tier: 2,
  },
  {
    name: 'email_templates_update',
    description: 'Update an existing email template. Only provided fields are changed.',
    inputSchema: {
      id: z.string().describe('Email template ID'),
      name: z.string().min(1).max(200).optional().describe('Template name'),
      subjectLine: z.string().min(1).max(500).optional().describe('Email subject'),
      htmlContent: z.string().min(1).max(100000).optional().describe('HTML content'),
      textContent: z.string().min(1).max(50000).optional().describe('Text content'),
      isActive: z.boolean().optional().describe('Active status'),
    },
    method: 'PUT',
    path: '/api/email-templates/:id',
    tier: 2,
  },
  {
    name: 'listmonk_sync_status',
    description:
      'Get Listmonk newsletter sync status: last sync time, synced subscribers, ' +
      'list counts, and sync health.',
    inputSchema: {},
    method: 'GET',
    path: '/api/listmonk/sync/status',
    tier: 2,
  },
  {
    name: 'listmonk_trigger_sync',
    description: 'Trigger a manual Listmonk subscriber sync. Syncs platform users → Listmonk lists.',
    inputSchema: {},
    method: 'POST',
    path: '/api/listmonk/sync/trigger',
    tier: 2,
  },
];

export const emailPack: ToolPack = {
  name: 'email',
  description: 'Email queue monitoring, email templates, and Listmonk newsletter sync',
  tools: emailTools,
};
