-- ============================================================================
-- Phase 8: Grant Inventory Analytics Modules to Dev Team Group
-- Purpose: Make all 13 inventory analytics modules accessible to Dev Team
-- ============================================================================

-- Grant all Phase 8 Inventory Analytics modules to Dev Team group
INSERT INTO group_module_permissions (group_id, module_id, action)
SELECT 
    '0194a8b2-eeee-7123-8123-123456789ace'::uuid AS group_id,  -- Dev Team
    m.module_id,
    'allow' AS action
FROM modules m
WHERE m.name IN (
    'vat_analytics_dashboard',
    'product_movement_analytics',
    'payment_methods_analytics',
    'customer_analytics_dashboard',
    'staff_performance_analytics',
    'branch_performance_dashboard',
    'sales_timeseries_analytics',
    'profit_margin_analytics',
    'customer_segmentation_rfm',
    'transaction_export_dashboard',
    'top_10_best_sellers',
    'slow_moving_items',
    'vat_summary_report'
)
ON CONFLICT DO NOTHING;

-- Verify grants were created
SELECT 
    g.name AS group_name,
    m.name AS module_name,
    m.icon,
    gmp.action
FROM group_module_permissions gmp
JOIN groups g ON g.group_id = gmp.group_id
JOIN modules m ON m.module_id = gmp.module_id
WHERE g.name = 'Dev Team'
  AND m.name IN (
    'vat_analytics_dashboard',
    'product_movement_analytics',
    'payment_methods_analytics',
    'customer_analytics_dashboard',
    'staff_performance_analytics',
    'branch_performance_dashboard',
    'sales_timeseries_analytics',
    'profit_margin_analytics',
    'customer_segmentation_rfm',
    'transaction_export_dashboard',
    'top_10_best_sellers',
    'slow_moving_items',
    'vat_summary_report'
  )
ORDER BY m.name;
