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

const smsTools: ToolDef[] = [
  {
    name: 'sms_contacts_list',
    description: 'List SMS contact lists. Each list contains phone contacts for campaign targeting.',
    inputSchema: {
      page: z.coerce.number().int().positive().default(1).describe('Page number'),
      limit: z.coerce.number().int().positive().max(100).default(20).describe('Items per page'),
    },
    method: 'GET',
    path: '/api/sms/contacts',
    tier: 2,
  },
  {
    name: 'sms_contacts_get',
    description: 'Get details of a specific contact list including entry count and sample contacts.',
    inputSchema: {
      id: z.string().describe('Contact list ID'),
    },
    method: 'GET',
    path: '/api/sms/contacts/:id',
    tier: 2,
  },
  {
    name: 'sms_campaigns_list',
    description:
      'List SMS campaigns with their status (DRAFT, SENDING, COMPLETED, FAILED), ' +
      'delivery stats, and linked advocacy campaign.',
    inputSchema: {
      page: z.coerce.number().int().positive().default(1).describe('Page number'),
      limit: z.coerce.number().int().positive().max(100).default(20).describe('Items per page'),
    },
    method: 'GET',
    path: '/api/sms/campaigns',
    tier: 2,
  },
  {
    name: 'sms_campaigns_create',
    description:
      'Create a new SMS campaign. Requires a message template and contact list. ' +
      'The campaign starts in DRAFT status and must be explicitly started.',
    inputSchema: {
      name: z.string().min(1).max(200).describe('Campaign name'),
      messageTemplate: z.string().min(1).max(1600).describe('SMS message text (max 1600 chars)'),
      contactListId: z.string().describe('Contact list to target'),
      advocacyCampaignId: z.string().nullable().optional()
        .describe('Link to an advocacy campaign (optional)'),
      delayBetweenMs: z.number().int().min(1000).max(60000).default(3000)
        .describe('Delay between sends in ms (default 3000)'),
    },
    method: 'POST',
    path: '/api/sms/campaigns',
    tier: 2,
  },
  {
    name: 'sms_conversations_list',
    description: 'List SMS conversation threads with recent messages and response classification.',
    inputSchema: {
      page: z.coerce.number().int().positive().default(1).describe('Page number'),
      limit: z.coerce.number().int().positive().max(100).default(20).describe('Items per page'),
    },
    method: 'GET',
    path: '/api/sms/conversations',
    tier: 2,
  },
  {
    name: 'sms_device_status',
    description:
      'Check the status of the SMS sending device (Termux Android bridge). ' +
      'Returns connectivity, battery level, and queue depth.',
    inputSchema: {},
    method: 'GET',
    path: '/api/sms/device/status',
    tier: 2,
  },
  {
    name: 'sms_send_message',
    description: 'Send an ad-hoc SMS message to a specific phone number.',
    inputSchema: {
      to: z.string().describe('Recipient phone number'),
      message: z.string().min(1).max(1600).describe('SMS message text'),
    },
    method: 'POST',
    path: '/api/sms/messages/send',
    tier: 2,
  },
];

export const smsPack: ToolPack = {
  name: 'sms',
  description: 'SMS contacts, campaigns, conversations, and device management',
  tools: smsTools,
};
