package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.PlatformShopMembershipPlan;
import shop.alien.entity.store.dto.PlatformShopMembershipPlanDeleteRequest;
import shop.alien.store.service.PlatformShopMembershipPlanPlatformService;

import javax.validation.Valid;

/**
 * 平台端 — 会员卡方案配置
 */
@Slf4j
@Api(tags = {"平台-店铺会员卡方案"})
@CrossOrigin
@RestController
@RequestMapping("/platformShopMembershipPlan")
@RequiredArgsConstructor
public class PlatformShopMembershipPlanPlatformController {

    private final PlatformShopMembershipPlanPlatformService planPlatformService;

    @ApiOperation("分页查询方案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "planName", value = "会员卡名称，模糊匹配，可选", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "enabled", value = "是否上架 0/1，空为全部", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageNum", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", paramType = "query", dataType = "int")
    })
    @GetMapping("/page")
    public R<IPage<PlatformShopMembershipPlan>> page(
            @RequestParam(required = false) String planName,
            @RequestParam(required = false) Integer enabled,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return R.data(planPlatformService.pagePlans(planName, enabled, pageNum, pageSize));
    }

    @ApiOperation("新增或修改方案")
    @PostMapping("/saveOrUpdate")
    public R<Boolean> saveOrUpdate(@RequestBody PlatformShopMembershipPlan plan) {
        try {
            return R.data(planPlatformService.saveOrUpdatePlan(plan));
        } catch (IllegalArgumentException e) {
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("上架/下架")
    @PostMapping("/toggleEnabled")
    public R<Boolean> toggleEnabled(@RequestParam Integer id, @RequestParam Integer enabled) {
        boolean ok = planPlatformService.toggleEnabled(id, enabled);
        return ok ? R.data(true) : R.fail("操作失败");
    }

    @ApiOperation("推荐/取消推荐")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "方案ID", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "recommended", value = "是否推荐 0否 1是", required = true, paramType = "query", dataType = "int")
    })
    @GetMapping("/toggleRecommended")
    public R<Boolean> toggleRecommended(@RequestParam Integer id, @RequestParam Integer recommended) {
        boolean ok = planPlatformService.toggleRecommended(id, recommended);
        return ok ? R.data(true) : R.fail("操作失败");
    }

    @ApiOperation("详情")
    @GetMapping("/detail")
    public R<PlatformShopMembershipPlan> detail(@RequestParam Integer id) {
        return R.data(planPlatformService.getById(id));
    }

    @ApiOperation("逻辑删除方案（按方案 id，单个或批量；仅标记 delete_flag，不物理删）")
    @PostMapping("/logicDelete")
    public R<Integer> logicDelete(@Valid @RequestBody PlatformShopMembershipPlanDeleteRequest body) {
        try {
            return R.data(planPlatformService.logicDeleteByIds(body.getIds()));
        } catch (IllegalArgumentException e) {
            return R.fail(e.getMessage());
        }
    }
}
