-- v3.5.8: HQ 权限追加/撤销、城市门店服务、账号城市范围

ALTER TABLE `hq_account`
  MODIFY COLUMN `admin_role` ENUM(
    'SUPER_ADMIN',
    'OPS',
    'FINANCE',
    'CUSTOMER_SERVICE',
    'CITY_STORE_SERVICE'
  ) NOT NULL DEFAULT 'OPS';

ALTER TABLE `hq_role_permission`
  MODIFY COLUMN `admin_role` ENUM(
    'SUPER_ADMIN',
    'OPS',
    'FINANCE',
    'CUSTOMER_SERVICE',
    'CITY_STORE_SERVICE'
  ) NOT NULL;

ALTER TABLE `hq_account_permission`
  ADD COLUMN `effect` ENUM('GRANT', 'DENY') NOT NULL DEFAULT 'GRANT' AFTER `permission_key`;

CREATE TABLE IF NOT EXISTS `hq_account_city` (
  `hq_account_id` BIGINT UNSIGNED NOT NULL,
  `city_id` BIGINT UNSIGNED NOT NULL,
  `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  PRIMARY KEY (`hq_account_id`, `city_id`),
  INDEX `hq_account_city_city_id_idx` (`city_id`),
  CONSTRAINT `hq_account_city_hq_account_id_fkey`
    FOREIGN KEY (`hq_account_id`) REFERENCES `hq_account` (`id`) ON DELETE CASCADE,
  CONSTRAINT `hq_account_city_city_id_fkey`
    FOREIGN KEY (`city_id`) REFERENCES `common_city` (`id`) ON DELETE CASCADE
) DEFAULT CHARSET = utf8mb4;

-- 存量 stores 拆键：补齐审核/评价/分类/账户/资源，保持与拆分前菜单一致
INSERT IGNORE INTO `hq_role_permission` (`admin_role`, `permission_key`, `created_at`)
SELECT `admin_role`, 'store_audits', NOW(3) FROM `hq_role_permission` WHERE `permission_key` = 'stores';
INSERT IGNORE INTO `hq_role_permission` (`admin_role`, `permission_key`, `created_at`)
SELECT `admin_role`, 'store_ratings', NOW(3) FROM `hq_role_permission` WHERE `permission_key` = 'stores';
INSERT IGNORE INTO `hq_role_permission` (`admin_role`, `permission_key`, `created_at`)
SELECT `admin_role`, 'store_categories', NOW(3) FROM `hq_role_permission` WHERE `permission_key` = 'stores';
INSERT IGNORE INTO `hq_role_permission` (`admin_role`, `permission_key`, `created_at`)
SELECT `admin_role`, 'store_accounts', NOW(3) FROM `hq_role_permission` WHERE `permission_key` = 'stores';
INSERT IGNORE INTO `hq_role_permission` (`admin_role`, `permission_key`, `created_at`)
SELECT `admin_role`, 'store_media', NOW(3) FROM `hq_role_permission` WHERE `permission_key` = 'stores';

INSERT IGNORE INTO `hq_account_permission` (`hq_account_id`, `permission_key`, `effect`, `created_at`)
SELECT `hq_account_id`, 'store_audits', `effect`, NOW(3) FROM `hq_account_permission` WHERE `permission_key` = 'stores';
INSERT IGNORE INTO `hq_account_permission` (`hq_account_id`, `permission_key`, `effect`, `created_at`)
SELECT `hq_account_id`, 'store_ratings', `effect`, NOW(3) FROM `hq_account_permission` WHERE `permission_key` = 'stores';
INSERT IGNORE INTO `hq_account_permission` (`hq_account_id`, `permission_key`, `effect`, `created_at`)
SELECT `hq_account_id`, 'store_categories', `effect`, NOW(3) FROM `hq_account_permission` WHERE `permission_key` = 'stores';
INSERT IGNORE INTO `hq_account_permission` (`hq_account_id`, `permission_key`, `effect`, `created_at`)
SELECT `hq_account_id`, 'store_accounts', `effect`, NOW(3) FROM `hq_account_permission` WHERE `permission_key` = 'stores';
INSERT IGNORE INTO `hq_account_permission` (`hq_account_id`, `permission_key`, `effect`, `created_at`)
SELECT `hq_account_id`, 'store_media', `effect`, NOW(3) FROM `hq_account_permission` WHERE `permission_key` = 'stores';

-- 删除分类：有分类权限的角色/账号补齐，城市门店服务除外（可新增、不可删除）
INSERT IGNORE INTO `hq_role_permission` (`admin_role`, `permission_key`, `created_at`)
SELECT `admin_role`, 'store_categories_delete', NOW(3)
FROM `hq_role_permission`
WHERE `permission_key` = 'store_categories'
  AND `admin_role` <> 'CITY_STORE_SERVICE';

INSERT IGNORE INTO `hq_account_permission` (`hq_account_id`, `permission_key`, `effect`, `created_at`)
SELECT p.`hq_account_id`, 'store_categories_delete', p.`effect`, NOW(3)
FROM `hq_account_permission` p
INNER JOIN `hq_account` a ON a.`id` = p.`hq_account_id`
WHERE p.`permission_key` = 'store_categories'
  AND a.`admin_role` <> 'CITY_STORE_SERVICE';
