INSERT INTO permission (id, permission_code, permission_name, module)
SELECT t.next_id, 'enterprise.read', '查看企业', 'enterprise'
FROM (SELECT IFNULL(MAX(id), 0) + 1 AS next_id FROM permission) t
WHERE NOT EXISTS (
    SELECT 1 FROM permission WHERE permission_code = 'enterprise.read'
);

INSERT INTO permission (id, permission_code, permission_name, module)
SELECT t.next_id, 'enterprise.manage', '管理企业', 'enterprise'
FROM (SELECT IFNULL(MAX(id), 0) + 1 AS next_id FROM permission) t
WHERE NOT EXISTS (
    SELECT 1 FROM permission WHERE permission_code = 'enterprise.manage'
);

INSERT INTO permission (id, permission_code, permission_name, module)
SELECT t.next_id, 'enterprise.delete', '删除企业', 'enterprise'
FROM (SELECT IFNULL(MAX(id), 0) + 1 AS next_id FROM permission) t
WHERE NOT EXISTS (
    SELECT 1 FROM permission WHERE permission_code = 'enterprise.delete'
);

UPDATE permission
SET permission_name = '查看企业',
    module = 'enterprise'
WHERE permission_code = 'enterprise.read';

UPDATE permission
SET permission_name = '管理企业',
    module = 'enterprise'
WHERE permission_code = 'enterprise.manage';

UPDATE permission
SET permission_name = '删除企业',
    module = 'enterprise'
WHERE permission_code = 'enterprise.delete';

UPDATE menu
SET permission_code = 'enterprise.read',
    updated_at = CURRENT_TIMESTAMP
WHERE route_path = '/enterprises';

SET @next_role_permission_id := (SELECT IFNULL(MAX(id), 0) FROM role_permission);

INSERT INTO role_permission (id, tenant_id, role_id, permission_id)
SELECT
    (@next_role_permission_id := @next_role_permission_id + 1) AS id,
    src.tenant_id,
    src.role_id,
    p.id AS permission_id
FROM (
    SELECT DISTINCT rp.tenant_id, rp.role_id
    FROM role_permission rp
    JOIN permission legacy ON legacy.id = rp.permission_id
    WHERE legacy.permission_code IN ('enterprises.read', 'enterprises.create', 'tenant.manage', 'project.create', 'enterprise.delete', 'enterprise.manage')
) src
JOIN permission p ON p.permission_code = 'enterprise.read'
WHERE NOT EXISTS (
      SELECT 1
      FROM role_permission rp
      WHERE rp.tenant_id = src.tenant_id
        AND rp.role_id = src.role_id
        AND rp.permission_id = p.id
  );

INSERT INTO role_permission (id, tenant_id, role_id, permission_id)
SELECT
    (@next_role_permission_id := @next_role_permission_id + 1) AS id,
    src.tenant_id,
    src.role_id,
    p.id AS permission_id
FROM (
    SELECT DISTINCT rp.tenant_id, rp.role_id
    FROM role_permission rp
    JOIN permission legacy ON legacy.id = rp.permission_id
    WHERE legacy.permission_code IN ('enterprises.create', 'tenant.manage', 'enterprise.manage')
) src
JOIN permission p ON p.permission_code = 'enterprise.manage'
WHERE NOT EXISTS (
      SELECT 1
      FROM role_permission rp
      WHERE rp.tenant_id = src.tenant_id
        AND rp.role_id = src.role_id
        AND rp.permission_id = p.id
  );

INSERT INTO role_permission (id, tenant_id, role_id, permission_id)
SELECT
    (@next_role_permission_id := @next_role_permission_id + 1) AS id,
    src.tenant_id,
    src.role_id,
    p.id AS permission_id
FROM (
    SELECT DISTINCT rp.tenant_id, rp.role_id
    FROM role_permission rp
    JOIN permission legacy ON legacy.id = rp.permission_id
    WHERE legacy.permission_code = 'enterprise.delete'
) src
JOIN permission p ON p.permission_code = 'enterprise.delete'
WHERE NOT EXISTS (
      SELECT 1
      FROM role_permission rp
      WHERE rp.tenant_id = src.tenant_id
        AND rp.role_id = src.role_id
        AND rp.permission_id = p.id
  );

DELETE rp
FROM role_permission rp
JOIN permission legacy ON legacy.id = rp.permission_id
WHERE legacy.permission_code IN ('enterprises.read', 'enterprises.create');

DELETE FROM permission
WHERE permission_code IN ('enterprises.read', 'enterprises.create');

INSERT INTO role_permission (id, tenant_id, role_id, permission_id)
SELECT
    (@next_role_permission_id := @next_role_permission_id + 1) AS id,
    r.tenant_id,
    r.id AS role_id,
    p.id AS permission_id
FROM role r
JOIN permission p ON p.permission_code = 'enterprise.read'
WHERE r.role_code IN ('TENANT_ADMIN', 'PROJECT_OWNER', 'EXECUTOR', 'AUDITOR', 'FINANCE')
  AND r.is_deleted = 0
  AND NOT EXISTS (
      SELECT 1
      FROM role_permission rp
      WHERE rp.tenant_id = r.tenant_id
        AND rp.role_id = r.id
        AND rp.permission_id = p.id
  );
