[
  {
    "type": "create_table",
    "description": "Create ccEvents table for Comeback Cash event configuration",
    "database": "{{store}}",
    "check_query": "SHOW TABLES LIKE 'ccEvents'",
    "sql": "CREATE TABLE `ccEvents` (\n  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,\n  `name` varchar(100) NOT NULL COMMENT 'Display name for event',\n  `side` enum('buy','sales') NOT NULL COMMENT 'buy = customer selling TO store, sales = customer buying FROM store',\n  `status` enum('draft','scheduled','active','ended','cancelled') NOT NULL DEFAULT 'draft',\n  `start_date` datetime DEFAULT NULL COMMENT 'When event becomes active (NULL = manual start)',\n  `end_date` datetime DEFAULT NULL COMMENT 'When event ends (NULL = manual end)',\n  `earning_type` enum('tiered','flat','percentage') NOT NULL DEFAULT 'flat',\n  `earning_tiers` json DEFAULT NULL COMMENT 'Sales-side only: [{\"min\": 50, \"max\": 99.99, \"reward\": 10}]',\n  `earning_flat_amount` decimal(10,2) DEFAULT NULL COMMENT 'Flat reward amount - REQUIRED for buy-side, optional for sales-side',\n  `earning_percentage` decimal(5,2) DEFAULT NULL COMMENT 'Sales-side only: Percentage of transaction',\n  `redemption_min_purchase` decimal(10,2) DEFAULT NULL COMMENT 'Minimum purchase to redeem (pre-tax)',\n  `redemption_start_date` datetime DEFAULT NULL COMMENT 'When redemption period starts',\n  `redemption_end_date` datetime DEFAULT NULL COMMENT 'When redemption period ends',\n  `redemption_days_valid` int(10) unsigned DEFAULT NULL COMMENT 'Days coupon is valid from issue date',\n  `allow_double_up` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'Sales-side only: Allow earning new coupon while redeeming',\n  `refund_policy` enum('forfeit','reinstate') NOT NULL DEFAULT 'forfeit',\n  `sms_enabled` tinyint(1) NOT NULL DEFAULT 1 COMMENT 'Send SMS notifications',\n  `created_by` int(10) unsigned NOT NULL,\n  `created_at` datetime NOT NULL DEFAULT current_timestamp(),\n  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),\n  PRIMARY KEY (`id`),\n  KEY `idx_side_status` (`side`, `status`),\n  KEY `idx_dates` (`start_date`, `end_date`),\n  KEY `idx_status` (`status`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Comeback Cash event configuration'"
  },
  {
    "type": "create_table",
    "description": "Create ccCoupons table for issued Comeback Cash coupons",
    "database": "{{store}}",
    "check_query": "SHOW TABLES LIKE 'ccCoupons'",
    "sql": "CREATE TABLE `ccCoupons` (\n  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,\n  `event_id` int(10) unsigned NOT NULL,\n  `code` varchar(12) NOT NULL COMMENT '8-char unique code (e.g., A1B2C3D4)',\n  `value` decimal(10,2) NOT NULL COMMENT 'Coupon face value',\n  `original_value` decimal(10,2) NOT NULL COMMENT 'Original value (for partial redemption tracking)',\n  `source_transaction_id` varchar(50) NOT NULL COMMENT 'POS transaction ID that generated this coupon',\n  `source_transaction_amount` decimal(10,2) NOT NULL COMMENT 'Transaction amount that qualified',\n  `customer_phone` varchar(20) DEFAULT NULL COMMENT 'Phone for SMS notifications',\n  `customer_name` varchar(100) DEFAULT NULL COMMENT 'Customer name (if provided)',\n  `status` enum('active','redeemed','expired','voided') NOT NULL DEFAULT 'active',\n  `expires_at` datetime NOT NULL,\n  `issued_at` datetime NOT NULL DEFAULT current_timestamp(),\n  `issued_by_employee_id` int(10) unsigned DEFAULT NULL COMMENT 'NULL if issued by POS automatically',\n  PRIMARY KEY (`id`),\n  UNIQUE KEY `uk_code` (`code`),\n  KEY `idx_event` (`event_id`),\n  KEY `idx_status_expires` (`status`, `expires_at`),\n  KEY `idx_phone` (`customer_phone`),\n  KEY `idx_transaction` (`source_transaction_id`),\n  CONSTRAINT `fk_ccCoupon_event` FOREIGN KEY (`event_id`) REFERENCES `ccEvents` (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Issued Comeback Cash coupons'"
  },
  {
    "type": "create_table",
    "description": "Create ccRedemptions table for redemption audit log",
    "database": "{{store}}",
    "check_query": "SHOW TABLES LIKE 'ccRedemptions'",
    "sql": "CREATE TABLE `ccRedemptions` (\n  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,\n  `coupon_id` int(10) unsigned NOT NULL,\n  `redeemed_amount` decimal(10,2) NOT NULL COMMENT 'Amount applied to transaction',\n  `transaction_id` varchar(50) NOT NULL COMMENT 'POS transaction ID where redeemed',\n  `transaction_amount` decimal(10,2) NOT NULL COMMENT 'Transaction subtotal (pre-tax)',\n  `redeemed_by_employee_id` int(10) unsigned NOT NULL,\n  `redeemed_at` datetime NOT NULL DEFAULT current_timestamp(),\n  `redemption_method` enum('scan','manual','pos') NOT NULL DEFAULT 'scan',\n  PRIMARY KEY (`id`),\n  KEY `idx_coupon` (`coupon_id`),\n  KEY `idx_transaction` (`transaction_id`),\n  KEY `idx_employee_date` (`redeemed_by_employee_id`, `redeemed_at`),\n  CONSTRAINT `fk_ccRedemption_coupon` FOREIGN KEY (`coupon_id`) REFERENCES `ccCoupons` (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Comeback Cash redemption audit log'"
  }
]
