package com.zsElectric.boot.business.controller;

import com.zsElectric.boot.business.service.CouponService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zsElectric.boot.business.model.form.CouponForm;
import com.zsElectric.boot.business.model.query.CouponQuery;
import com.zsElectric.boot.business.model.vo.CouponVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zsElectric.boot.core.web.PageResult;
import com.zsElectric.boot.core.web.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 优惠劵前端控制层
 *
 * @author zsElectric
 * @since 2025-12-15 09:38
 */
@Tag(name = "优惠劵接口")
@RestController
@RequestMapping("/api/v1/coupon")
@RequiredArgsConstructor
public class CouponController  {

    private final CouponService couponService;

    @Operation(summary = "优惠劵分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('business:coupon:query')")
    public PageResult<CouponVO> getCouponPage(CouponQuery queryParams ) {
        IPage<CouponVO> result = couponService.getCouponPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增优惠劵")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('business:coupon:add')")
    public Result<Void> saveCoupon(@RequestBody @Valid CouponForm formData ) {
        boolean result = couponService.saveCoupon(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取优惠劵表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('business:coupon:edit')")
    public Result<CouponForm> getCouponForm(
        @Parameter(description = "优惠劵ID") @PathVariable Long id
    ) {
        CouponForm formData = couponService.getCouponFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改优惠劵")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('business:coupon:edit')")
    public Result<Void> updateCoupon(
            @Parameter(description = "优惠劵ID") @PathVariable Long id,
            @RequestBody @Validated CouponForm formData
    ) {
        boolean result = couponService.updateCoupon(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除优惠劵")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('business:coupon:delete')")
    public Result<Void> deleteCoupons(
        @Parameter(description = "优惠劵ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = couponService.deleteCoupons(ids);
        return Result.judge(result);
    }
}
