-- +goose Up

INSERT INTO permissions (code, name, resource, action) VALUES
('backup:view', '查看备份状态', 'backup', 'view'),
('backup:schedule:update', '修改备份定时', 'backup', 'schedule_update')
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  resource = VALUES(resource),
  action = VALUES(action);

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id FROM roles r, permissions p
WHERE r.code = 'super_admin' AND p.code IN ('backup:view', 'backup:schedule:update');

INSERT INTO system_configs (`key`, `value`, description) VALUES
('backup.schedule_enabled', 'true', '在线备份定时任务是否启用'),
('backup.full_hour', '4', '每日全量备份小时（Asia/Shanghai）'),
('backup.full_minute', '30', '每日全量备份分钟（Asia/Shanghai）'),
('backup.binlog_interval_minutes', '1', 'binlog 归档间隔分钟（1-5）')
ON DUPLICATE KEY UPDATE description = VALUES(description);

-- +goose Down

DELETE rp FROM role_permissions rp
JOIN permissions p ON p.id = rp.permission_id
WHERE p.code IN ('backup:view', 'backup:schedule:update');

DELETE FROM permissions WHERE code IN ('backup:view', 'backup:schedule:update');

DELETE FROM system_configs WHERE `key` IN (
  'backup.schedule_enabled',
  'backup.full_hour',
  'backup.full_minute',
  'backup.binlog_interval_minutes'
);
