import { ShopifyIcon } from '@/components/icons'
import { getScopesForService } from '@/lib/oauth/utils'
import type { BlockConfig, BlockMeta } from '@/blocks/types'
import { AuthMode, IntegrationType } from '@/blocks/types'
import { parseOptionalBooleanInput, parseOptionalNumberInput } from '@/blocks/utils'

interface ShopifyResponse {
  success: boolean
  error?: string
  output: Record<string, unknown>
}

const LIST_OPERATIONS = [
  'shopify_list_products',
  'shopify_list_orders',
  'shopify_list_customers',
  'shopify_list_inventory_items',
  'shopify_list_locations',
  'shopify_list_collections',
] as const

export const ShopifyBlock: BlockConfig<ShopifyResponse> = {
  type: 'shopify',
  name: 'Shopify',
  description: 'Manage products, orders, customers, and inventory in your Shopify store',
  authMode: AuthMode.OAuth,
  longDescription:
    'Integrate Shopify into your workflow. Manage products, orders, customers, and inventory. Create, read, update, and delete products. List and manage orders. Handle customer data and adjust inventory levels.',
  docsLink: 'https://docs.sim.ai/integrations/shopify',
  category: 'tools',
  integrationType: IntegrationType.Commerce,
  icon: ShopifyIcon,
  bgColor: '#FFFFFF',
  subBlocks: [
    {
      id: 'operation',
      title: 'Operation',
      type: 'dropdown',
      options: [
        // Product Operations
        { label: 'Create Product', id: 'shopify_create_product' },
        { label: 'Get Product', id: 'shopify_get_product' },
        { label: 'List Products', id: 'shopify_list_products' },
        { label: 'Update Product', id: 'shopify_update_product' },
        { label: 'Delete Product', id: 'shopify_delete_product' },
        // Order Operations
        { label: 'Get Order', id: 'shopify_get_order' },
        { label: 'List Orders', id: 'shopify_list_orders' },
        { label: 'Update Order', id: 'shopify_update_order' },
        { label: 'Cancel Order', id: 'shopify_cancel_order' },
        // Customer Operations
        { label: 'Create Customer', id: 'shopify_create_customer' },
        { label: 'Get Customer', id: 'shopify_get_customer' },
        { label: 'List Customers', id: 'shopify_list_customers' },
        { label: 'Update Customer', id: 'shopify_update_customer' },
        { label: 'Delete Customer', id: 'shopify_delete_customer' },
        // Inventory Operations
        { label: 'List Inventory Items', id: 'shopify_list_inventory_items' },
        { label: 'Get Inventory Level', id: 'shopify_get_inventory_level' },
        { label: 'Adjust Inventory', id: 'shopify_adjust_inventory' },
        // Location Operations
        { label: 'List Locations', id: 'shopify_list_locations' },
        // Fulfillment Operations
        { label: 'Create Fulfillment', id: 'shopify_create_fulfillment' },
        // Collection Operations
        { label: 'List Collections', id: 'shopify_list_collections' },
        { label: 'Get Collection', id: 'shopify_get_collection' },
      ],
      value: () => 'shopify_list_products',
    },
    {
      id: 'credential',
      title: 'Shopify Account',
      type: 'oauth-input',
      serviceId: 'shopify',
      canonicalParamId: 'oauthCredential',
      mode: 'basic',
      requiredScopes: getScopesForService('shopify'),
      placeholder: 'Select Shopify account',
      required: true,
    },
    {
      id: 'manualCredential',
      title: 'Shopify Account',
      type: 'short-input',
      canonicalParamId: 'oauthCredential',
      mode: 'advanced',
      placeholder: 'Enter credential ID',
      required: true,
    },
    {
      id: 'shopDomain',
      title: 'Shop Domain',
      type: 'short-input',
      placeholder: 'Auto-detected from OAuth or enter manually',
      hidden: true,
    },
    // Product ID (for get/update/delete operations)
    {
      id: 'productId',
      title: 'Product ID',
      type: 'short-input',
      placeholder: 'gid://shopify/Product/123456789',
      required: true,
      condition: {
        field: 'operation',
        value: ['shopify_get_product', 'shopify_update_product', 'shopify_delete_product'],
      },
    },
    // Product Title (for create/update)
    {
      id: 'title',
      title: 'Product Title',
      type: 'short-input',
      placeholder: 'Enter product title',
      required: {
        field: 'operation',
        value: ['shopify_create_product'],
      },
      condition: {
        field: 'operation',
        value: ['shopify_create_product', 'shopify_update_product'],
      },
    },
    // Product Description
    {
      id: 'descriptionHtml',
      title: 'Description (HTML)',
      type: 'long-input',
      placeholder: 'Enter product description',
      condition: {
        field: 'operation',
        value: ['shopify_create_product', 'shopify_update_product'],
      },
    },
    // Product Type
    {
      id: 'productType',
      title: 'Product Type',
      type: 'short-input',
      placeholder: 'e.g., Shoes, Electronics',
      condition: {
        field: 'operation',
        value: ['shopify_create_product', 'shopify_update_product'],
      },
    },
    // Vendor
    {
      id: 'vendor',
      title: 'Vendor',
      type: 'short-input',
      placeholder: 'Enter vendor name',
      condition: {
        field: 'operation',
        value: ['shopify_create_product', 'shopify_update_product'],
      },
    },
    // Tags
    {
      id: 'tags',
      title: 'Tags',
      type: 'short-input',
      placeholder: 'tag1, tag2, tag3 (comma-separated)',
      condition: {
        field: 'operation',
        value: ['shopify_create_product', 'shopify_update_product'],
      },
    },
    // Status
    {
      id: 'status',
      title: 'Status',
      type: 'dropdown',
      options: [
        { label: 'Active', id: 'ACTIVE' },
        { label: 'Draft', id: 'DRAFT' },
        { label: 'Archived', id: 'ARCHIVED' },
      ],
      value: () => 'ACTIVE',
      condition: {
        field: 'operation',
        value: ['shopify_create_product', 'shopify_update_product'],
      },
    },
    // Query for listing products
    {
      id: 'productQuery',
      title: 'Search Query',
      type: 'short-input',
      placeholder: 'Filter products (optional)',
      mode: 'advanced',
      condition: {
        field: 'operation',
        value: ['shopify_list_products'],
      },
    },
    // Query for listing customers
    {
      id: 'customerQuery',
      title: 'Search Query',
      type: 'short-input',
      placeholder: 'e.g., first_name:John OR email:*@gmail.com',
      mode: 'advanced',
      condition: {
        field: 'operation',
        value: ['shopify_list_customers'],
      },
    },
    // Query for listing inventory items
    {
      id: 'inventoryQuery',
      title: 'Search Query',
      type: 'short-input',
      placeholder: 'e.g., sku:ABC123',
      mode: 'advanced',
      condition: {
        field: 'operation',
        value: ['shopify_list_inventory_items'],
      },
    },
    {
      id: 'first',
      title: 'Max Results',
      type: 'short-input',
      placeholder: 'Defaults to 50, max 250',
      mode: 'advanced',
      condition: {
        field: 'operation',
        value: [...LIST_OPERATIONS],
      },
    },
    // Order ID
    {
      id: 'orderId',
      title: 'Order ID',
      type: 'short-input',
      placeholder: 'gid://shopify/Order/123456789',
      required: true,
      condition: {
        field: 'operation',
        value: ['shopify_get_order', 'shopify_update_order', 'shopify_cancel_order'],
      },
    },
    // Order Status (for listing)
    {
      id: 'orderStatus',
      title: 'Order Status',
      type: 'dropdown',
      options: [
        { label: 'Any', id: 'any' },
        { label: 'Open', id: 'open' },
        { label: 'Closed', id: 'closed' },
        { label: 'Cancelled', id: 'cancelled' },
      ],
      value: () => 'any',
      condition: {
        field: 'operation',
        value: ['shopify_list_orders'],
      },
    },
    {
      id: 'orderQuery',
      title: 'Search Query',
      type: 'short-input',
      placeholder: 'e.g., financial_status:paid OR email:customer@example.com',
      mode: 'advanced',
      condition: {
        field: 'operation',
        value: ['shopify_list_orders'],
      },
    },
    // Order Note (for update)
    {
      id: 'orderNote',
      title: 'Order Note',
      type: 'long-input',
      placeholder: 'Enter order note',
      condition: {
        field: 'operation',
        value: ['shopify_update_order'],
      },
    },
    // Order Email (for update)
    {
      id: 'orderEmail',
      title: 'Customer Email',
      type: 'short-input',
      placeholder: 'customer@example.com',
      condition: {
        field: 'operation',
        value: ['shopify_update_order'],
      },
    },
    // Order Tags (for update)
    {
      id: 'orderTags',
      title: 'Order Tags',
      type: 'short-input',
      placeholder: 'tag1, tag2, tag3 (comma-separated)',
      condition: {
        field: 'operation',
        value: ['shopify_update_order'],
      },
    },
    // Cancel Order Reason
    {
      id: 'cancelReason',
      title: 'Cancel Reason',
      type: 'dropdown',
      options: [
        { label: 'Customer Request', id: 'CUSTOMER' },
        { label: 'Declined Payment', id: 'DECLINED' },
        { label: 'Fraud', id: 'FRAUD' },
        { label: 'Inventory Issue', id: 'INVENTORY' },
        { label: 'Staff Error', id: 'STAFF' },
        { label: 'Other', id: 'OTHER' },
      ],
      value: () => 'OTHER',
      required: true,
      condition: {
        field: 'operation',
        value: ['shopify_cancel_order'],
      },
    },
    // Staff Note (for cancel order)
    {
      id: 'staffNote',
      title: 'Staff Note',
      type: 'long-input',
      placeholder: 'Internal note about this cancellation',
      condition: {
        field: 'operation',
        value: ['shopify_cancel_order'],
      },
    },
    {
      id: 'restock',
      title: 'Restock Inventory',
      type: 'dropdown',
      options: [
        { label: 'Yes', id: 'true' },
        { label: 'No', id: 'false' },
      ],
      value: () => 'false',
      required: true,
      mode: 'advanced',
      condition: {
        field: 'operation',
        value: ['shopify_cancel_order'],
      },
    },
    {
      id: 'cancelNotifyCustomer',
      title: 'Notify Customer',
      type: 'dropdown',
      options: [
        { label: 'Yes', id: 'true' },
        { label: 'No', id: 'false' },
      ],
      value: () => 'false',
      mode: 'advanced',
      condition: {
        field: 'operation',
        value: ['shopify_cancel_order'],
      },
    },
    {
      id: 'refundOriginalPayment',
      title: 'Refund to Original Payment Method',
      type: 'dropdown',
      options: [
        { label: 'Yes', id: 'true' },
        { label: 'No', id: 'false' },
      ],
      value: () => 'false',
      mode: 'advanced',
      condition: {
        field: 'operation',
        value: ['shopify_cancel_order'],
      },
    },
    // Customer ID
    {
      id: 'customerId',
      title: 'Customer ID',
      type: 'short-input',
      placeholder: 'gid://shopify/Customer/123456789',
      required: true,
      condition: {
        field: 'operation',
        value: ['shopify_get_customer', 'shopify_update_customer', 'shopify_delete_customer'],
      },
    },
    // Customer Email (at least one of email/phone/firstName/lastName required for create)
    {
      id: 'customerEmail',
      title: 'Email',
      type: 'short-input',
      placeholder: 'customer@example.com',
      condition: {
        field: 'operation',
        value: ['shopify_create_customer', 'shopify_update_customer'],
      },
    },
    // Customer First Name
    {
      id: 'firstName',
      title: 'First Name',
      type: 'short-input',
      placeholder: 'Enter first name',
      condition: {
        field: 'operation',
        value: ['shopify_create_customer', 'shopify_update_customer'],
      },
    },
    // Customer Last Name
    {
      id: 'lastName',
      title: 'Last Name',
      type: 'short-input',
      placeholder: 'Enter last name',
      condition: {
        field: 'operation',
        value: ['shopify_create_customer', 'shopify_update_customer'],
      },
    },
    // Customer Phone
    {
      id: 'phone',
      title: 'Phone',
      type: 'short-input',
      placeholder: '+1234567890',
      condition: {
        field: 'operation',
        value: ['shopify_create_customer', 'shopify_update_customer'],
      },
    },
    // Customer Note
    {
      id: 'customerNote',
      title: 'Customer Note',
      type: 'long-input',
      placeholder: 'Enter note about customer',
      condition: {
        field: 'operation',
        value: ['shopify_create_customer', 'shopify_update_customer'],
      },
    },
    // Customer Tags
    {
      id: 'customerTags',
      title: 'Customer Tags',
      type: 'short-input',
      placeholder: 'vip, wholesale (comma-separated)',
      condition: {
        field: 'operation',
        value: ['shopify_create_customer', 'shopify_update_customer'],
      },
    },
    // Inventory Item ID
    {
      id: 'inventoryItemId',
      title: 'Inventory Item ID',
      type: 'short-input',
      placeholder: 'gid://shopify/InventoryItem/123456789',
      required: true,
      condition: {
        field: 'operation',
        value: ['shopify_get_inventory_level', 'shopify_adjust_inventory'],
      },
    },
    // Location ID
    {
      id: 'locationId',
      title: 'Location ID',
      type: 'short-input',
      placeholder: 'gid://shopify/Location/123456789',
      required: {
        field: 'operation',
        value: 'shopify_adjust_inventory',
      },
      condition: {
        field: 'operation',
        value: ['shopify_get_inventory_level', 'shopify_adjust_inventory'],
      },
    },
    // Delta (for inventory adjustment)
    {
      id: 'delta',
      title: 'Quantity Change',
      type: 'short-input',
      placeholder: 'Positive to add, negative to subtract',
      required: true,
      condition: {
        field: 'operation',
        value: ['shopify_adjust_inventory'],
      },
    },
    // Fulfillment Order ID
    {
      id: 'fulfillmentOrderId',
      title: 'Fulfillment Order ID',
      type: 'short-input',
      placeholder: 'gid://shopify/FulfillmentOrder/123456789',
      required: true,
      condition: {
        field: 'operation',
        value: ['shopify_create_fulfillment'],
      },
    },
    // Tracking Number
    {
      id: 'trackingNumber',
      title: 'Tracking Number',
      type: 'short-input',
      placeholder: 'Enter tracking number',
      condition: {
        field: 'operation',
        value: ['shopify_create_fulfillment'],
      },
    },
    // Tracking Company
    {
      id: 'trackingCompany',
      title: 'Shipping Carrier',
      type: 'short-input',
      placeholder: 'e.g., UPS, FedEx, USPS, DHL',
      condition: {
        field: 'operation',
        value: ['shopify_create_fulfillment'],
      },
    },
    // Tracking URL
    {
      id: 'trackingUrl',
      title: 'Tracking URL',
      type: 'short-input',
      placeholder: 'https://...',
      condition: {
        field: 'operation',
        value: ['shopify_create_fulfillment'],
      },
    },
    // Notify Customer (for fulfillment)
    {
      id: 'notifyCustomer',
      title: 'Notify Customer',
      type: 'dropdown',
      options: [
        { label: 'Yes', id: 'true' },
        { label: 'No', id: 'false' },
      ],
      value: () => 'true',
      mode: 'advanced',
      condition: {
        field: 'operation',
        value: ['shopify_create_fulfillment'],
      },
    },
    {
      id: 'includeInactive',
      title: 'Include Inactive Locations',
      type: 'dropdown',
      options: [
        { label: 'Yes', id: 'true' },
        { label: 'No', id: 'false' },
      ],
      value: () => 'false',
      mode: 'advanced',
      condition: {
        field: 'operation',
        value: ['shopify_list_locations'],
      },
    },
    // Collection ID
    {
      id: 'collectionId',
      title: 'Collection ID',
      type: 'short-input',
      placeholder: 'gid://shopify/Collection/123456789',
      required: true,
      condition: {
        field: 'operation',
        value: ['shopify_get_collection'],
      },
    },
    // Collection Query
    {
      id: 'collectionQuery',
      title: 'Search Query',
      type: 'short-input',
      placeholder: 'e.g., title:Summer OR collection_type:smart',
      mode: 'advanced',
      condition: {
        field: 'operation',
        value: ['shopify_list_collections'],
      },
    },
    {
      id: 'productsFirst',
      title: 'Max Products In Collection',
      type: 'short-input',
      placeholder: 'Defaults to 50, max 250',
      mode: 'advanced',
      condition: {
        field: 'operation',
        value: ['shopify_get_collection'],
      },
    },
  ],
  tools: {
    access: [
      'shopify_create_product',
      'shopify_get_product',
      'shopify_list_products',
      'shopify_update_product',
      'shopify_delete_product',
      'shopify_get_order',
      'shopify_list_orders',
      'shopify_update_order',
      'shopify_cancel_order',
      'shopify_create_customer',
      'shopify_get_customer',
      'shopify_list_customers',
      'shopify_update_customer',
      'shopify_delete_customer',
      'shopify_list_inventory_items',
      'shopify_get_inventory_level',
      'shopify_adjust_inventory',
      'shopify_list_locations',
      'shopify_create_fulfillment',
      'shopify_list_collections',
      'shopify_get_collection',
    ],
    config: {
      tool: (params) => {
        return params.operation || 'shopify_list_products'
      },
      params: (params) => {
        const first = parseOptionalNumberInput(params.first, 'first')
        const baseParams: Record<string, unknown> = {
          oauthCredential: params.oauthCredential,
          shopDomain: params.shopDomain?.trim(),
        }

        switch (params.operation) {
          // Product Operations
          case 'shopify_create_product':
            if (!params.title?.trim()) {
              throw new Error('Product title is required.')
            }
            return {
              ...baseParams,
              title: params.title.trim(),
              descriptionHtml: params.descriptionHtml?.trim(),
              productType: params.productType?.trim(),
              vendor: params.vendor?.trim(),
              tags: params.tags
                ?.split(',')
                .map((t: string) => t.trim())
                .filter(Boolean),
              status: params.status,
            }

          case 'shopify_get_product':
            if (!params.productId?.trim()) {
              throw new Error('Product ID is required.')
            }
            return {
              ...baseParams,
              productId: params.productId.trim(),
            }

          case 'shopify_list_products':
            return {
              ...baseParams,
              first,
              query: params.productQuery?.trim(),
            }

          case 'shopify_update_product':
            if (!params.productId?.trim()) {
              throw new Error('Product ID is required.')
            }
            return {
              ...baseParams,
              productId: params.productId.trim(),
              title: params.title?.trim(),
              descriptionHtml: params.descriptionHtml?.trim(),
              productType: params.productType?.trim(),
              vendor: params.vendor?.trim(),
              tags: params.tags
                ?.split(',')
                .map((t: string) => t.trim())
                .filter(Boolean),
              status: params.status,
            }

          case 'shopify_delete_product':
            if (!params.productId?.trim()) {
              throw new Error('Product ID is required.')
            }
            return {
              ...baseParams,
              productId: params.productId.trim(),
            }

          // Order Operations
          case 'shopify_get_order':
            if (!params.orderId?.trim()) {
              throw new Error('Order ID is required.')
            }
            return {
              ...baseParams,
              orderId: params.orderId.trim(),
            }

          case 'shopify_list_orders':
            return {
              ...baseParams,
              first,
              status: params.orderStatus !== 'any' ? params.orderStatus : undefined,
              query: params.orderQuery?.trim(),
            }

          case 'shopify_update_order':
            if (!params.orderId?.trim()) {
              throw new Error('Order ID is required.')
            }
            return {
              ...baseParams,
              orderId: params.orderId.trim(),
              note: params.orderNote?.trim(),
              email: params.orderEmail?.trim(),
              tags: params.orderTags
                ?.split(',')
                .map((t: string) => t.trim())
                .filter(Boolean),
            }

          case 'shopify_cancel_order':
            if (!params.orderId?.trim()) {
              throw new Error('Order ID is required.')
            }
            if (!params.cancelReason) {
              throw new Error('Cancel reason is required.')
            }
            return {
              ...baseParams,
              orderId: params.orderId.trim(),
              reason: params.cancelReason,
              restock: parseOptionalBooleanInput(params.restock) ?? false,
              notifyCustomer: parseOptionalBooleanInput(params.cancelNotifyCustomer),
              refundMethod:
                parseOptionalBooleanInput(params.refundOriginalPayment) === true
                  ? { originalPaymentMethodsRefund: true }
                  : undefined,
              staffNote: params.staffNote?.trim(),
            }

          // Customer Operations
          case 'shopify_create_customer':
            // At least one of email/phone/firstName/lastName required (validated in tool)
            return {
              ...baseParams,
              email: params.customerEmail?.trim(),
              firstName: params.firstName?.trim(),
              lastName: params.lastName?.trim(),
              phone: params.phone?.trim(),
              note: params.customerNote?.trim(),
              tags: params.customerTags
                ?.split(',')
                .map((t: string) => t.trim())
                .filter(Boolean),
            }

          case 'shopify_get_customer':
            if (!params.customerId?.trim()) {
              throw new Error('Customer ID is required.')
            }
            return {
              ...baseParams,
              customerId: params.customerId.trim(),
            }

          case 'shopify_list_customers':
            return {
              ...baseParams,
              first,
              query: params.customerQuery?.trim(),
            }

          case 'shopify_update_customer':
            if (!params.customerId?.trim()) {
              throw new Error('Customer ID is required.')
            }
            return {
              ...baseParams,
              customerId: params.customerId.trim(),
              email: params.customerEmail?.trim(),
              firstName: params.firstName?.trim(),
              lastName: params.lastName?.trim(),
              phone: params.phone?.trim(),
              note: params.customerNote?.trim(),
              tags: params.customerTags
                ?.split(',')
                .map((t: string) => t.trim())
                .filter(Boolean),
            }

          case 'shopify_delete_customer':
            if (!params.customerId?.trim()) {
              throw new Error('Customer ID is required.')
            }
            return {
              ...baseParams,
              customerId: params.customerId.trim(),
            }

          // Inventory Operations
          case 'shopify_list_inventory_items':
            return {
              ...baseParams,
              first,
              query: params.inventoryQuery?.trim(),
            }

          case 'shopify_get_inventory_level':
            if (!params.inventoryItemId?.trim()) {
              throw new Error('Inventory Item ID is required.')
            }
            return {
              ...baseParams,
              inventoryItemId: params.inventoryItemId.trim(),
              locationId: params.locationId?.trim(),
            }

          case 'shopify_adjust_inventory':
            if (!params.inventoryItemId?.trim()) {
              throw new Error('Inventory Item ID is required.')
            }
            if (!params.locationId?.trim()) {
              throw new Error('Location ID is required.')
            }
            if (params.delta === undefined || params.delta === '') {
              throw new Error('Quantity change (delta) is required.')
            }
            return {
              ...baseParams,
              inventoryItemId: params.inventoryItemId.trim(),
              locationId: params.locationId.trim(),
              delta: Number(params.delta),
            }

          // Location Operations
          case 'shopify_list_locations':
            return {
              ...baseParams,
              first,
              includeInactive: parseOptionalBooleanInput(params.includeInactive),
            }

          // Fulfillment Operations
          case 'shopify_create_fulfillment':
            if (!params.fulfillmentOrderId?.trim()) {
              throw new Error('Fulfillment Order ID is required.')
            }
            return {
              ...baseParams,
              fulfillmentOrderId: params.fulfillmentOrderId.trim(),
              trackingNumber: params.trackingNumber?.trim(),
              trackingCompany: params.trackingCompany?.trim(),
              trackingUrl: params.trackingUrl?.trim(),
              notifyCustomer: parseOptionalBooleanInput(params.notifyCustomer),
            }

          // Collection Operations
          case 'shopify_list_collections':
            return {
              ...baseParams,
              first,
              query: params.collectionQuery?.trim(),
            }

          case 'shopify_get_collection':
            if (!params.collectionId?.trim()) {
              throw new Error('Collection ID is required.')
            }
            return {
              ...baseParams,
              collectionId: params.collectionId.trim(),
              productsFirst: parseOptionalNumberInput(params.productsFirst, 'productsFirst'),
            }

          default:
            return baseParams
        }
      },
    },
  },
  inputs: {
    operation: { type: 'string', description: 'Operation to perform' },
    oauthCredential: { type: 'string', description: 'Shopify access token' },
    shopDomain: { type: 'string', description: 'Shopify store domain' },
    // Product inputs
    productId: { type: 'string', description: 'Product ID' },
    title: { type: 'string', description: 'Product title' },
    descriptionHtml: { type: 'string', description: 'Product description (HTML)' },
    productType: { type: 'string', description: 'Product type' },
    vendor: { type: 'string', description: 'Product vendor' },
    tags: { type: 'string', description: 'Tags (comma-separated)' },
    status: { type: 'string', description: 'Product status' },
    productQuery: { type: 'string', description: 'Product search query' },
    first: { type: 'number', description: 'Maximum number of results to return' },
    // Order inputs
    orderId: { type: 'string', description: 'Order ID' },
    orderStatus: { type: 'string', description: 'Order status filter' },
    orderQuery: { type: 'string', description: 'Order search query' },
    orderNote: { type: 'string', description: 'Order note' },
    orderEmail: { type: 'string', description: 'Order customer email' },
    orderTags: { type: 'string', description: 'Order tags' },
    cancelReason: { type: 'string', description: 'Order cancellation reason' },
    restock: { type: 'boolean', description: 'Whether to restock cancelled items' },
    cancelNotifyCustomer: { type: 'boolean', description: 'Whether to notify the customer' },
    refundOriginalPayment: {
      type: 'boolean',
      description: 'Whether to refund to the original payment method',
    },
    staffNote: { type: 'string', description: 'Staff note for order cancellation' },
    // Customer inputs
    customerId: { type: 'string', description: 'Customer ID' },
    customerEmail: { type: 'string', description: 'Customer email' },
    firstName: { type: 'string', description: 'Customer first name' },
    lastName: { type: 'string', description: 'Customer last name' },
    phone: { type: 'string', description: 'Customer phone' },
    customerNote: { type: 'string', description: 'Customer note' },
    customerTags: { type: 'string', description: 'Customer tags' },
    customerQuery: { type: 'string', description: 'Customer search query' },
    // Inventory inputs
    inventoryQuery: { type: 'string', description: 'Inventory search query' },
    inventoryItemId: { type: 'string', description: 'Inventory item ID' },
    locationId: { type: 'string', description: 'Location ID' },
    delta: { type: 'number', description: 'Quantity change' },
    // Fulfillment inputs
    fulfillmentOrderId: { type: 'string', description: 'Fulfillment order ID' },
    trackingNumber: { type: 'string', description: 'Shipment tracking number' },
    trackingCompany: { type: 'string', description: 'Shipping carrier name' },
    trackingUrl: { type: 'string', description: 'Tracking URL' },
    notifyCustomer: { type: 'boolean', description: 'Send shipping notification email' },
    includeInactive: { type: 'boolean', description: 'Include inactive locations in results' },
    // Collection inputs
    collectionId: { type: 'string', description: 'Collection ID' },
    collectionQuery: { type: 'string', description: 'Collection search query' },
    productsFirst: { type: 'number', description: 'Maximum number of products to return' },
  },
  outputs: {
    // Product outputs
    product: {
      type: 'json',
      description:
        'Product details (id, title, handle, descriptionHtml, vendor, productType, tags, status, variants, images)',
    },
    products: {
      type: 'json',
      description: 'List of products with core product fields and media summaries',
    },
    // Order outputs
    order: {
      type: 'json',
      description:
        'Order details or cancellation result depending on the operation (order fields, customer, totals, notes, line items, or cancellation job status)',
    },
    orders: {
      type: 'json',
      description: 'List of orders with status, totals, customer, and shipping summary fields',
    },
    // Customer outputs
    customer: {
      type: 'json',
      description:
        'Customer details (id, email, name, phone, note, tags, amountSpent, addresses, defaultAddress)',
    },
    customers: {
      type: 'json',
      description: 'List of customers with contact details, tags, spend, and default address',
    },
    // Inventory outputs
    inventoryItems: {
      type: 'json',
      description:
        'Inventory items with SKU, tracking status, variant details, and per-location stock',
    },
    inventoryLevel: {
      type: 'json',
      description:
        'Inventory levels for an item or an inventory adjustment result (levels by location, or adjustmentGroup and changes)',
    },
    // Location outputs
    locations: {
      type: 'json',
      description:
        'Store locations with id, name, active status, fulfillment capability, and address',
    },
    // Fulfillment outputs
    fulfillment: {
      type: 'json',
      description:
        'Fulfillment result (id, status, trackingInfo, createdAt, updatedAt, fulfillmentLineItems)',
    },
    // Collection outputs
    collection: {
      type: 'json',
      description:
        'Collection details (id, title, handle, descriptionHtml, image, sortOrder, productsCount, products)',
    },
    collections: {
      type: 'json',
      description:
        'List of collections with id, title, handle, product counts, sort order, and image',
    },
    pageInfo: {
      type: 'json',
      description: 'Pagination info for list operations (hasNextPage, hasPreviousPage)',
    },
    // Delete outputs
    deletedId: { type: 'string', description: 'ID of deleted resource' },
    // Success indicator
    success: { type: 'boolean', description: 'Operation success status' },
  },
}

export const ShopifyBlockMeta = {
  tags: ['payments', 'automation'],
  url: 'https://www.shopify.com',
  templates: [
    {
      icon: ShopifyIcon,
      title: 'E-commerce order monitor',
      prompt:
        'Build a workflow that monitors Shopify orders, flags high-value or unusual orders for review, tracks fulfillment status in a table, and sends daily inventory and sales summaries to Slack with restock alerts when items run low.',
      modules: ['tables', 'scheduled', 'agent', 'workflows'],
      category: 'operations',
      tags: ['ecommerce', 'monitoring', 'reporting'],
      alsoIntegrations: ['slack'],
    },
    {
      icon: ShopifyIcon,
      title: 'Unpaid order recovery',
      prompt:
        'Build a scheduled workflow that lists Shopify orders left open and unpaid in the past day, drafts a personalized recovery email referencing the items, and sends it via Gmail while logging recovery attempts to a table.',
      modules: ['scheduled', 'tables', 'agent', 'workflows'],
      category: 'operations',
      tags: ['ecommerce', 'marketing', 'automation'],
      alsoIntegrations: ['gmail'],
    },
    {
      icon: ShopifyIcon,
      title: 'Low-stock restock alerter',
      prompt:
        'Create a scheduled hourly workflow that lists Shopify inventory items, computes days-of-cover from recent sales velocity, flags SKUs below a configurable threshold, and posts a Slack alert to the operations channel with the variant, location, and recommended reorder quantity.',
      modules: ['scheduled', 'tables', 'agent', 'workflows'],
      category: 'operations',
      tags: ['ecommerce', 'monitoring', 'operations'],
      alsoIntegrations: ['slack'],
    },
    {
      icon: ShopifyIcon,
      title: 'Shopify VIP segmenter',
      prompt:
        'Build a scheduled weekly workflow that pulls Shopify customers, calculates lifetime value and order frequency, segments them into VIP, regular, and at-risk cohorts in a tracking table, and emails the marketing team a list of new VIPs to nurture.',
      modules: ['scheduled', 'tables', 'agent', 'workflows'],
      category: 'operations',
      tags: ['ecommerce', 'marketing', 'analysis'],
    },
    {
      icon: ShopifyIcon,
      title: 'Fulfillment status tracker',
      prompt:
        'Create a scheduled workflow that lists Shopify orders and their fulfillment status, updates a status table with shipped, in-transit, and delivered states, and proactively emails customers when their order misses an SLA so support gets ahead of the inquiry.',
      modules: ['scheduled', 'tables', 'agent', 'workflows'],
      category: 'operations',
      tags: ['ecommerce', 'support', 'monitoring'],
      alsoIntegrations: ['gmail'],
    },
    {
      icon: ShopifyIcon,
      title: 'Product launch publisher',
      prompt:
        'Build a workflow that takes a new product brief, creates the product in Shopify with variants and pricing, adds it to the right collection, drafts a launch announcement, and queues a Slack and email broadcast for marketing review before going live.',
      modules: ['agent', 'workflows'],
      category: 'operations',
      tags: ['ecommerce', 'marketing', 'automation'],
      alsoIntegrations: ['gmail', 'slack'],
    },
    {
      icon: ShopifyIcon,
      title: 'Order anomaly detector',
      prompt:
        'Create a scheduled workflow that runs every fifteen minutes, lists recent Shopify orders, scores each for anomalies — high value, unusual destination, mismatched billing — flags suspects in a review queue table, and Slacks the operations team for hands-on inspection.',
      modules: ['scheduled', 'tables', 'agent', 'workflows'],
      category: 'operations',
      tags: ['ecommerce', 'monitoring', 'analysis'],
      alsoIntegrations: ['slack'],
    },
  ],
  skills: [
    {
      name: 'create-product-listing',
      description: 'Create a new Shopify product with title, description, status, and variants.',
      content:
        '# Create Product Listing\n\nAdd a new product to the Shopify store.\n\n## Steps\n1. Run Create Product with the title, body description, vendor, and product type.\n2. Set the status (active, draft, or archived) so the product publishes only when ready.\n3. Verify with Get Product on the returned product ID.\n\n## Output\nReturn the new product ID, title, and status, and confirm the listing was created as draft or active as intended.',
    },
    {
      name: 'process-recent-orders',
      description:
        'List recent Shopify orders and summarize them by status, value, or fulfillment need.',
      content:
        '# Process Recent Orders\n\nReview the latest orders to triage fulfillment and flag anything unusual.\n\n## Steps\n1. Run List Orders filtered by status (open, closed, cancelled, or any) and a recent time window.\n2. For orders needing detail, run Get Order to read line items, customer, and shipping address.\n3. Group orders by fulfillment status and total value.\n\n## Output\nReturn a summary of recent orders with their order numbers, totals, and status, highlighting any that need immediate fulfillment or review.',
    },
    {
      name: 'fulfill-order',
      description: 'Create a fulfillment for a Shopify order and update its status.',
      content:
        '# Fulfill Order\n\nMark an order as fulfilled once it has shipped.\n\n## Steps\n1. Run Get Order to confirm the order and its line items, and List Locations to identify the fulfilling location.\n2. Run Create Fulfillment for the order, supplying the location and tracking details if available.\n3. Optionally run Update Order to record any notes.\n\n## Output\nConfirm the order number, the fulfillment created, and any tracking number supplied.',
    },
    {
      name: 'adjust-inventory',
      description: 'Check and adjust Shopify inventory levels for an item at a location.',
      content:
        '# Adjust Inventory\n\nReconcile stock levels for an inventory item.\n\n## Steps\n1. Run List Inventory Items and List Locations to identify the item and the location.\n2. Run Get Inventory Level to read the current available quantity.\n3. Run Adjust Inventory with the delta needed to reach the correct count.\n\n## Output\nReport the inventory item, the location, the previous and new quantities, and the adjustment applied.',
    },
    {
      name: 'manage-customer-record',
      description: 'Create, look up, or update a Shopify customer record.',
      content:
        '# Manage Customer Record\n\nMaintain a customer profile in Shopify.\n\n## Steps\n1. To find an existing customer, run List Customers with a filter or Get Customer by ID.\n2. To add a new one, run Create Customer with name, email, and any tags.\n3. To change details, run Update Customer with only the fields to modify.\n\n## Output\nReturn the customer ID, name, and email, and note whether the record was created, found, or updated.',
    },
  ],
} as const satisfies BlockMeta
