-- Migration: Add product_discounts table and form
-- Date: 2025-10-04
-- Description: Adds product discount functionality with date-based discount ranges

-- The product_discounts table is already created in TAFDB.pgsql
-- This migration ensures the table is registered in tenant_tables for IndexedDB sync

-- Register product_discounts table in tenant_tables if not already present
INSERT INTO tenant_tables (table_name, definition, isDeleted, updatedAt)
SELECT 
  'product_discounts',
  '{
    "columns": [
      {"name": "discount_id", "type": "uuid", "primary": true},
      {"name": "product_id", "type": "uuid", "references": "products.product_id"},
      {"name": "discount_amount", "type": "numeric"},
      {"name": "start_date", "type": "date"},
      {"name": "end_date", "type": "date"},
      {"name": "org_id", "type": "uuid", "references": "orgs.org_id"},
      {"name": "isDeleted", "type": "boolean"},
      {"name": "updatedAt", "type": "timestamptz"}
    ]
  }'::jsonb,
  FALSE,
  now()
WHERE NOT EXISTS (
  SELECT 1 FROM tenant_tables WHERE table_name = 'product_discounts'
);

-- The Product Discount form (ID 134) is already added in sql/defaults.sql
-- The Products module is already updated with the Discount button in sql/modules.sql
