-- Migration: Add avatar_url and update menu for Account Settings
-- Created at: 2026-01-15

BEGIN;

-- 1. Add avatar_url to users table if it doesn't exist
-- Note: MySQL 5.7 doesn't support IF NOT EXISTS for columns easily in one line without procedure,
-- but for this environment we assume it doesn't exist or ignore error if strictly handled.
-- However, creating a safe idempotent script is better.
-- Since I can't run complex procedures easily here, I'll just run the ALTER.
-- If it fails, it fails (user can ignore if already applied).
ALTER TABLE `users` ADD COLUMN `avatar_url` VARCHAR(512) DEFAULT NULL AFTER `email`;

-- 2. Remove 'change_password' menu
DELETE FROM `role_menu_permissions` WHERE `menu_id` IN (SELECT `menu_id` FROM `menus` WHERE `menu_code` = 'change_password');
DELETE FROM `menus` WHERE `menu_code` = 'change_password';

-- 3. Add 'account_settings' menu
INSERT INTO `menus` (`menu_code`, `menu_name`, `menu_icon`, `menu_url`, `menu_type`, `sort_order`, `is_active`, `description`)
VALUES ('account_settings', '账户设置', 'UserCog', '/account-settings', 'link', 1, 1, '管理个人账户信息');

-- 4. Grant permissions
-- Grant to Admin (role_id=1) and User (role_id=2)
INSERT INTO `role_menu_permissions` (`role_id`, `menu_id`)
SELECT 1, menu_id FROM `menus` WHERE `menu_code` = 'account_settings';

INSERT INTO `role_menu_permissions` (`role_id`, `menu_id`)
SELECT 2, menu_id FROM `menus` WHERE `menu_code` = 'account_settings';

COMMIT;
