-- ============================================================================
-- Phase 8: Inventory Analytics Modules Registration
-- Purpose: Register declarative modules for inventory analytics dashboards
-- Pattern: Follows procurement analytics module pattern with data-controller
-- ============================================================================

-- Module IDs for Phase 8 Inventory Analytics
INSERT INTO modules (module_id, name, description) VALUES
    ('01948801-0001-7123-8123-000000000001'::uuid, 'VAT Analytics Dashboard', 'VAT summary and reconciliation analytics'),
    ('01948801-0002-7123-8123-000000000002'::uuid, 'Product Movement Analytics', 'Product sales and inventory movement analytics'),
    ('01948801-0003-7123-8123-000000000003'::uuid, 'Payment Methods Analytics', 'Payment method breakdown and credit analysis'),
    ('01948801-0004-7123-8123-000000000004'::uuid, 'Customer Analytics Dashboard', 'Customer purchase patterns and revenue analytics'),
    ('01948801-0005-7123-8123-000000000005'::uuid, 'Staff Performance Analytics', 'Cashier sales and efficiency metrics'),
    ('01948801-0006-7123-8123-000000000006'::uuid, 'Branch Performance Dashboard', 'Branch-level sales comparison and trends'),
    ('01948801-0007-7123-8123-000000000007'::uuid, 'Sales Time-Series Analytics', 'Daily, hourly, and trend analytics'),
    ('01948801-0008-7123-8123-000000000008'::uuid, 'Profit & Margin Analytics', 'Product profitability and discount impact'),
    ('01948801-0009-7123-8123-000000000009'::uuid, 'Customer Segmentation (RFM)', 'RFM analysis and customer lifecycle'),
    ('01948801-0010-7123-8123-000000000010'::uuid, 'Transaction Export Dashboard', 'Full transaction data export and reporting')
ON CONFLICT (module_id) DO NOTHING;

-- ============================================================================
-- Module 1: VAT Analytics Dashboard
-- Queries: Basic VAT Summary, Daily Cash Flow
-- Uses: AggregateRowsController GROUP BY
-- ============================================================================
INSERT INTO modules (module_id, name, config, org_id, branch_id, icon, aria_label, description) VALUES
  (
    '01948801-0001-7123-8123-000000000001'::uuid,
    'vat_analytics_dashboard',
    '{"title":"VAT Analytics Dashboard","body":[{"type":"html","html":"<div data-controller=\"VatAnalyticsDashboard\" class=\"space-y-6\">\n  <div class=\"flex flex-wrap items-center justify-between gap-3\">\n    <div class=\"flex flex-wrap gap-3\">\n      <label class=\"form-control w-48\">\n        <span class=\"label-text text-sm\">Date Range</span>\n        <input type=\"date\" class=\"input input-bordered input-sm\" data-filter=\"start_date\" />\n      </label>\n      <label class=\"form-control w-48\">\n        <span class=\"label-text text-sm\">To</span>\n        <input type=\"date\" class=\"input input-bordered input-sm\" data-filter=\"end_date\" />\n      </label>\n      <label class=\"form-control w-40\">\n        <span class=\"label-text text-sm\">Organization</span>\n        <select class=\"select select-bordered select-sm\" data-filter=\"org_id\">\n          <option value=\"\">All Orgs</option>\n        </select>\n      </label>\n    </div>\n    <button class=\"btn btn-sm btn-primary\" onclick=\"refreshVatAnalytics()\">Refresh</button>\n  </div>\n  <div class=\"hidden text-sm text-error\" data-role=\"error-container\"></div>\n  <div class=\"grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-4\" data-role=\"metric-cards\"></div>\n  <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n    <div class=\"card-body\">\n      <h3 class=\"card-title\">VAT Summary by Code</h3>\n      <div class=\"overflow-x-auto\">\n        <table class=\"table table-sm\" data-role=\"vat-summary-table\">\n          <thead>\n            <tr><th>VAT Code</th><th>VAT Rate</th><th>Taxable Amount</th><th>VAT Amount</th><th>Total</th><th>Transactions</th></tr>\n          </thead>\n          <tbody></tbody>\n        </table>\n      </div>\n    </div>\n  </div>\n  <div class=\"grid grid-cols-1 lg:grid-cols-2 gap-6\">\n    <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n      <div class=\"card-body\">\n        <h3 class=\"card-title\">VAT Distribution</h3>\n        <div class=\"h-64\">\n          <canvas data-role=\"vat-chart\"></canvas>\n        </div>\n      </div>\n    </div>\n    <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n      <div class=\"card-body\">\n        <h3 class=\"card-title\">Daily VAT Trend</h3>\n        <div class=\"h-64\">\n          <canvas data-role=\"daily-vat-chart\"></canvas>\n        </div>\n      </div>\n    </div>\n  </div>\n</div>"}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}'::jsonb,
    NULL,
    NULL,
    'fa-percent',
    'VAT Analytics Dashboard',
    'VAT Analytics Dashboard'
  )
ON CONFLICT (module_id) DO UPDATE SET
    config = EXCLUDED.config,
    icon = EXCLUDED.icon,
    aria_label = EXCLUDED.aria_label,
    description = EXCLUDED.description;

-- ============================================================================
-- Module 2: Product Movement Analytics
-- Queries: Top 10 Best Sellers, Product Sales Trend
-- Uses: AggregateRowsController with filters
-- ============================================================================
INSERT INTO modules (module_id, name, config, org_id, branch_id, icon, aria_label, description) VALUES
  (
    '01948801-0002-7123-8123-000000000002'::uuid,
    'product_movement_analytics',
    '{"title":"Product Movement Analytics","body":[{"type":"html","html":"<div data-controller=\"ProductMovementAnalytics\" class=\"space-y-6\">\n  <div class=\"flex flex-wrap items-center justify-between gap-3\">\n    <div class=\"flex flex-wrap gap-3\">\n      <label class=\"form-control w-48\">\n        <span class=\"label-text text-sm\">Date Range</span>\n        <input type=\"date\" class=\"input input-bordered input-sm\" data-filter=\"start_date\" />\n      </label>\n      <label class=\"form-control w-48\">\n        <span class=\"label-text text-sm\">To</span>\n        <input type=\"date\" class=\"input input-bordered input-sm\" data-filter=\"end_date\" />\n      </label>\n      <label class=\"form-control w-40\">\n        <span class=\"label-text text-sm\">Category</span>\n        <select class=\"select select-bordered select-sm\" data-filter=\"category_id\">\n          <option value=\"\">All Categories</option>\n        </select>\n      </label>\n    </div>\n    <button class=\"btn btn-sm btn-primary\" onclick=\"refreshProductMovement()\">Refresh</button>\n  </div>\n  <div class=\"hidden text-sm text-error\" data-role=\"error-container\"></div>\n  <div class=\"grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-4\" data-role=\"metric-cards\"></div>\n  <div class=\"grid grid-cols-1 lg:grid-cols-2 gap-6\">\n    <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n      <div class=\"card-body\">\n        <h3 class=\"card-title\">Top 10 Best Sellers</h3>\n        <div class=\"h-80\">\n          <canvas data-role=\"top-products-chart\"></canvas>\n        </div>\n      </div>\n    </div>\n    <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n      <div class=\"card-body\">\n        <h3 class=\"card-title\">Sales by Category</h3>\n        <div class=\"h-80\">\n          <canvas data-role=\"category-chart\"></canvas>\n        </div>\n      </div>\n    </div>\n  </div>\n  <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n    <div class=\"card-body\">\n      <h3 class=\"card-title\">Product Performance Table</h3>\n      <div class=\"overflow-x-auto\">\n        <table class=\"table table-sm\" data-role=\"product-table\">\n          <thead>\n            <tr><th>Product</th><th>Quantity Sold</th><th>Revenue</th><th>Avg Price</th><th>Transactions</th></tr>\n          </thead>\n          <tbody></tbody>\n        </table>\n      </div>\n    </div>\n  </div>\n</div>"}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}'::jsonb,
    NULL,
    NULL,
    'fa-boxes-stacked',
    'Product Movement Analytics',
    'Product Movement Analytics'
  )
ON CONFLICT (module_id) DO UPDATE SET
    config = EXCLUDED.config,
    icon = EXCLUDED.icon,
    aria_label = EXCLUDED.aria_label,
    description = EXCLUDED.description;

-- ============================================================================
-- Module 3: Payment Methods Analytics
-- Queries: Payment Method Summary, Daily Cash Flow
-- Uses: AggregateRowsController GROUP BY payment_method
-- ============================================================================
INSERT INTO modules (module_id, name, config, org_id, branch_id, icon, aria_label, description) VALUES
  (
    '01948801-0003-7123-8123-000000000003'::uuid,
    'payment_methods_analytics',
    '{"title":"Payment Methods Analytics","body":[{"type":"html","html":"<div data-controller=\"PaymentMethodsAnalytics\" class=\"space-y-6\">\n  <div class=\"flex flex-wrap items-center justify-between gap-3\">\n    <div class=\"flex flex-wrap gap-3\">\n      <label class=\"form-control w-48\">\n        <span class=\"label-text text-sm\">Date Range</span>\n        <input type=\"date\" class=\"input input-bordered input-sm\" data-filter=\"start_date\" />\n      </label>\n      <label class=\"form-control w-48\">\n        <span class=\"label-text text-sm\">To</span>\n        <input type=\"date\" class=\"input input-bordered input-sm\" data-filter=\"end_date\" />\n      </label>\n      <label class=\"form-control w-40\">\n        <span class=\"label-text text-sm\">Branch</span>\n        <select class=\"select select-bordered select-sm\" data-filter=\"branch_id\">\n          <option value=\"\">All Branches</option>\n        </select>\n      </label>\n    </div>\n    <button class=\"btn btn-sm btn-primary\" onclick=\"refreshPaymentMethods()\">Refresh</button>\n  </div>\n  <div class=\"hidden text-sm text-error\" data-role=\"error-container\"></div>\n  <div class=\"grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-4\" data-role=\"metric-cards\"></div>\n  <div class=\"grid grid-cols-1 lg:grid-cols-2 gap-6\">\n    <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n      <div class=\"card-body\">\n        <h3 class=\"card-title\">Payment Method Breakdown</h3>\n        <div class=\"h-64\">\n          <canvas data-role=\"payment-breakdown-chart\"></canvas>\n        </div>\n      </div>\n    </div>\n    <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n      <div class=\"card-body\">\n        <h3 class=\"card-title\">Daily Cash Flow</h3>\n        <div class=\"h-64\">\n          <canvas data-role=\"cash-flow-chart\"></canvas>\n        </div>\n      </div>\n    </div>\n  </div>\n  <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n    <div class=\"card-body\">\n      <h3 class=\"card-title\">Payment Methods Summary</h3>\n      <div class=\"overflow-x-auto\">\n        <table class=\"table table-sm\" data-role=\"payment-summary-table\">\n          <thead>\n            <tr><th>Payment Method</th><th>Total Amount</th><th>Transactions</th><th>Avg Transaction</th><th>% of Total</th></tr>\n          </thead>\n          <tbody></tbody>\n        </table>\n      </div>\n    </div>\n  </div>\n  <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n    <div class=\"card-body\">\n      <h3 class=\"card-title\">Credit Outstanding</h3>\n      <div class=\"overflow-x-auto\">\n        <table class=\"table table-sm\" data-role=\"credit-table\">\n          <thead>\n            <tr><th>Customer</th><th>Amount Due</th><th>Due Date</th><th>Days Outstanding</th><th>Status</th></tr>\n          </thead>\n          <tbody></tbody>\n        </table>\n      </div>\n    </div>\n  </div>\n</div>"}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}'::jsonb,
    NULL,
    NULL,
    'fa-credit-card',
    'Payment Methods Analytics',
    'Payment Methods Analytics'
  )
ON CONFLICT (module_id) DO UPDATE SET
    config = EXCLUDED.config,
    icon = EXCLUDED.icon,
    aria_label = EXCLUDED.aria_label,
    description = EXCLUDED.description;

-- ============================================================================
-- Module 4: Customer Analytics Dashboard
-- Queries: Top Customers by Revenue, Purchase Frequency
-- Uses: AggregateRowsController GROUP BY customer_id
-- ============================================================================
INSERT INTO modules (module_id, name, config, org_id, branch_id, icon, aria_label, description) VALUES
  (
    '01948801-0004-7123-8123-000000000004'::uuid,
    'customer_analytics_dashboard',
    '{"title":"Customer Analytics Dashboard","body":[{"type":"html","html":"<div data-controller=\"CustomerAnalyticsDashboard\" class=\"space-y-6\">\n  <div class=\"flex flex-wrap items-center justify-between gap-3\">\n    <div class=\"flex flex-wrap gap-3\">\n      <label class=\"form-control w-48\">\n        <span class=\"label-text text-sm\">Date Range</span>\n        <input type=\"date\" class=\"input input-bordered input-sm\" data-filter=\"start_date\" />\n      </label>\n      <label class=\"form-control w-48\">\n        <span class=\"label-text text-sm\">To</span>\n        <input type=\"date\" class=\"input input-bordered input-sm\" data-filter=\"end_date\" />\n      </label>\n    </div>\n    <button class=\"btn btn-sm btn-primary\" onclick=\"refreshCustomerAnalytics()\">Refresh</button>\n  </div>\n  <div class=\"hidden text-sm text-error\" data-role=\"error-container\"></div>\n  <div class=\"grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-4\" data-role=\"metric-cards\"></div>\n  <div class=\"grid grid-cols-1 lg:grid-cols-2 gap-6\">\n    <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n      <div class=\"card-body\">\n        <h3 class=\"card-title\">Top 10 Customers by Revenue</h3>\n        <div class=\"h-80\">\n          <canvas data-role=\"top-customers-chart\"></canvas>\n        </div>\n      </div>\n    </div>\n    <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n      <div class=\"card-body\">\n        <h3 class=\"card-title\">Purchase Frequency Distribution</h3>\n        <div class=\"h-80\">\n          <canvas data-role=\"frequency-chart\"></canvas>\n        </div>\n      </div>\n    </div>\n  </div>\n  <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n    <div class=\"card-body\">\n      <h3 class=\"card-title\">Customer Performance Table</h3>\n      <div class=\"overflow-x-auto\">\n        <table class=\"table table-sm\" data-role=\"customer-table\">\n          <thead>\n            <tr><th>Customer</th><th>Total Revenue</th><th>Transactions</th><th>Avg Transaction</th><th>Last Purchase</th></tr>\n          </thead>\n          <tbody></tbody>\n        </table>\n      </div>\n    </div>\n  </div>\n</div>"}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}'::jsonb,
    NULL,
    NULL,
    'fa-users',
    'Customer Analytics Dashboard',
    'Customer Analytics Dashboard'
  )
ON CONFLICT (module_id) DO UPDATE SET
    config = EXCLUDED.config,
    icon = EXCLUDED.icon,
    aria_label = EXCLUDED.aria_label,
    description = EXCLUDED.description;

-- ============================================================================
-- Module 5: Staff Performance Analytics
-- Queries: Cashier Sales Performance, Cashier Efficiency
-- Uses: AggregateRowsController GROUP BY cashier_id
-- ============================================================================
INSERT INTO modules (module_id, name, config, org_id, branch_id, icon, aria_label, description) VALUES
  (
    '01948801-0005-7123-8123-000000000005'::uuid,
    'staff_performance_analytics',
    '{"title":"Staff Performance Analytics","body":[{"type":"html","html":"<div data-controller=\"StaffPerformanceAnalytics\" class=\"space-y-6\">\n  <div class=\"flex flex-wrap items-center justify-between gap-3\">\n    <div class=\"flex flex-wrap gap-3\">\n      <label class=\"form-control w-48\">\n        <span class=\"label-text text-sm\">Date Range</span>\n        <input type=\"date\" class=\"input input-bordered input-sm\" data-filter=\"start_date\" />\n      </label>\n      <label class=\"form-control w-48\">\n        <span class=\"label-text text-sm\">To</span>\n        <input type=\"date\" class=\"input input-bordered input-sm\" data-filter=\"end_date\" />\n      </label>\n      <label class=\"form-control w-40\">\n        <span class=\"label-text text-sm\">Branch</span>\n        <select class=\"select select-bordered select-sm\" data-filter=\"branch_id\">\n          <option value=\"\">All Branches</option>\n        </select>\n      </label>\n    </div>\n    <button class=\"btn btn-sm btn-primary\" onclick=\"refreshStaffPerformance()\">Refresh</button>\n  </div>\n  <div class=\"hidden text-sm text-error\" data-role=\"error-container\"></div>\n  <div class=\"grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-4\" data-role=\"metric-cards\"></div>\n  <div class=\"grid grid-cols-1 lg:grid-cols-2 gap-6\">\n    <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n      <div class=\"card-body\">\n        <h3 class=\"card-title\">Top Performers by Sales</h3>\n        <div class=\"h-80\">\n          <canvas data-role=\"top-staff-chart\"></canvas>\n        </div>\n      </div>\n    </div>\n    <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n      <div class=\"card-body\">\n        <h3 class=\"card-title\">Sales vs Transactions</h3>\n        <div class=\"h-80\">\n          <canvas data-role=\"efficiency-chart\"></canvas>\n        </div>\n      </div>\n    </div>\n  </div>\n  <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n    <div class=\"card-body\">\n      <h3 class=\"card-title\">Staff Performance Table</h3>\n      <div class=\"overflow-x-auto\">\n        <table class=\"table table-sm\" data-role=\"staff-table\">\n          <thead>\n            <tr><th>Cashier</th><th>Total Sales</th><th>Transactions</th><th>Avg Transaction</th><th>Items/Transaction</th><th>Efficiency Score</th></tr>\n          </thead>\n          <tbody></tbody>\n        </table>\n      </div>\n    </div>\n  </div>\n</div>"}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}'::jsonb,
    NULL,
    NULL,
    'fa-user-tie',
    'Staff Performance Analytics',
    'Staff Performance Analytics'
  )
ON CONFLICT (module_id) DO UPDATE SET
    config = EXCLUDED.config,
    icon = EXCLUDED.icon,
    aria_label = EXCLUDED.aria_label,
    description = EXCLUDED.description;

-- ============================================================================
-- Module 6: Branch Performance Dashboard
-- Queries: Branch Sales Comparison
-- Uses: AggregateRowsController GROUP BY branch_id
-- ============================================================================
INSERT INTO modules (module_id, name, config, org_id, branch_id, icon, aria_label, description) VALUES
  (
    '01948801-0006-7123-8123-000000000006'::uuid,
    'branch_performance_dashboard',
    '{"title":"Branch Performance Dashboard","body":[{"type":"html","html":"<div data-controller=\"BranchPerformanceDashboard\" class=\"space-y-6\">\n  <div class=\"flex flex-wrap items-center justify-between gap-3\">\n    <div class=\"flex flex-wrap gap-3\">\n      <label class=\"form-control w-48\">\n        <span class=\"label-text text-sm\">Date Range</span>\n        <input type=\"date\" class=\"input input-bordered input-sm\" data-filter=\"start_date\" />\n      </label>\n      <label class=\"form-control w-48\">\n        <span class=\"label-text text-sm\">To</span>\n        <input type=\"date\" class=\"input input-bordered input-sm\" data-filter=\"end_date\" />\n      </label>\n    </div>\n    <button class=\"btn btn-sm btn-primary\" onclick=\"refreshBranchPerformance()\">Refresh</button>\n  </div>\n  <div class=\"hidden text-sm text-error\" data-role=\"error-container\"></div>\n  <div class=\"grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-4\" data-role=\"metric-cards\"></div>\n  <div class=\"grid grid-cols-1 lg:grid-cols-2 gap-6\">\n    <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n      <div class=\"card-body\">\n        <h3 class=\"card-title\">Branch Sales Comparison</h3>\n        <div class=\"h-80\">\n          <canvas data-role=\"branch-comparison-chart\"></canvas>\n        </div>\n      </div>\n    </div>\n    <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n      <div class=\"card-body\">\n        <h3 class=\"card-title\">Branch Performance Trend</h3>\n        <div class=\"h-80\">\n          <canvas data-role=\"branch-trend-chart\"></canvas>\n        </div>\n      </div>\n    </div>\n  </div>\n  <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n    <div class=\"card-body\">\n      <h3 class=\"card-title\">Branch Performance Table</h3>\n      <div class=\"overflow-x-auto\">\n        <table class=\"table table-sm\" data-role=\"branch-table\">\n          <thead>\n            <tr><th>Branch</th><th>Total Sales</th><th>Transactions</th><th>Avg Transaction</th><th>Top Product</th><th>Growth %</th></tr>\n          </thead>\n          <tbody></tbody>\n        </table>\n      </div>\n    </div>\n  </div>\n</div>"}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}'::jsonb,
    NULL,
    NULL,
    'fa-store',
    'Branch Performance Dashboard',
    'Branch Performance Dashboard'
  )
ON CONFLICT (module_id) DO UPDATE SET
    config = EXCLUDED.config,
    icon = EXCLUDED.icon,
    aria_label = EXCLUDED.aria_label,
    description = EXCLUDED.description;

-- ============================================================================
-- Module 7: Sales Time-Series Analytics
-- Queries: Daily Sales Summary, Hourly Sales Pattern
-- Uses: AggregateRowsController with date grouping
-- ============================================================================
INSERT INTO modules (module_id, name, config, org_id, branch_id, icon, aria_label, description) VALUES
  (
    '01948801-0007-7123-8123-000000000007'::uuid,
    'sales_timeseries_analytics',
    '{"title":"Sales Time-Series Analytics","body":[{"type":"html","html":"<div data-controller=\"SalesTimeseriesAnalytics\" class=\"space-y-6\">\n  <div class=\"flex flex-wrap items-center justify-between gap-3\">\n    <div class=\"flex flex-wrap gap-3\">\n      <label class=\"form-control w-48\">\n        <span class=\"label-text text-sm\">Date Range</span>\n        <input type=\"date\" class=\"input input-bordered input-sm\" data-filter=\"start_date\" />\n      </label>\n      <label class=\"form-control w-48\">\n        <span class=\"label-text text-sm\">To</span>\n        <input type=\"date\" class=\"input input-bordered input-sm\" data-filter=\"end_date\" />\n      </label>\n      <label class=\"form-control w-32\">\n        <span class=\"label-text text-sm\">Group By</span>\n        <select class=\"select select-bordered select-sm\" data-filter=\"group_by\">\n          <option value=\"day\">Daily</option>\n          <option value=\"week\">Weekly</option>\n          <option value=\"month\">Monthly</option>\n        </select>\n      </label>\n    </div>\n    <button class=\"btn btn-sm btn-primary\" onclick=\"refreshSalesTimeseries()\">Refresh</button>\n  </div>\n  <div class=\"hidden text-sm text-error\" data-role=\"error-container\"></div>\n  <div class=\"grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-4\" data-role=\"metric-cards\"></div>\n  <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n    <div class=\"card-body\">\n      <h3 class=\"card-title\">Sales Trend Over Time</h3>\n      <div class=\"h-80\">\n        <canvas data-role=\"trend-chart\"></canvas>\n      </div>\n    </div>\n  </div>\n  <div class=\"grid grid-cols-1 lg:grid-cols-2 gap-6\">\n    <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n      <div class=\"card-body\">\n        <h3 class=\"card-title\">Hourly Sales Pattern</h3>\n        <div class=\"h-64\">\n          <canvas data-role=\"hourly-chart\"></canvas>\n        </div>\n      </div>\n    </div>\n    <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n      <div class=\"card-body\">\n        <h3 class=\"card-title\">Day of Week Pattern</h3>\n        <div class=\"h-64\">\n          <canvas data-role=\"dow-chart\"></canvas>\n        </div>\n      </div>\n    </div>\n  </div>\n</div>"}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}'::jsonb,
    NULL,
    NULL,
    'fa-chart-line',
    'Sales Time-Series Analytics',
    'Sales Time-Series Analytics'
  )
ON CONFLICT (module_id) DO UPDATE SET
    config = EXCLUDED.config,
    icon = EXCLUDED.icon,
    aria_label = EXCLUDED.aria_label,
    description = EXCLUDED.description;

-- ============================================================================
-- Module 8: Profit & Margin Analytics
-- Queries: Product Profitability, Discount Impact
-- Uses: GenericController with profit_margin column
-- ============================================================================
INSERT INTO modules (module_id, name, config, org_id, branch_id, icon, aria_label, description) VALUES
  (
    '01948801-0008-7123-8123-000000000008'::uuid,
    'profit_margin_analytics',
    '{"title":"Profit & Margin Analytics","body":[{"type":"html","html":"<div data-controller=\"ProfitMarginAnalytics\" class=\"space-y-6\">\n  <div class=\"flex flex-wrap items-center justify-between gap-3\">\n    <div class=\"flex flex-wrap gap-3\">\n      <label class=\"form-control w-48\">\n        <span class=\"label-text text-sm\">Date Range</span>\n        <input type=\"date\" class=\"input input-bordered input-sm\" data-filter=\"start_date\" />\n      </label>\n      <label class=\"form-control w-48\">\n        <span class=\"label-text text-sm\">To</span>\n        <input type=\"date\" class=\"input input-bordered input-sm\" data-filter=\"end_date\" />\n      </label>\n    </div>\n    <button class=\"btn btn-sm btn-primary\" onclick=\"refreshProfitMargin()\">Refresh</button>\n  </div>\n  <div class=\"hidden text-sm text-error\" data-role=\"error-container\"></div>\n  <div class=\"grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-4\" data-role=\"metric-cards\"></div>\n  <div class=\"grid grid-cols-1 lg:grid-cols-2 gap-6\">\n    <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n      <div class=\"card-body\">\n        <h3 class=\"card-title\">Top Products by Profit</h3>\n        <div class=\"h-80\">\n          <canvas data-role=\"top-profit-chart\"></canvas>\n        </div>\n      </div>\n    </div>\n    <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n      <div class=\"card-body\">\n        <h3 class=\"card-title\">Discount Impact Analysis</h3>\n        <div class=\"h-80\">\n          <canvas data-role=\"discount-chart\"></canvas>\n        </div>\n      </div>\n    </div>\n  </div>\n  <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n    <div class=\"card-body\">\n      <h3 class=\"card-title\">Product Profitability Table</h3>\n      <div class=\"overflow-x-auto\">\n        <table class=\"table table-sm\" data-role=\"profitability-table\">\n          <thead>\n            <tr><th>Product</th><th>Revenue</th><th>Cost</th><th>Gross Profit</th><th>Margin %</th><th>Discount Given</th><th>Discount %</th></tr>\n          </thead>\n          <tbody></tbody>\n        </table>\n      </div>\n    </div>\n  </div>\n</div>"}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}'::jsonb,
    NULL,
    NULL,
    'fa-chart-column',
    'Profit & Margin Analytics',
    'Profit & Margin Analytics'
  )
ON CONFLICT (module_id) DO UPDATE SET
    config = EXCLUDED.config,
    icon = EXCLUDED.icon,
    aria_label = EXCLUDED.aria_label,
    description = EXCLUDED.description;

-- ============================================================================
-- Module 9: Customer Segmentation (RFM) - Advanced
-- Note: This requires database view or custom endpoint for RFM calculation
-- ============================================================================
INSERT INTO modules (module_id, name, config, org_id, branch_id, icon, aria_label, description) VALUES
  (
    '01948801-0009-7123-8123-000000000009'::uuid,
    'customer_segmentation_rfm',
    '{"title":"Customer Segmentation (RFM)","body":[{"type":"html","html":"<div data-controller=\"CustomerSegmentationRfm\" class=\"space-y-6\">\n  <div class=\"flex flex-wrap items-center justify-between gap-3\">\n    <label class=\"form-control w-48\">\n      <span class=\"label-text text-sm\">Analysis Period (months)</span>\n      <input type=\"number\" class=\"input input-bordered input-sm\" data-filter=\"months\" value=\"12\" min=\"1\" max=\"36\" />\n    </label>\n    <button class=\"btn btn-sm btn-primary\" onclick=\"refreshCustomerRfm()\">Refresh</button>\n  </div>\n  <div class=\"hidden text-sm text-error\" data-role=\"error-container\"></div>\n  <div class=\"alert alert-info mb-4\">\n    <svg xmlns=\"http://www.w3.org/2000/svg\" fill=\"none\" viewBox=\"0 0 24 24\" class=\"stroke-current shrink-0 w-6 h-6\"><path stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z\"></path></svg>\n    <span><strong>RFM Analysis:</strong> Recency (days since last purchase), Frequency (number of purchases), Monetary (total revenue)</span>\n  </div>\n  <div class=\"grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-4\" data-role=\"metric-cards\"></div>\n  <div class=\"grid grid-cols-1 lg:grid-cols-2 gap-6\">\n    <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n      <div class=\"card-body\">\n        <h3 class=\"card-title\">Customer Segments</h3>\n        <div class=\"h-64\">\n          <canvas data-role=\"segment-chart\"></canvas>\n        </div>\n      </div>\n    </div>\n    <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n      <div class=\"card-body\">\n        <h3 class=\"card-title\">RFM Score Distribution</h3>\n        <div class=\"h-64\">\n          <canvas data-role=\"rfm-distribution-chart\"></canvas>\n        </div>\n      </div>\n    </div>\n  </div>\n  <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n    <div class=\"card-body\">\n      <h3 class=\"card-title\">Customer Segmentation Table</h3>\n      <div class=\"overflow-x-auto\">\n        <table class=\"table table-sm\" data-role=\"rfm-table\">\n          <thead>\n            <tr><th>Customer</th><th>Segment</th><th>Recency (days)</th><th>Frequency</th><th>Monetary</th><th>RFM Score</th><th>Action</th></tr>\n          </thead>\n          <tbody></tbody>\n        </table>\n      </div>\n    </div>\n  </div>\n</div>"}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}'::jsonb,
    NULL,
    NULL,
    'fa-diagram-project',
    'Customer Segmentation (RFM)',
    'Customer Segmentation (RFM)'
  )
ON CONFLICT (module_id) DO UPDATE SET
    config = EXCLUDED.config,
    icon = EXCLUDED.icon,
    aria_label = EXCLUDED.aria_label,
    description = EXCLUDED.description;

-- ============================================================================
-- Module 10: Transaction Export Dashboard
-- Queries: Full transaction export
-- Uses: GenericController with large dataset handling
-- ============================================================================
INSERT INTO modules (module_id, name, config, org_id, branch_id, icon, aria_label, description) VALUES
  (
    '01948801-0010-7123-8123-000000000010'::uuid,
    'transaction_export_dashboard',
    '{"title":"Transaction Export Dashboard","body":[{"type":"html","html":"<div data-controller=\"TransactionExportDashboard\" class=\"space-y-6\">\n  <div class=\"flex flex-wrap items-center justify-between gap-3\">\n    <div class=\"flex flex-wrap gap-3\">\n      <label class=\"form-control w-48\">\n        <span class=\"label-text text-sm\">Date Range</span>\n        <input type=\"date\" class=\"input input-bordered input-sm\" data-filter=\"start_date\" />\n      </label>\n      <label class=\"form-control w-48\">\n        <span class=\"label-text text-sm\">To</span>\n        <input type=\"date\" class=\"input input-bordered input-sm\" data-filter=\"end_date\" />\n      </label>\n      <label class=\"form-control w-32\">\n        <span class=\"label-text text-sm\">Format</span>\n        <select class=\"select select-bordered select-sm\" data-filter=\"format\">\n          <option value=\"csv\">CSV</option>\n          <option value=\"excel\">Excel</option>\n          <option value=\"json\">JSON</option>\n        </select>\n      </label>\n    </div>\n    <div class=\"flex gap-2\">\n      <button class=\"btn btn-sm btn-outline\" onclick=\"previewTransactionExport()\">Preview</button>\n      <button class=\"btn btn-sm btn-primary\" onclick=\"downloadTransactionExport()\">Export</button>\n    </div>\n  </div>\n  <div class=\"hidden text-sm text-error\" data-role=\"error-container\"></div>\n  <div class=\"alert alert-warning\">\n    <svg xmlns=\"http://www.w3.org/2000/svg\" class=\"stroke-current shrink-0 h-6 w-6\" fill=\"none\" viewBox=\"0 0 24 24\"><path stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z\" /></svg>\n    <span><strong>Note:</strong> Large exports may take several minutes. Export includes all transaction details, customer information, and product data.</span>\n  </div>\n  <div class=\"grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-4\" data-role=\"metric-cards\"></div>\n  <div class=\"card bg-base-100 shadow-md shadow-slate-400/50\">\n    <div class=\"card-body\">\n      <h3 class=\"card-title\">Export Preview (First 100 rows)</h3>\n      <div class=\"overflow-x-auto\" style=\"max-height: 400px;\">\n        <table class=\"table table-xs table-pin-rows\" data-role=\"preview-table\">\n          <thead>\n            <tr><th>Date</th><th>Transaction #</th><th>Product</th><th>Customer</th><th>Qty</th><th>Price</th><th>Discount</th><th>VAT</th><th>Total</th><th>Payment</th><th>Cashier</th></tr>\n          </thead>\n          <tbody></tbody>\n        </table>\n      </div>\n    </div>\n  </div>\n</div>"}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}'::jsonb,
    NULL,
    NULL,
    'fa-file-export',
    'Transaction Export Dashboard',
    'Transaction Export Dashboard'
  )
ON CONFLICT (module_id) DO UPDATE SET
    config = EXCLUDED.config,
    icon = EXCLUDED.icon,
    aria_label = EXCLUDED.aria_label,
    description = EXCLUDED.description;

-- ============================================================================
-- Phase 8: End Module Registration
-- Next Step: Create FrontEnd/js/modules/Inventory/Analytics.js
-- ============================================================================

COMMENT ON TABLE modules IS 'Module registry - Phase 8 adds 10 inventory analytics dashboard modules';
