package com.smyhub.infogenie.infogeniebackendjava.controller;

import com.smyhub.infogenie.infogeniebackendjava.dto.request.AdminAIRuntimeRequest;
import com.smyhub.infogenie.infogeniebackendjava.dto.request.AdminDisabledUpdateRequest;
import com.smyhub.infogenie.infogeniebackendjava.dto.request.AdminSourceUpdateRequest;
import com.smyhub.infogenie.infogeniebackendjava.dto.response.AIRuntimeResponse;
import com.smyhub.infogenie.infogeniebackendjava.dto.response.DiagnosticsResponse;
import com.smyhub.infogenie.infogeniebackendjava.dto.response.SiteSourceResponse;
import com.smyhub.infogenie.infogeniebackendjava.service.AIRuntimeService;
import com.smyhub.infogenie.infogeniebackendjava.service.HealthService;
import com.smyhub.infogenie.infogeniebackendjava.service.SiteConfigService;
import com.smyhub.infogenie.infogeniebackendjava.util.AdminTokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Admin-only endpoints protected by X-Site-Admin-Token header.
 *
 * PUT  /api/admin/site/60s-disabled
 * PUT  /api/admin/site/60s-source
 * PUT  /api/admin/site/ai-model-disabled
 * GET  /api/admin/site/ai-runtime
 * PUT  /api/admin/site/ai-runtime
 * GET  /api/admin/site/diagnostics
 */
@RestController
@RequestMapping("/api/admin/site")
@RequiredArgsConstructor
public class AdminController {

    private final SiteConfigService siteConfigService;
    private final AIRuntimeService aiRuntimeService;
    private final HealthService healthService;
    private final AdminTokenUtil adminTokenUtil;

    @PutMapping("/60s-disabled")
    public ResponseEntity<?> update60sDisabled(@RequestBody AdminDisabledUpdateRequest req,
                                                HttpServletRequest request) {
        if (!adminTokenUtil.isValid(request)) return unauthorized();
        int count = siteConfigService.update60sDisabled(
                req.getDisabled() != null ? req.getDisabled() : java.util.List.of());
        return ResponseEntity.ok(Map.of("ok", true, "count", count));
    }

    @PutMapping("/60s-source")
    public ResponseEntity<?> update60sSource(@RequestBody AdminSourceUpdateRequest req,
                                              HttpServletRequest request) {
        if (!adminTokenUtil.isValid(request)) return unauthorized();
        try {
            SiteSourceResponse resp = siteConfigService.update60sSource(req.getSourceId());
            return ResponseEntity.ok(resp);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        }
    }

    @PutMapping("/ai-model-disabled")
    public ResponseEntity<?> updateAiModelDisabled(@RequestBody AdminDisabledUpdateRequest req,
                                                    HttpServletRequest request) {
        if (!adminTokenUtil.isValid(request)) return unauthorized();
        int count = siteConfigService.updateAiModelDisabled(
                req.getDisabled() != null ? req.getDisabled() : java.util.List.of());
        return ResponseEntity.ok(Map.of("ok", true, "count", count));
    }

    @GetMapping("/ai-runtime")
    public ResponseEntity<?> getAiRuntime(HttpServletRequest request) {
        if (!adminTokenUtil.isValid(request)) return unauthorized();
        AIRuntimeResponse resp = aiRuntimeService.getRuntimeResponse();
        return ResponseEntity.ok(resp);
    }

    @PutMapping("/ai-runtime")
    public ResponseEntity<?> updateAiRuntime(@RequestBody AdminAIRuntimeRequest req,
                                              HttpServletRequest request) {
        if (!adminTokenUtil.isValid(request)) return unauthorized();
        AIRuntimeResponse resp = aiRuntimeService.updateRuntime(req);
        return ResponseEntity.ok(Map.of("ok", true, "runtime", resp));
    }

    @GetMapping("/diagnostics")
    public ResponseEntity<?> getDiagnostics(HttpServletRequest request) {
        if (!adminTokenUtil.isValid(request)) return unauthorized();
        DiagnosticsResponse resp = healthService.buildDiagnostics();
        return ResponseEntity.ok(resp);
    }

    private ResponseEntity<Map<String, Object>> unauthorized() {
        return ResponseEntity.status(401)
                .body(Map.of("error", "无效或缺失的管理员令牌"));
    }
}
