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

const paymentTools: ToolDef[] = [
  {
    name: 'donations_list',
    description:
      'List donations with filtering by status (PENDING, COMPLETED, FAILED, REFUNDED), ' +
      'type (product or donation), and search by donor email/name.',
    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'),
      status: z.enum(['PENDING', 'COMPLETED', 'FAILED', 'REFUNDED']).optional()
        .describe('Filter by order status'),
      type: z.enum(['product', 'donation']).optional()
        .describe('Filter by order type'),
      search: z.string().optional().describe('Search by donor name or email'),
    },
    method: 'GET',
    path: '/api/payments/admin/orders',
    tier: 2,
  },
  {
    name: 'donations_stats',
    description:
      'Get donation statistics: total revenue, donation count, average amount, ' +
      'recent trends.',
    inputSchema: {},
    method: 'GET',
    path: '/api/payments/admin/stats',
    tier: 2,
  },
  {
    name: 'donations_refund',
    description: 'Refund a donation by order ID. Processes the refund via Stripe.',
    inputSchema: {
      id: z.string().describe('Order ID to refund'),
      reason: z.string().max(500).optional().describe('Reason for refund'),
    },
    method: 'POST',
    path: '/api/payments/admin/orders/:id/refund',
    tier: 2,
  },
  {
    name: 'products_list',
    description:
      'List products (digital goods, events, donation items) available in the shop.',
    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'),
    },
    method: 'GET',
    path: '/api/payments/admin/products',
    tier: 2,
  },
  {
    name: 'products_create',
    description:
      'Create a new product. Types: DIGITAL (downloadable), EVENT (ticketed), ' +
      'DONATION (cause-based). Price is in cents (CAD).',
    inputSchema: {
      title: z.string().min(1).max(200).describe('Product title'),
      slug: z.string().min(1).max(200).regex(/^[a-z0-9-]+$/)
        .describe('URL slug (lowercase, hyphens only)'),
      description: z.string().max(5000).nullable().optional().describe('Product description'),
      priceCAD: z.number().int().min(0).describe('Price in cents CAD (e.g., 1000 = $10.00)'),
      type: z.enum(['DIGITAL', 'EVENT', 'DONATION']).describe('Product type'),
      isActive: z.boolean().optional().describe('Whether product is listed publicly'),
    },
    method: 'POST',
    path: '/api/payments/admin/products',
    tier: 2,
  },
  {
    name: 'products_update',
    description: 'Update an existing product. Only provided fields are changed.',
    inputSchema: {
      id: z.string().describe('Product ID'),
      title: z.string().min(1).max(200).optional().describe('Product title'),
      description: z.string().max(5000).nullable().optional().describe('Description'),
      priceCAD: z.number().int().min(0).optional().describe('Price in cents CAD'),
      isActive: z.boolean().optional().describe('Active status'),
    },
    method: 'PUT',
    path: '/api/payments/admin/products/:id',
    tier: 2,
  },
  {
    name: 'plans_list',
    description:
      'List subscription plans with pricing, features, and subscriber counts.',
    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'),
      isActive: z.enum(['true', 'false']).optional().describe('Filter by active status'),
    },
    method: 'GET',
    path: '/api/payments/admin/plans',
    tier: 2,
  },
  {
    name: 'subscriptions_list',
    description:
      'List active subscriptions with their status (active, cancelled, grace_period, delinquent).',
    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'),
      status: z.enum(['active', 'cancelled', 'grace_period', 'delinquent', 'none', 'lifetime']).optional()
        .describe('Filter by subscription status'),
      planId: z.coerce.number().int().optional().describe('Filter by plan'),
      search: z.string().optional().describe('Search by subscriber name or email'),
    },
    method: 'GET',
    path: '/api/payments/admin/subscriptions',
    tier: 2,
  },
  {
    name: 'payment_settings_get',
    description: 'Get payment configuration: Stripe status, currency, donation settings.',
    inputSchema: {},
    method: 'GET',
    path: '/api/payments/admin/settings',
    tier: 2,
  },
];

export const paymentsPack: ToolPack = {
  name: 'payments',
  description: 'Donations, products, subscription plans, and payment configuration',
  tools: paymentTools,
};
