package shop.alien.storeplatform.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.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeGroupBuyThali;
import shop.alien.entity.store.dto.LifeGroupBuyDto;
import shop.alien.entity.store.vo.LifeGroupBuyThaliVo;
import shop.alien.entity.storePlatform.vo.LifeGroupBuyPlatformDto;
import shop.alien.storeplatform.service.LifeGroupBuyPlatformService;
import shop.alien.util.string.StringUtil;

/**
 * 团购管理控制器（平台端）
 * 提供团购相关的管理接口，包括团购的创建、查询、修改、删除等功能
 * 支持草稿保存、状态管理、库存管理等操作
 *
 * @author alien-cloud
 * @date 2025-11-18
 */
@Api(tags = {"商家端-团购"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/PcGroupBuy")
@RequiredArgsConstructor
public class LifeGroupBuyPlatformController {

    /**
     * 团购服务层
     */
    private final LifeGroupBuyPlatformService lifeGroupBuyService;

    /**
     * 保存套餐团购草稿
     * 将团购信息保存为草稿状态，可后续编辑提交
     *
     * @param lifeGroupBuyDto 套餐团购数据传输对象
     * @return R 操作结果
     * @author alien-cloud
     * @date 2025-11-18
     */
    @ApiOperation("保存套餐团购草稿")
    @PostMapping("/saveDraft")
    public R saveDraft(@RequestBody LifeGroupBuyPlatformDto lifeGroupBuyDto) {
        log.info("LifeGroupBuyController.saveThali?lifeGroupBuyDto={}", lifeGroupBuyDto.toString());
        if (lifeGroupBuyService.saveDraft(lifeGroupBuyDto)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }

    /**
     * 保存套餐团购并返回套餐信息
     * 保存套餐团购数据，并返回保存后的完整套餐信息
     *
     * @param lifeGroupBuyDto 套餐团购数据传输对象
     * @return R<LifeGroupBuyThali> 保存后的套餐详情
     * @author alien-cloud
     * @date 2025-11-18
     */
    @ApiOperation("保存套餐团购（返回保存套餐）")
    @PostMapping("/saveThali")
    public R<LifeGroupBuyThali> saveThali(@RequestBody LifeGroupBuyPlatformDto lifeGroupBuyDto) {
        log.info("LifeGroupBuyController.saveThaliNew?lifeGroupBuyDto={}", lifeGroupBuyDto.toString());
        LifeGroupBuyThali lifeGroupBuyThali = lifeGroupBuyService.saveThali(lifeGroupBuyDto);
        return R.data(lifeGroupBuyThali);
    }

    /**
     * 分页查询套餐团购列表
     * 支持多条件筛选查询团购列表，包括状态、名称、类型等条件
     *
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param storeId 门店ID
     * @param status 状态(0草稿/1待审核/2未开始/3审核拒绝/4已售罄/5进行中/6已下架/7已结束/8=2+手动下架)
     * @param groupName 团购名称
     * @param groupType 团购类型
     * @param groupNo 团购编号
     * @param reviewType 审核状态
     * @return R<IPage<LifeGroupBuyThaliVo>> 套餐团购分页信息
     * @author alien-cloud
     * @date 2025-11-18
     */
    @ApiOperation("套餐团购列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页容", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "状态(0草稿/1待审核/2未开始/3审核拒绝/4已售罄/5进行中/6已下架/7已结束/8=2+手动下架)", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "groupName", value = "名称", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "groupType", value = "团购类型", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "reviewType", value = "审核状态", dataType = "String", paramType = "query", required = false)
    })
    @GetMapping("/getThaliList")
    private R<IPage<LifeGroupBuyThaliVo>> getThaliList(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                       @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                       @RequestParam(value = "storeId") String storeId,
                                                       @RequestParam(value = "status", required = false) String status,
                                                       @RequestParam(value = "groupName", required = false) String groupName,
                                                       @RequestParam(value = "groupType", required = false) String groupType,
                                                       @RequestParam(value = "groupNo", required = false) String groupNo,
                                                       @RequestParam(value = "reviewType", required = false) String reviewType) {
        log.info("LifeGroupBuyController.getThaliList?page={},size={},storeId={},status={},groupName={},groupType={},reviewType={},groupNo={}", pageNum, pageSize, storeId, status, groupName, groupType, reviewType, groupNo);

        if (StringUtils.isBlank(storeId)) {
            return R.data(null ,"当前用户未入驻");
        }

        return R.data(lifeGroupBuyService.getThaliList(pageNum, pageSize, storeId, status, groupName, groupType, reviewType, groupNo));
    }

    /**
     * 根据编号查询套餐团购详情
     * 根据团购ID查询团购的详细信息
     *
     * @param id 团购ID
     * @param userId 用户ID
     * @return R 团购详情信息
     * @author alien-cloud
     * @date 2025-11-18
     */
    @ApiOperation("id查询套餐团购")
    @GetMapping("/getThaliById")
    private R getThaliById(@RequestParam("id") String id, String userId) {
        log.info("LifeGroupBuyController.getThaliById?id={}&userId={}", id, userId);
        return R.data(lifeGroupBuyService.getThaliById(id, userId));
    }

    /**
     * 更新团购状态
     * 修改团购的状态，包括审核、上架、下架等操作
     *
     * @param id 团购ID
     * @param status 状态值
     * @param approvalComments 审核意见
     * @return R 操作结果
     * @author alien-cloud
     * @date 2025-11-18
     */
    @ApiOperation("修改团购状态")
    @GetMapping("/updateStatus")
    private R updateStatus(@RequestParam("id") String id, @RequestParam("status") String status, @RequestParam("approvalComments") String approvalComments) {
        log.info("LifeGroupBuyController.updateStatus?id={},status={},approvalComments={}", id, status, approvalComments);
        if (lifeGroupBuyService.updateStatus(id, status, approvalComments)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }

    /**
     * 修改团购库存
     * 更新团购的库存数量
     *
     * @param id 团购ID
     * @param num 库存数量
     * @return R 操作结果
     * @author alien-cloud
     * @date 2025-11-18
     */
    @ApiOperation("修改团购库存")
    @GetMapping("/updateNum")
    private R updateNum(@RequestParam("id") String id, @RequestParam("num") String num) {
        log.info("LifeGroupBuyController.updateNum?id={},num={}", id, num);
        if (lifeGroupBuyService.updateNum(id, num)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }

    /**
     * 删除套餐团购
     * 根据团购ID和类型删除团购信息
     *
     * @param id 团购ID
     * @param groupType 团购类型(1美食)
     * @return R 操作结果
     * @author alien-cloud
     * @date 2025-11-18
     */
    @ApiOperation("删除套餐团购")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "groupType", value = "团购类型(1美食)", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/delThaliById")
    private R delThaliById(@RequestParam("id") String id, @RequestParam("groupType") String groupType) {
        log.info("LifeGroupBuyController.delThaliById?id={},groupType={}", id, groupType);
        if (lifeGroupBuyService.delThaliById(id, groupType)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }
}
