package com.erp.systemtool.controller;

import com.erp.systemtool.service.SystemConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * System Configuration Controller
 * 
 * Provides REST API for system configuration management.
 * Migrations from PHP SystemConfigController.
 */
@RestController
@RequestMapping("/api/system/configs")
@RequiredArgsConstructor
public class SystemConfigController {

    private final SystemConfigService configService;

    /**
     * Get all configurations
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> getAllConfigs(
            @RequestHeader(value = "X-Tenant-Id", defaultValue = "1") Integer tenantId) {
        Map<String, Map<String, String>> configs = configService.getAllConfigs(tenantId);
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "data", configs,
            "message", "success"
        ));
    }

    /**
     * Get configurations for a specific group
     */
    @GetMapping("/{group}")
    public ResponseEntity<Map<String, Object>> getGroupConfigs(
            @PathVariable String group,
            @RequestHeader(value = "X-Tenant-Id", defaultValue = "1") Integer tenantId) {
        Map<String, String> configs = configService.getGroupConfigs(tenantId, group);
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "data", configs,
            "message", "success"
        ));
    }

    /**
     * Update a configuration value
     */
    @PutMapping
    public ResponseEntity<Map<String, Object>> updateConfig(
            @RequestBody Map<String, Object> request,
            @RequestHeader(value = "X-Tenant-Id", defaultValue = "1") Integer tenantId) {
        
        String group = (String) request.get("group");
        String key = (String) request.get("key");
        Object value = request.get("value");

        if (group == null || key == null || value == null) {
            return ResponseEntity.badRequest().body(Map.of(
                "code", 400,
                "message", "Missing required parameters: group, key, value"
            ));
        }

        configService.updateConfig(tenantId, group, key, value);

        return ResponseEntity.ok(Map.of(
            "code", 200,
            "message", "更新成功"
        ));
    }

    /**
     * Batch update configurations
     */
    @PostMapping("/batch")
    public ResponseEntity<Map<String, Object>> batchUpdate(
            @RequestBody Map<String, Object> request,
            @RequestHeader(value = "X-Tenant-Id", defaultValue = "1") Integer tenantId) {
        
        @SuppressWarnings("unchecked")
        Map<String, Map<String, Object>> configs = (Map<String, Map<String, Object>>) request.get("configs");

        if (configs == null) {
            return ResponseEntity.badRequest().body(Map.of(
                "code", 400,
                "message", "Missing 'configs' parameter"
            ));
        }

        configService.batchUpdateConfigs(tenantId, configs);

        return ResponseEntity.ok(Map.of(
            "code", 200,
            "message", "更新成功"
        ));
    }

    /**
     * Get a single config value
     */
    @GetMapping("/{group}/{key}")
    public ResponseEntity<Map<String, Object>> getConfigValue(
            @PathVariable String group,
            @PathVariable String key,
            @RequestParam(required = false) String defaultValue,
            @RequestHeader(value = "X-Tenant-Id", defaultValue = "1") Integer tenantId) {
        
        String value = configService.getConfigValue(tenantId, group, key, defaultValue);
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", Map.of(
            "group", group,
            "key", key,
            "value", value != null ? value : ""
        ));
        result.put("message", "success");
        
        return ResponseEntity.ok(result);
    }
}
