package com.kym.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.kym.common.R;
import com.kym.common.annotation.SysLog;
import com.kym.entity.RechargeConfig;
import com.kym.entity.RechargeConfigGroup;
import com.kym.service.RechargeConfigGroupService;
import com.kym.service.RechargeConfigService;
import com.kym.service.RechargeConfigStationGroupService;
import org.springframework.web.bind.annotation.*;

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

/**
 * 充值配置管理（分组 + 配置项两级管理，分组通过关联表绑定站点）
 *
 * @author skyline
 */
@RestController
@RequestMapping("/rechargeConfig")
public class RechargeConfigController {

    private final RechargeConfigService itemService;
    private final RechargeConfigGroupService groupService;
    private final RechargeConfigStationGroupService stationGroupService;

    public RechargeConfigController(RechargeConfigService itemService,
                                     RechargeConfigGroupService groupService,
                                     RechargeConfigStationGroupService stationGroupService) {
        this.itemService = itemService;
        this.groupService = groupService;
        this.stationGroupService = stationGroupService;
    }

    // ==================== 分组管理 ====================

    /**
     * 所有分组列表（含关联站点）
     */
    @SaCheckPermission("rechargeConfig.list")
    @GetMapping("/group/list")
    R<?> groupList() {
        var groups = groupService.list();
        var result = groups.stream().map(g -> {
            var stationIds = stationGroupService.listStationIdsByGroupId(g.getId());
            return Map.of("group", g, "stationIds", stationIds);
        }).toList();
        return R.success(result);
    }

    /**
     * 创建分组（可选关联站点列表）
     */
    @SaCheckPermission("rechargeConfig.add")
    @SysLog("创建充值配置分组")
    @PostMapping("/group/add")
    R<?> groupAdd(@RequestBody Map<String, Object> body) {
        var group = new RechargeConfigGroup();
        group.setName((String) body.getOrDefault("name", null));
        group.setIsDefault(Boolean.TRUE.equals(body.get("isDefault")));
        @SuppressWarnings("unchecked")
        var stationIds = (List<String>) body.get("stationIds");
        groupService.createGroup(group, stationIds);
        return R.success();
    }

    /**
     * 修改分组名称
     */
    @SaCheckPermission("rechargeConfig.modify")
    @SysLog("修改充值配置分组")
    @PostMapping("/group/modify")
    R<?> groupModify(@RequestBody RechargeConfigGroup group) {
        groupService.updateById(group);
        return R.success();
    }

    /**
     * 删除分组
     */
    @SaCheckPermission("rechargeConfig.remove")
    @SysLog("删除充值配置分组")
    @GetMapping("/group/remove/{id}")
    R<?> groupRemove(@PathVariable long id) {
        groupService.removeById(id);
        return R.success();
    }

    // ==================== 分组-站点关联管理 ====================

    /**
     * 查询某分组关联的站点列表
     */
    @GetMapping("/group/stationIds")
    R<?> groupStationIds(@RequestParam Long groupId) {
        return R.success(stationGroupService.listStationIdsByGroupId(groupId));
    }

    /**
     * 为分组分配一个站点
     */
    @SaCheckPermission("rechargeConfig.modify")
    @SysLog("分配站点到充值配置分组")
    @PostMapping("/group/assignStation")
    R<?> assignStation(@RequestBody Map<String, Object> body) {
        String stationId = (String) body.get("stationId");
        Long groupId = Long.valueOf(body.get("groupId").toString());
        stationGroupService.assignStationToGroup(stationId, groupId);
        return R.success();
    }

    /**
     * 移除站点与分组的关联
     */
    @SaCheckPermission("rechargeConfig.modify")
    @SysLog("移除站点充值配置分组关联")
    @PostMapping("/group/removeStation")
    R<?> removeStation(@RequestBody Map<String, Object> body) {
        String stationId = (String) body.get("stationId");
        Long groupId = Long.valueOf(body.get("groupId").toString());
        stationGroupService.removeStationFromGroup(stationId, groupId);
        return R.success();
    }

    // ==================== 配置项管理 ====================

    @GetMapping("/item/list")
    R<?> itemList(@RequestParam Long groupId) {
        return R.success(itemService.listByGroupId(groupId));
    }

    @SaCheckPermission("rechargeConfig.add")
    @SysLog("新增充值配置项")
    @PostMapping("/item/add")
    R<?> itemAdd(@RequestBody RechargeConfig config) {
        itemService.save(config);
        return R.success();
    }

    @SaCheckPermission("rechargeConfig.modify")
    @SysLog("修改充值配置项")
    @PostMapping("/item/modify")
    R<?> itemModify(@RequestBody RechargeConfig config) {
        itemService.updateById(config);
        return R.success();
    }

    @SaCheckPermission("rechargeConfig.remove")
    @SysLog("删除充值配置项")
    @GetMapping("/item/remove/{id}")
    R<?> itemRemove(@PathVariable long id) {
        itemService.removeById(id);
        return R.success();
    }

    // ==================== 兼容旧接口 ====================

    @SaCheckPermission("rechargeConfig.list")
    @GetMapping("/list")
    R<?> list(@RequestParam(required = false) String stationId) {
        return R.success(itemService.listByStationId(stationId));
    }

    @SaCheckPermission("rechargeConfig.list")
    @GetMapping("/listAll")
    R<?> listAll() {
        return R.success(itemService.list());
    }
}
