package com.reading.platform.controller.admin;

import com.reading.platform.common.annotation.RequireRole;
import com.reading.platform.common.enums.UserRole;
import com.reading.platform.common.response.Result;
import com.reading.platform.dto.request.AdminSettingsUpdateRequest;
import com.reading.platform.dto.response.SystemSettingsResponse;
import com.reading.platform.service.SystemSettingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "管理员 - 设置", description = "管理员系统设置管理")
@RestController
@RequestMapping("/api/v1/admin/settings")
@RequiredArgsConstructor
@RequireRole(UserRole.ADMIN)
public class AdminSettingsController {

    // Use tenantId="0" for global admin settings
    private static final String ADMIN_TENANT_ID = "0";

    private final SystemSettingService systemSettingService;

    @Operation(summary = "获取管理员系统设置")
    @GetMapping
    public Result<SystemSettingsResponse> getSettings() {
        Map<String, String> settings = systemSettingService.getSettings(ADMIN_TENANT_ID);

        SystemSettingsResponse response = new SystemSettingsResponse();
        response.setSystemName(settings.get("systemName"));
        response.setSystemLogo(settings.get("systemLogo"));
        response.setLoginBackground(settings.get("loginBackground"));
        response.setAnnouncement(settings.get("announcement"));
        response.setContactInfo(settings.get("contactInfo"));

        return Result.success(response);
    }

    @Operation(summary = "更新管理员系统设置")
    @PutMapping
    public Result<Void> updateSettings(@RequestBody AdminSettingsUpdateRequest request) {
        Map<String, String> stringSettings = new java.util.HashMap<>();

        if (request.getSystemName() != null) stringSettings.put("systemName", request.getSystemName());
        if (request.getSystemDesc() != null) stringSettings.put("systemDesc", request.getSystemDesc());
        if (request.getContactPhone() != null) stringSettings.put("contactPhone", request.getContactPhone());
        if (request.getContactEmail() != null) stringSettings.put("contactEmail", request.getContactEmail());
        if (request.getSystemLogo() != null) stringSettings.put("systemLogo", request.getSystemLogo());
        if (request.getPasswordStrength() != null) stringSettings.put("passwordStrength", request.getPasswordStrength());
        if (request.getMaxLoginAttempts() != null) stringSettings.put("maxLoginAttempts", request.getMaxLoginAttempts().toString());
        if (request.getTokenExpire() != null) stringSettings.put("tokenExpire", request.getTokenExpire());
        if (request.getForceHttps() != null) stringSettings.put("forceHttps", request.getForceHttps().toString());
        if (request.getEmailEnabled() != null) stringSettings.put("emailEnabled", request.getEmailEnabled().toString());
        if (request.getSmsEnabled() != null) stringSettings.put("smsEnabled", request.getSmsEnabled().toString());
        if (request.getStorageType() != null) stringSettings.put("storageType", request.getStorageType());
        if (request.getMaxFileSize() != null) stringSettings.put("maxFileSize", request.getMaxFileSize().toString());
        if (request.getAllowedTypes() != null) stringSettings.put("allowedTypes", request.getAllowedTypes());
        if (request.getDefaultTeacherQuota() != null) stringSettings.put("defaultTeacherQuota", request.getDefaultTeacherQuota().toString());
        if (request.getDefaultStudentQuota() != null) stringSettings.put("defaultStudentQuota", request.getDefaultStudentQuota().toString());
        if (request.getEnableAutoExpire() != null) stringSettings.put("enableAutoExpire", request.getEnableAutoExpire().toString());
        if (request.getNotifyBeforeDays() != null) stringSettings.put("notifyBeforeDays", request.getNotifyBeforeDays().toString());

        systemSettingService.updateSettings(ADMIN_TENANT_ID, stringSettings);
        return Result.success();
    }
}
