package com.starry.admin.modules.withdraw.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.withdraw.entity.FreezePolicyEntity;
import com.starry.admin.modules.withdraw.service.IFreezePolicyService;
import com.starry.admin.modules.withdraw.vo.FreezePolicyVo;
import com.starry.admin.utils.SecurityUtils;
import com.starry.common.result.TypedR;
import com.starry.common.utils.IdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

@Api(tags = "提现冻结策略管理")
@RestController
@RequestMapping("/admin/withdraw/freeze-policy")
public class AdminFreezePolicyController {

    @Resource
    private IFreezePolicyService freezePolicyService;

    @Data
    public static class UpsertRequest {
        private Integer freezeHours;
    }

    @ApiOperation("获取租户默认冻结策略")
    @GetMapping("/default")
    public TypedR<FreezePolicyVo> getTenantDefault() {
        String tenantId = SecurityUtils.getTenantId();
        FreezePolicyEntity p = freezePolicyService.getOne(new LambdaQueryWrapper<FreezePolicyEntity>()
                .eq(FreezePolicyEntity::getTenantId, tenantId)
                .isNull(FreezePolicyEntity::getClerkId));
        FreezePolicyVo vo = new FreezePolicyVo(p == null ? null : p.getFreezeHours());
        return TypedR.ok(vo);
    }

    @ApiOperation("设置租户默认冻结策略")
    @PutMapping("/default")
    public TypedR<Void> upsertTenantDefault(@RequestBody UpsertRequest body) {
        if (body.getFreezeHours() == null || body.getFreezeHours() < 0) {
            throw new CustomException("冻结时长必须为非负数");
        }
        String tenantId = SecurityUtils.getTenantId();
        FreezePolicyEntity existing = freezePolicyService.getOne(new LambdaQueryWrapper<FreezePolicyEntity>()
                .eq(FreezePolicyEntity::getTenantId, tenantId)
                .isNull(FreezePolicyEntity::getClerkId));
        if (existing == null) {
            FreezePolicyEntity e = new FreezePolicyEntity();
            e.setId(IdUtils.getUuid());
            e.setTenantId(tenantId);
            e.setClerkId(null);
            e.setFreezeHours(body.getFreezeHours());
            freezePolicyService.save(e);
        } else {
            existing.setFreezeHours(body.getFreezeHours());
            freezePolicyService.updateById(existing);
        }
        return TypedR.ok(null);
    }

    @ApiOperation("获取店员冻结策略（覆盖）")
    @GetMapping("/clerk")
    public TypedR<FreezePolicyVo> getClerkPolicy(@RequestParam("clerkId") String clerkId) {
        String tenantId = SecurityUtils.getTenantId();
        FreezePolicyEntity p = freezePolicyService.getOne(new LambdaQueryWrapper<FreezePolicyEntity>()
                .eq(FreezePolicyEntity::getTenantId, tenantId)
                .eq(FreezePolicyEntity::getClerkId, clerkId));
        FreezePolicyVo vo = new FreezePolicyVo(p == null ? null : p.getFreezeHours());
        return TypedR.ok(vo);
    }

    @ApiOperation("设置店员冻结策略（覆盖）")
    @PutMapping("/clerk")
    public TypedR<Void> upsertClerkPolicy(@RequestParam("clerkId") String clerkId, @RequestBody UpsertRequest body) {
        if (body.getFreezeHours() == null || body.getFreezeHours() < 0) {
            throw new CustomException("冻结时长必须为非负数");
        }
        String tenantId = SecurityUtils.getTenantId();
        FreezePolicyEntity existing = freezePolicyService.getOne(new LambdaQueryWrapper<FreezePolicyEntity>()
                .eq(FreezePolicyEntity::getTenantId, tenantId)
                .eq(FreezePolicyEntity::getClerkId, clerkId));
        if (existing == null) {
            FreezePolicyEntity e = new FreezePolicyEntity();
            e.setId(IdUtils.getUuid());
            e.setTenantId(tenantId);
            e.setClerkId(clerkId);
            e.setFreezeHours(body.getFreezeHours());
            freezePolicyService.save(e);
        } else {
            existing.setFreezeHours(body.getFreezeHours());
            freezePolicyService.updateById(existing);
        }
        return TypedR.ok(null);
    }

    @ApiOperation("删除店员冻结策略（恢复为租户默认）")
    @DeleteMapping("/clerk")
    public TypedR<Boolean> deleteClerkPolicy(@RequestParam("clerkId") String clerkId) {
        String tenantId = SecurityUtils.getTenantId();
        boolean removed = freezePolicyService.remove(new LambdaQueryWrapper<FreezePolicyEntity>()
                .eq(FreezePolicyEntity::getTenantId, tenantId)
                .eq(FreezePolicyEntity::getClerkId, clerkId));
        return TypedR.ok(removed);
    }
}
