package com.yimeng.okr.controller;

import com.yimeng.okr.annotation.OperationLogAnnotation;
import com.yimeng.okr.dto.ApiResult;
import com.yimeng.okr.enums.UserRole;
import com.yimeng.okr.exception.BusinessException;
import com.yimeng.okr.security.SecurityUtils;
import com.yimeng.okr.service.DatabaseBackupService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/maintenance")
@RequiredArgsConstructor
public class MaintenanceController {

    private final DatabaseBackupService backupService;

    @PostMapping("/backup")
    @OperationLogAnnotation(action = "BACKUP_DATABASE", entityType = "database", sensitive = true)
    public ApiResult<Map<String, String>> backupDatabase() {
        if (!SecurityUtils.hasRole(UserRole.SUPER_ADMIN)) {
            throw new BusinessException(403, "无权限，需要超级管理员");
        }
        return ApiResult.success(Map.of("path", backupService.createBackup().toString()));
    }
}
