package com.aps.controller;

import com.aps.common.util.R;
import com.aps.service.UserStrategyRuleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userStrategyRule")
@Tag(name = "用户策略规则", description = "用户策略规则")
public class UserStrategyRuleController {

    @Autowired
    private UserStrategyRuleService userStrategyRuleService;

    @PostMapping("/effective")
    @Operation(
            summary = "获取当前生效策略",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "前端传 userId + baseRuleId，baseRuleId 为 STRATEGY_RULE.ID；后端优先返回该基础策略对应的用户策略，没有则返回全局策略。",
                    required = true,
                    content = @Content(
                            mediaType = "application/json",
                            examples = {
                                    @ExampleObject(
                                            name = "按基础策略获取生效策略",
                                            value = "{\n" +
                                                    "  \"userId\": 10001,\n" +
                                                    "  \"baseRuleId\": 1\n" +
                                                    "}"
                                    ),
                                    @ExampleObject(
                                            name = "指定用户策略",
                                            value = "{\n" +
                                                    "  \"userId\": 10001,\n" +
                                                    "  \"userRuleId\": \"f3d7e0f0-3b8a-4d0c-8f2a-7d4e64f4c901\"\n" +
                                                    "}"
                                    )
                            }
                    )
            ),
            responses = @ApiResponse(
                    responseCode = "200",
                    description = "查询成功",
                    content = @Content(
                            mediaType = "application/json",
                            examples = @ExampleObject(
                                    name = "策略详情响应",
                                    value = "{\n" +
                                            "  \"code\": 200,\n" +
                                            "  \"msg\": \"success\",\n" +
                                            "  \"data\": {\n" +
                                            "    \"id\": 1,\n" +
                                            "    \"source\": \"USER\",\n" +
                                            "    \"userRuleId\": \"f3d7e0f0-3b8a-4d0c-8f2a-7d4e64f4c901\",\n" +
                                            "    \"baseRuleId\": 1,\n" +
                                            "    \"sceneId\": null,\n" +
                                            "    \"name\": \"交期优先\",\n" +
                                            "    \"isjit\": false,\n" +
                                            "    \"forwardScheduling\": [\n" +
                                            "      {\n" +
                                            "        \"name\": \"deliveryDate\",\n" +
                                            "        \"title\": \"交期优先\",\n" +
                                            "        \"content\": \"按订单交期排序\",\n" +
                                            "        \"amplitude\": 80,\n" +
                                            "        \"value\": true,\n" +
                                            "        \"sort\": 1\n" +
                                            "      }\n" +
                                            "    ],\n" +
                                            "    \"kpiConfig\": [\n" +
                                            "      {\n" +
                                            "        \"name\": \"makespan\",\n" +
                                            "        \"title\": \"最早完工时间\",\n" +
                                            "        \"enabled\": true,\n" +
                                            "        \"isMinimize\": true,\n" +
                                            "        \"level\": 1,\n" +
                                            "        \"weight\": 0.4,\n" +
                                            "        \"val\": 0,\n" +
                                            "        \"sort\": 1\n" +
                                            "      }\n" +
                                            "    ]\n" +
                                            "  },\n" +
                                            "  \"timestamp\": \"2026-04-30T10:00:00.000Z\"\n" +
                                            "}"
                            )
                    )
            )
    )
    public R<Map<String, Object>> effective(@RequestBody Map<String, Object> params) {
        return R.ok(userStrategyRuleService.getEffectiveRule(params));
    }

    @PostMapping("/save")
    @Operation(
            summary = "保存用户策略",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "前端传 userId + baseRuleId 保存用户策略，baseRuleId 为 STRATEGY_RULE.ID；后端按 userId + baseRuleId 新增或更新 USER_STRATEGY_RULE。",
                    required = true,
                    content = @Content(
                            mediaType = "application/json",
                            examples = {
                                    @ExampleObject(
                                            name = "从全局策略保存用户规则",
                                            value = "{\n" +
                                                    "  \"userId\": 10001,\n" +
                                                    "  \"baseRuleId\": 1,\n" +
                                                    "  \"isjit\": \"true\",\n" +
                                                    "  \"forwardScheduling\": [\n" +
                                                    "    {\n" +
                                                    "      \"name\": \"deliveryDate\",\n" +
                                                    "      \"title\": \"交期优先\",\n" +
                                                    "      \"content\": \"按订单交期排序\",\n" +
                                                    "      \"amplitude\": 80,\n" +
                                                    "      \"value\": true,\n" +
                                                    "      \"sort\": 1\n" +
                                                    "    }\n" +
                                                    "  ],\n" +
                                                    "  \"kpiConfig\": [\n" +
                                                    "    {\n" +
                                                    "      \"name\": \"makespan\",\n" +
                                                    "      \"title\": \"最早完工时间\",\n" +
                                                    "      \"enabled\": true,\n" +
                                                    "      \"isMinimize\": true,\n" +
                                                    "      \"level\": 1,\n" +
                                                    "      \"weight\": 0.4,\n" +
                                                    "      \"val\": 0,\n" +
                                                    "      \"sort\": 1\n" +
                                                    "    }\n" +
                                                    "  ]\n" +
                                                    "}"
                                    ),
                                    @ExampleObject(
                                            name = "更新已有用户规则",
                                            value = "{\n" +
                                                    "  \"userId\": 10001,\n" +
                                                    "  \"baseRuleId\": 1,\n" +
                                                    "  \"isjit\": \"false\",\n" +
                                                    "  \"forwardScheduling\": [],\n" +
                                                    "  \"kpiConfig\": []\n" +
                                                    "}"
                                    )
                            }
                    )
            )
    )
    public R<Map<String, Object>> save(@RequestBody Map<String, Object> params) {
        return R.ok(userStrategyRuleService.saveUserRule(params));
    }

    @PostMapping("/reset")
    @Operation(
            summary = "恢复全局策略",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "删除用户自定义策略，恢复使用全局策略。",
                    required = true,
                    content = @Content(
                            mediaType = "application/json",
                            examples = {
                                    @ExampleObject(
                                            name = "按全局策略恢复",
                                            value = "{\n" +
                                                    "  \"userId\": 10001,\n" +
                                                    "  \"id\": \"GLOBAL:1\"\n" +
                                                    "}"
                                    ),
                                    @ExampleObject(
                                            name = "按用户策略恢复",
                                            value = "{\n" +
                                                    "  \"userId\": 10001,\n" +
                                                    "  \"id\": \"USER:f3d7e0f0-3b8a-4d0c-8f2a-7d4e64f4c901\"\n" +
                                                    "}"
                                    )
                            }
                    )
            )
    )
    public R<Boolean> reset(@RequestBody Map<String, Object> params) {
        return R.ok(userStrategyRuleService.resetUserRule(params));
    }

    @GetMapping("/globalList")
    @Operation(
            summary = "获取策略下拉选项",
            description = "按 STRATEGY_RULE 生成下拉。全局有几条就返回几条；当前用户已保存过的基础策略返回 USER，否则返回 GLOBAL。id/baseRuleId 都是 STRATEGY_RULE.ID。",
            responses = @ApiResponse(
                    responseCode = "200",
                    description = "查询成功",
                    content = @Content(
                            mediaType = "application/json",
                            examples = @ExampleObject(
                                    name = "四个策略下拉示例",
                                    value = "{\n" +
                                            "  \"code\": 200,\n" +
                                            "  \"msg\": \"success\",\n" +
                                            "  \"data\": [\n" +
                                            "    {\n" +
                                            "      \"id\": 1,\n" +
                                            "      \"source\": \"USER\",\n" +
                                            "      \"baseRuleId\": 1,\n" +
                                            "      \"userRuleId\": \"f3d7e0f0-3b8a-4d0c-8f2a-7d4e64f4c901\",\n" +
                                            "      \"name\": \"交期优先\",\n" +
                                            "      \"globalRuleId\": 1,\n" +
                                            "      \"globalRuleName\": \"交期优先\",\n" +
                                            "      \"isjit\": false,\n" +
                                            "      \"forwardScheduling\": [\n" +
                                            "        {\n" +
                                            "          \"name\": \"deliveryDate\",\n" +
                                            "          \"title\": \"交期优先\",\n" +
                                            "          \"content\": \"按订单交期排序\",\n" +
                                            "          \"amplitude\": 80,\n" +
                                            "          \"value\": true,\n" +
                                            "          \"sort\": 1\n" +
                                            "        }\n" +
                                            "      ],\n" +
                                            "      \"isDefault\": 1\n" +
                                            "    },\n" +
                                            "    {\n" +
                                            "      \"id\": 2,\n" +
                                            "      \"source\": \"GLOBAL\",\n" +
                                            "      \"baseRuleId\": 2,\n" +
                                            "      \"name\": \"齐套优先\",\n" +
                                            "      \"globalRuleId\": 2,\n" +
                                            "      \"globalRuleName\": \"齐套优先\",\n" +
                                            "      \"isjit\": false,\n" +
                                            "      \"forwardScheduling\": []\n" +
                                            "    },\n" +
                                            "    {\n" +
                                            "      \"id\": 3,\n" +
                                            "      \"source\": \"GLOBAL\",\n" +
                                            "      \"baseRuleId\": 3,\n" +
                                            "      \"name\": \"换型最少\",\n" +
                                            "      \"globalRuleId\": 3,\n" +
                                            "      \"globalRuleName\": \"换型最少\",\n" +
                                            "      \"forwardScheduling\": []\n" +
                                            "    },\n" +
                                            "    {\n" +
                                            "      \"id\": 4,\n" +
                                            "      \"source\": \"GLOBAL\",\n" +
                                            "      \"baseRuleId\": 4,\n" +
                                            "      \"name\": \"设备负载均衡\",\n" +
                                            "      \"globalRuleId\": 4,\n" +
                                            "      \"globalRuleName\": \"设备负载均衡\",\n" +
                                            "      \"forwardScheduling\": []\n" +
                                            "    }\n" +
                                            "  ],\n" +
                                            "  \"timestamp\": \"2026-04-30T10:00:00.000Z\"\n" +
                                            "}"
                            )
                    )
            )
    )
    public R<List<Map<String, Object>>> globalList(
            @Parameter(description = "当前用户ID")
            @RequestParam(value = "userId", required = false) Long userId) {
        return R.ok(userStrategyRuleService.getStrategyOptions(userId));
    }
}
