-- Add account_id to expense_categories
ALTER TABLE expense_categories ADD COLUMN account_id INT DEFAULT NULL;
ALTER TABLE expense_categories ADD CONSTRAINT fk_expense_category_account FOREIGN KEY (account_id) REFERENCES accounting_accounts(id) ON DELETE SET NULL;

-- Seed default mappings (Best effort based on Arabic account names vs English categories)
-- Accounts:
-- 13: مصروفات الرواتب (Salaries)
-- 16: مصروفات التسويق (Marketing)
-- 30: مصروفات الإيجار (Rent)
-- 31: مصروفات المرافق (Utilities)
-- 12: تكلفة البضاعة المباعة (COGS)

-- Update 'Salaries' category
UPDATE expense_categories SET account_id = 13 WHERE name LIKE '%Salaries%' OR name LIKE '%رواتب%';

-- Update 'Rent' category
UPDATE expense_categories SET account_id = 30 WHERE name LIKE '%Rent%' OR name LIKE '%إيجار%';

-- Update 'Utilities' category
UPDATE expense_categories SET account_id = 31 WHERE name LIKE '%Utilities%' OR name LIKE '%مرافق%';

-- Update 'Advertising' category
UPDATE expense_categories SET account_id = 16 WHERE name LIKE '%Advertising%' OR name LIKE '%Marketing%' OR name LIKE '%تسو%';
