import type { FeatureKey } from "../billing-plans";

export type AuthRequirement = "none" | "storefront" | "customer";
export type ConfirmationRequirement = "required" | "optional";

export interface AIToolDefinition {
  name: string;
  description: string;
  parameters: Record<string, any>;
  authentication: AuthRequirement;
  confirmation: ConfirmationRequirement;
  feature: FeatureKey;
}

export const REGISTERED_AI_TOOLS: AIToolDefinition[] = [
  {
    name: "search_products",
    description: "Search store catalog for products matching a natural language query, category, or max price. Returns a small preview (3–6 products max) and shop link for complete catalog browsing.",
    authentication: "none",
    confirmation: "optional",
    feature: "shopify.products",
    parameters: {
      type: "object",
      properties: {
        query: { type: "string", description: "Search query or product name e.g. 'black t-shirt'" },
        category: { type: "string", description: "Optional category filter e.g. 'electronics' or 'shoes'" },
        maxPrice: { type: "number", description: "Maximum price filter e.g. 50" },
      },
      required: ["query"],
    },
  },
  {
    name: "get_product_details",
    description: "Get detailed information and variant IDs for a specific product handle or ID.",
    authentication: "none",
    confirmation: "optional",
    feature: "shopify.products",
    parameters: {
      type: "object",
      properties: {
        productHandle: { type: "string", description: "Product handle slug e.g. 'classic-cotton-t-shirt'" },
      },
      required: ["productHandle"],
    },
  },
  {
    name: "search_knowledge_base",
    description: "Query store FAQs, shipping policy, return policy, or general store terms.",
    authentication: "none",
    confirmation: "optional",
    feature: "ai.support",
    parameters: {
      type: "object",
      properties: {
        query: { type: "string", description: "FAQ or policy topic query e.g. 'shipping times' or 'refund policy'" },
      },
      required: ["query"],
    },
  },
  {
    name: "search_blog_posts",
    description: "Search store blog articles, buying guides, sizing charts, how-to tutorials, styling advice, product care tips, brand stories, and articles.",
    authentication: "none",
    confirmation: "optional",
    feature: "shopify.products",
    parameters: {
      type: "object",
      properties: {
        query: { type: "string", description: "Search query or topic e.g. 'sizing guide', 'how to clean shoes', or 'sustainable materials'" },
        category: { type: "string", description: "Optional blog category or handle filter e.g. 'guides', 'news', or 'tips'" },
      },
      required: ["query"],
    },
  },
  {
    name: "get_article_details",
    description: "Get the full body content, tips, and instructions for a specific store blog post or article.",
    authentication: "none",
    confirmation: "optional",
    feature: "shopify.products",
    parameters: {
      type: "object",
      properties: {
        articleHandle: { type: "string", description: "Article handle slug or ID e.g. 'ultimate-sizing-fit-measurement-guide'" },
      },
      required: ["articleHandle"],
    },
  },
  {
    name: "create_cart",
    description: "Create a new storefront cart with chosen variant items and generate a checkout link.",
    authentication: "storefront",
    confirmation: "optional",
    feature: "shopify.cart",
    parameters: {
      type: "object",
      properties: {
        variantId: { type: "string", description: "Shopify Product Variant ID" },
        quantity: { type: "number", description: "Item quantity (default 1)" },
      },
      required: ["variantId"],
    },
  },
  {
    name: "add_to_cart",
    description: "Add items to an existing cart or create a new cart and return the checkout link.",
    authentication: "storefront",
    confirmation: "optional",
    feature: "shopify.cart",
    parameters: {
      type: "object",
      properties: {
        cartId: { type: "string", description: "Optional existing Storefront Cart ID" },
        variantId: { type: "string", description: "Shopify Product Variant ID" },
        quantity: { type: "number", description: "Item quantity (default 1)" },
      },
      required: ["variantId"],
    },
  },
  {
    name: "get_customer_orders",
    description: "Fetch order history or specific order status for authenticated customer.",
    authentication: "customer",
    confirmation: "optional",
    feature: "ai.support",
    parameters: {
      type: "object",
      properties: {
        orderId: { type: "string", description: "Optional specific Order ID or Order Name e.g. '#1001'" },
      },
    },
  },
  {
    name: "cancel_order",
    description: "Cancel an order for the authenticated customer. REQUIRES explicit customer confirmation.",
    authentication: "customer",
    confirmation: "required",
    feature: "ai.support",
    parameters: {
      type: "object",
      properties: {
        orderId: { type: "string", description: "Order ID to cancel e.g. '#1001'" },
        confirmed: { type: "boolean", description: "Must be true if user explicitly confirmed cancellation in chat" },
      },
      required: ["orderId"],
    },
  },
  {
    name: "create_return",
    description: "Initiate a return request for an order. REQUIRES explicit customer confirmation.",
    authentication: "customer",
    confirmation: "required",
    feature: "ai.support",
    parameters: {
      type: "object",
      properties: {
        orderId: { type: "string", description: "Order ID for return e.g. '#1001'" },
        reason: { type: "string", description: "Reason for return e.g. 'wrong size'" },
        confirmed: { type: "boolean", description: "Must be true if user explicitly confirmed return request in chat" },
      },
      required: ["orderId"],
    },
  },
  {
    name: "get_available_discount_codes",
    description: "Fetch currently active/public discount codes, promotions, eligibility requirements, and applicable discounts for the store.",
    authentication: "none",
    confirmation: "optional",
    feature: "shopify.cart",
    parameters: {
      type: "object",
      properties: {},
    },
  },
  {
    name: "validate_discount_code",
    description: "Check whether a specific discount or promo code is valid and applicable to the customer's cart before attempting to apply it.",
    authentication: "none",
    confirmation: "optional",
    feature: "shopify.cart",
    parameters: {
      type: "object",
      properties: {
        code: { type: "string", description: "Promo/coupon code to validate e.g. 'SAVE20'" },
      },
      required: ["code"],
    },
  },
  {
    name: "apply_discount_code",
    description: "Apply a verified promo/discount coupon code to the current cart and return updated savings and checkout link.",
    authentication: "none",
    confirmation: "optional",
    feature: "shopify.cart",
    parameters: {
      type: "object",
      properties: {
        code: { type: "string", description: "Discount or coupon code e.g. 'SAVE20' or 'WELCOME10'" },
        cartId: { type: "string", description: "Optional active Storefront Cart ID" },
      },
      required: ["code"],
    },
  },
  {
    name: "calculate_shipping_rates",
    description: "Calculate available Shopify shipping methods and delivery costs for a destination address.",
    authentication: "none",
    confirmation: "optional",
    feature: "shopify.checkout",
    parameters: {
      type: "object",
      properties: {
        country: { type: "string", description: "Destination country e.g. 'India' or 'United States'" },
        zip: { type: "string", description: "Destination ZIP or Postal Code e.g. '90210' or '110001'" },
        province: { type: "string", description: "State or province e.g. 'California' or 'Delhi'" },
        city: { type: "string", description: "City name" },
      },
      required: ["country", "zip"],
    },
  },
  {
    name: "track_order",
    description: "Retrieve order status, line items, and tracking details by order number and customer email or postal code.",
    authentication: "none",
    confirmation: "optional",
    feature: "ai.support",
    parameters: {
      type: "object",
      properties: {
        orderNumber: { type: "string", description: "Shopify Order number or ID e.g. '#1001' or '1001'" },
        emailOrZip: { type: "string", description: "Customer email address or shipping postal code for security verification" },
      },
      required: ["orderNumber", "emailOrZip"],
    },
  },
  {
    name: "update_cart_quantity",
    description: "Update the quantity of a specific product variant in the active cart or remove it.",
    authentication: "storefront",
    confirmation: "optional",
    feature: "shopify.cart",
    parameters: {
      type: "object",
      properties: {
        variantId: { type: "string", description: "Shopify Product Variant ID" },
        quantity: { type: "number", description: "New quantity (0 to remove item)" },
      },
      required: ["variantId", "quantity"],
    },
  },
  {
    name: "handoff_to_human",
    description: "Escalate conversation to merchant human support team when query cannot be resolved.",
    authentication: "none",
    confirmation: "optional",
    feature: "ai.support",
    parameters: {
      type: "object",
      properties: {
        reason: { type: "string", description: "Reason for escalation" },
      },
      required: ["reason"],
    },
  },
  {
    name: "get_store_contact_info",
    description: "Get the store's verified customer support contact information including phone numbers, helpline departments, support email addresses, operating hours, and contact page link.",
    authentication: "none",
    confirmation: "optional",
    feature: "shopify.products",
    parameters: {
      type: "object",
      properties: {
        department: { type: "string", description: "Optional department filter e.g. 'support', 'billing', 'technical'" },
      },
    },
  },
];
