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.common.controller.IController;
import com.kym.entity.admin.StationWhitelist;
import com.kym.entity.admin.StationWhitelistConfig;
import com.kym.entity.admin.dto.BatchAddWhitelistDto;
import com.kym.entity.admin.queryParams.StationWhitelistQueryParam;
import com.kym.entity.admin.vo.StationWhitelistConfigVo;
import com.kym.service.admin.StationWhitelistConfigService;
import com.kym.service.admin.StationWhitelistService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/station-whitelist")
public class StationWhitelistController extends IController {

    private final StationWhitelistService stationWhitelistService;
    private final StationWhitelistConfigService stationWhitelistConfigService;

    public StationWhitelistController(StationWhitelistService stationWhitelistService,
                                       StationWhitelistConfigService stationWhitelistConfigService) {
        this.stationWhitelistService = stationWhitelistService;
        this.stationWhitelistConfigService = stationWhitelistConfigService;
    }

    @GetMapping("/list")
    @SaCheckPermission("station.whitelist.list")
    @SysLog("站点白名单-查询列表")
    public R<?> list(@ModelAttribute StationWhitelistQueryParam param) {
        return R.success(stationWhitelistService.listWhitelist(param));
    }

    @PostMapping("/add")
    @SaCheckPermission("station.whitelist.add")
    @SysLog(value = "站点白名单-添加用户", ignoreParams = true)
    public R<?> add(@RequestBody StationWhitelist whitelist) {
        stationWhitelistService.addWhitelist(whitelist);
        return R.success();
    }

    @PostMapping("/batchAdd")
    @SaCheckPermission("station.whitelist.add")
    @SysLog(value = "站点白名单-批量添加用户", ignoreParams = true)
    public R<?> batchAdd(@RequestBody BatchAddWhitelistDto dto) {
        stationWhitelistService.batchAddWhitelist(dto);
        return R.success();
    }

    @DeleteMapping("/remove/{id}")
    @SaCheckPermission("station.whitelist.delete")
    @SysLog("站点白名单-移除用户")
    public R<?> remove(@PathVariable Long id) {
        stationWhitelistService.removeWhitelist(id);
        return R.success();
    }

    @DeleteMapping("/batchRemove")
    @SaCheckPermission("station.whitelist.delete")
    @SysLog("站点白名单-批量移除用户")
    public R<?> batchRemove(@RequestBody List<Long> ids) {
        stationWhitelistService.batchRemoveWhitelist(ids);
        return R.success();
    }

    @PutMapping("/status/{id}")
    @SaCheckPermission("station.whitelist.modify")
    @SysLog("站点白名单-更新状态")
    public R<?> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        stationWhitelistService.updateStatus(id, status);
        return R.success();
    }

    @GetMapping("/config/list")
    @SaCheckPermission("station.whitelist.config.list")
    @SysLog("站点白名单配置-查询列表")
    public R<?> listConfig() {
        return R.success(stationWhitelistConfigService.listConfig());
    }

    @GetMapping("/config/station/{stationId}")
    @SaCheckPermission("station.whitelist.config.list")
    @SysLog("站点白名单配置-查询站点配置")
    public R<?> getConfigByStationId(@PathVariable String stationId) {
        StationWhitelistConfig config = stationWhitelistConfigService.getConfigByStationId(stationId);
        return R.success(config);
    }

    @PostMapping("/config/update")
    @SaCheckPermission("station.whitelist.config.modify")
    @SysLog("站点白名单配置-更新配置")
    public R<?> updateConfig(@RequestBody StationWhitelistConfig config) {
        stationWhitelistConfigService.updateConfig(config.getStationId(), config.getEnabled());
        return R.success();
    }

    @DeleteMapping("/config/delete/{stationId}")
    @SaCheckPermission("station.whitelist.config.delete")
    @SysLog("站点白名单配置-删除配置")
    public R<?> deleteConfig(@PathVariable String stationId) {
        stationWhitelistConfigService.deleteConfig(stationId);
        return R.success();
    }

    @PostMapping("/refreshCache/{stationId}")
    @SaCheckPermission("station.whitelist.modify")
    @SysLog("站点白名单-刷新缓存")
    public R<?> refreshCache(@PathVariable String stationId) {
        stationWhitelistService.refreshCache(stationId);
        return R.success();
    }
}
