-- Migration: 049_add_stat_card_permissions.sql
-- Description: Add permission_required values to dashboard stat cards based on their module
-- Date: 2024-11-11

-- Main dashboard cards
UPDATE dashboard_stat_cards SET permission_required = 'crm.view' WHERE card_key = 'total_customers' AND dashboard_section = 'main';
UPDATE dashboard_stat_cards SET permission_required = 'inventory.view' WHERE card_key = 'total_products' AND dashboard_section = 'main';
UPDATE dashboard_stat_cards SET permission_required = 'sales.view' WHERE card_key = 'sales_month' AND dashboard_section = 'main';
UPDATE dashboard_stat_cards SET permission_required = 'invoices.view' WHERE card_key = 'pending_invoices' AND dashboard_section = 'main';

-- CRM dashboard cards
UPDATE dashboard_stat_cards SET permission_required = 'crm.view' WHERE dashboard_section = 'crm';

-- HR dashboard cards
UPDATE dashboard_stat_cards SET permission_required = 'hr.view' WHERE dashboard_section = 'hr';

-- Inventory dashboard cards
UPDATE dashboard_stat_cards SET permission_required = 'inventory.view' WHERE dashboard_section = 'inventory';

-- Manufacturing dashboard cards
UPDATE dashboard_stat_cards SET permission_required = 'manufacturing.view' WHERE dashboard_section = 'manufacturing';

-- Sales dashboard cards
UPDATE dashboard_stat_cards SET permission_required = 'sales.view' WHERE dashboard_section = 'sales';

-- Product categories dashboard cards
UPDATE dashboard_stat_cards SET permission_required = 'inventory.view' WHERE dashboard_section = 'product_categories';

-- Lot tracking dashboard cards
UPDATE dashboard_stat_cards SET permission_required = 'inventory.view' WHERE dashboard_section = 'lot_tracking';

-- Verify changes
SELECT dashboard_section, COUNT(*) as card_count, 
       COUNT(CASE WHEN permission_required IS NOT NULL THEN 1 END) as with_permission
FROM dashboard_stat_cards 
GROUP BY dashboard_section
ORDER BY dashboard_section;
