package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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.PlatformShopMembershipOrderCreateRequest;
import shop.alien.entity.store.vo.PlatformShopMembershipOrderCreateResultVo;
import shop.alien.entity.store.vo.PlatformShopMembershipOrderItemVo;
import shop.alien.entity.store.vo.PlatformShopMembershipRemainingDetailVo;
import shop.alien.entity.store.vo.PlatformShopMembershipStatusVo;
import shop.alien.store.service.PlatformShopMembershipOrderService;
import shop.alien.store.service.PlatformShopMembershipPlanService;
import shop.alien.store.service.PlatformShopMembershipService;

import javax.validation.Valid;
import java.util.List;

/**
 * 店铺端 — 平台会员卡（开通/续费、状态、购买记录）
 */
@Slf4j
@Api(tags = {"店铺-平台会员卡"})
@RestController
@RequestMapping("/platform-shop-membership")
@RequiredArgsConstructor
public class PlatformShopMembershipController {

    private final PlatformShopMembershipPlanService planService;
    private final PlatformShopMembershipService membershipService;
    private final PlatformShopMembershipOrderService orderService;

    @ApiOperation("可购买的会员卡方案列表")
    @GetMapping("/plans")
    public R<List<PlatformShopMembershipPlan>> listPlans() {
        return R.data(planService.listEnabledForStore());
    }

    @ApiOperation("当前门店会员状态（含剩余天数）")
    @ApiImplicitParam(name = "storeId", value = "门店ID", required = true, paramType = "query", dataType = "int")
    @GetMapping("/status")
    public R<PlatformShopMembershipStatusVo> status(@RequestParam Integer storeId) {
        return R.data(membershipService.buildStatusVo(storeId));
    }

    @ApiOperation("会员卡剩余天数、当前连续购买周期起止（yyyy/MM/dd）及周期总天数")
    @ApiImplicitParam(name = "storeId", value = "门店ID", required = true, paramType = "query", dataType = "int")
    @GetMapping("/remaining-detail")
    public R<PlatformShopMembershipRemainingDetailVo> remainingDetail(@RequestParam Integer storeId) {
        return R.data(membershipService.buildRemainingDetailVo(storeId));
    }

    @ApiOperation("购买记录分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", dataType = "int")
    })
    @GetMapping("/history")
    public R<IPage<PlatformShopMembershipOrderItemVo>> history(
            @RequestParam Integer storeId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return R.data(orderService.pageHistory(storeId, pageNum, pageSize));
    }

    @ApiOperation("创建待支付订单（随后调用 /platform-shop-membership/payment/prePay）")
    @PostMapping("/order/create")
    public R<PlatformShopMembershipOrderCreateResultVo> createOrder(@Valid @RequestBody PlatformShopMembershipOrderCreateRequest request) {
        try {
            return R.data(orderService.createPendingOrder(request));
        } catch (IllegalArgumentException e) {
            log.warn("创建会员卡订单失败: {}", e.getMessage());
            return R.fail(e.getMessage());
        }
    }
}
