import { z } from 'zod';
import { ShopifyClient } from '../clients/shopify.js';

const ListCustomersInput = z.object({
  limit: z.number().min(1).max(250).default(50).describe('Results per page'),
  page_info: z.string().optional().describe('Cursor for pagination'),
  created_at_min: z.string().optional().describe('Filter by min creation date (ISO 8601)'),
  created_at_max: z.string().optional().describe('Filter by max creation date (ISO 8601)'),
  updated_at_min: z.string().optional().describe('Filter by min update date (ISO 8601)'),
  fields: z.string().optional().describe('Comma-separated list of fields to retrieve'),
});

const GetCustomerInput = z.object({
  id: z.string().describe('Customer ID'),
  fields: z.string().optional().describe('Comma-separated list of fields to retrieve'),
});

const CreateCustomerInput = z.object({
  email: z.string().optional().describe('Customer email'),
  first_name: z.string().optional().describe('First name'),
  last_name: z.string().optional().describe('Last name'),
  phone: z.string().optional().describe('Phone number'),
  tags: z.string().optional().describe('Comma-separated tags'),
  note: z.string().optional().describe('Customer note'),
  verified_email: z.boolean().optional().describe('Email is verified'),
  send_email_welcome: z.boolean().optional().describe('Send welcome email'),
  addresses: z.array(z.object({
    address1: z.string().optional().describe('Address line 1'),
    address2: z.string().optional().describe('Address line 2'),
    city: z.string().optional().describe('City'),
    province: z.string().optional().describe('State/Province'),
    country: z.string().optional().describe('Country'),
    zip: z.string().optional().describe('Postal code'),
    phone: z.string().optional().describe('Phone number'),
    first_name: z.string().optional().describe('First name'),
    last_name: z.string().optional().describe('Last name'),
  })).optional().describe('Customer addresses'),
});

const UpdateCustomerInput = z.object({
  id: z.string().describe('Customer ID'),
  email: z.string().optional().describe('Customer email'),
  first_name: z.string().optional().describe('First name'),
  last_name: z.string().optional().describe('Last name'),
  phone: z.string().optional().describe('Phone number'),
  tags: z.string().optional().describe('Comma-separated tags'),
  note: z.string().optional().describe('Customer note'),
  verified_email: z.boolean().optional().describe('Email is verified'),
});

const DeleteCustomerInput = z.object({
  id: z.string().describe('Customer ID'),
});

const SearchCustomersInput = z.object({
  query: z.string().describe('Search query (email, name, phone, etc.)'),
  limit: z.number().min(1).max(250).default(50).describe('Results per page'),
  fields: z.string().optional().describe('Comma-separated list of fields to retrieve'),
});

export default [
    {
      name: 'shopify_list_customers',
      description: 'List customers with pagination and date filtering',
      inputSchema: {
        type: 'object' as const,
        properties: {
          limit: { type: 'number', description: 'Results per page', default: 50, minimum: 1, maximum: 250 },
          page_info: { type: 'string', description: 'Cursor for pagination' },
          created_at_min: { type: 'string', description: 'Filter by min creation date (ISO 8601)' },
          created_at_max: { type: 'string', description: 'Filter by max creation date (ISO 8601)' },
          updated_at_min: { type: 'string', description: 'Filter by min update date (ISO 8601)' },
          fields: { type: 'string', description: 'Comma-separated list of fields to retrieve' },
        },
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = ListCustomersInput.parse(input);
        const results = await client.list('/customers.json', { params: validated });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(results, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_get_customer',
      description: 'Get a specific customer by ID with optional field filtering',
      inputSchema: {
        type: 'object' as const,
        properties: {
          id: { type: 'string', description: 'Customer ID' },
          fields: { type: 'string', description: 'Comma-separated list of fields to retrieve' },
        },
        required: ['id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = GetCustomerInput.parse(input);
        const result = await client.get(`/customers/${validated.id}.json`, {
          params: validated.fields ? { fields: validated.fields } : {},
        });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_create_customer',
      description: 'Create a new customer with optional addresses',
      inputSchema: {
        type: 'object' as const,
        properties: {
          email: { type: 'string', description: 'Customer email' },
          first_name: { type: 'string', description: 'First name' },
          last_name: { type: 'string', description: 'Last name' },
          phone: { type: 'string', description: 'Phone number' },
          tags: { type: 'string', description: 'Comma-separated tags' },
          note: { type: 'string', description: 'Customer note' },
          verified_email: { type: 'boolean', description: 'Email is verified' },
          send_email_welcome: { type: 'boolean', description: 'Send welcome email' },
          addresses: {
            type: 'array',
            description: 'Customer addresses',
            items: {
              type: 'object',
              properties: {
                address1: { type: 'string', description: 'Address line 1' },
                address2: { type: 'string', description: 'Address line 2' },
                city: { type: 'string', description: 'City' },
                province: { type: 'string', description: 'State/Province' },
                country: { type: 'string', description: 'Country' },
                zip: { type: 'string', description: 'Postal code' },
                phone: { type: 'string', description: 'Phone number' },
                first_name: { type: 'string', description: 'First name' },
                last_name: { type: 'string', description: 'Last name' },
              },
            },
          },
        },
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = CreateCustomerInput.parse(input);
        const result = await client.create('/customers.json', { customer: validated });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_update_customer',
      description: 'Update an existing customer',
      inputSchema: {
        type: 'object' as const,
        properties: {
          id: { type: 'string', description: 'Customer ID' },
          email: { type: 'string', description: 'Customer email' },
          first_name: { type: 'string', description: 'First name' },
          last_name: { type: 'string', description: 'Last name' },
          phone: { type: 'string', description: 'Phone number' },
          tags: { type: 'string', description: 'Comma-separated tags' },
          note: { type: 'string', description: 'Customer note' },
          verified_email: { type: 'boolean', description: 'Email is verified' },
        },
        required: ['id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = UpdateCustomerInput.parse(input);
        const { id, ...updates } = validated;
        const result = await client.update(`/customers/${id}.json`, { customer: updates });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_delete_customer',
      description: 'Delete a customer by ID',
      inputSchema: {
        type: 'object' as const,
        properties: {
          id: { type: 'string', description: 'Customer ID' },
        },
        required: ['id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = DeleteCustomerInput.parse(input);
        await client.delete(`/customers/${validated.id}.json`);
        return {
          content: [{ type: 'text' as const, text: `Customer ${validated.id} deleted successfully` }],
        };
      },
    },
    {
      name: 'shopify_search_customers',
      description: 'Search customers by query (email, name, phone, address, etc.)',
      inputSchema: {
        type: 'object' as const,
        properties: {
          query: { type: 'string', description: 'Search query (email, name, phone, etc.)' },
          limit: { type: 'number', description: 'Results per page', default: 50, minimum: 1, maximum: 250 },
          fields: { type: 'string', description: 'Comma-separated list of fields to retrieve' },
        },
        required: ['query'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = SearchCustomersInput.parse(input);
        const results = await client.list('/customers/search.json', { params: validated });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(results, null, 2) }],
        };
      },
    },
  ];
