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.DrinkInfo;
import shop.alien.entity.store.vo.DrinkInfoVo;
import shop.alien.store.service.DrinkInfoService;

import java.util.List;

/**
 * 酒水餐食信息 Controller
 * 基于 store_menu 表实现（dish_menu_type: 1=餐食，2=酒水）
 *
 * @author ssk
 * @since 2025-06-13
 */
@Slf4j
@Api(tags = {"酒水餐食管理"})
@ApiSort(1)
@CrossOrigin
@RestController
@RequestMapping("/store/drink")
@RequiredArgsConstructor
public class DrinkInfoController {

    private final DrinkInfoService drinkInfoService;

    @ApiOperation("分页查询酒水餐食列表")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "页容", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型（1:餐食，2:酒水）", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "category", value = "分类", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称关键词", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createUserId", value = "创建人ID（只查询自己创建的）", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/page")
    public R<IPage<DrinkInfoVo>> getDrinkInfoPage(
            @RequestParam("page") int page,
            @RequestParam("size") int size,
            @RequestParam(value = "storeId", required = false) Integer storeId,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "category", required = false) String category,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "createUserId", required = false) Integer createUserId) {
        log.info("DrinkInfoController.getDrinkInfoPage?page={}, size={}, storeId={}, type={}, category={}, name={}, createUserId={}", 
                page, size, storeId, type, category, name, createUserId);
        IPage<DrinkInfoVo> drinkInfoPage = drinkInfoService.getDrinkInfoPage(page, size, storeId, type, category, name, createUserId);
        return R.data(drinkInfoPage);
    }

    @ApiOperation("根据ID查询酒水餐食详情")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Integer", paramType = "path", required = true)
    })
    @GetMapping("/{id}")
    public R<DrinkInfoVo> getDrinkInfoById(@PathVariable("id") Integer id) {
        log.info("DrinkInfoController.getDrinkInfoById?id={}", id);
        DrinkInfoVo drinkInfoVo = drinkInfoService.getDrinkInfoById(id);
        if (drinkInfoVo == null) {
            return R.fail("未找到该酒水餐食信息");
        }
        return R.data(drinkInfoVo);
    }

    @ApiOperation("新增酒水餐食")
    @ApiOperationSupport(order = 3)
    @PostMapping
    public R<String> saveDrinkInfo(@RequestBody DrinkInfo drinkInfo) {
        log.info("DrinkInfoController.saveDrinkInfo?drinkInfo={}", drinkInfo);
        boolean result = drinkInfoService.saveDrinkInfo(drinkInfo);
        if (result) {
            return R.success("新增成功");
        }
        return R.fail("新增失败");
    }

    @ApiOperation("修改酒水餐食")
    @ApiOperationSupport(order = 4)
    @PutMapping
    public R<String> updateDrinkInfo(@RequestBody DrinkInfo drinkInfo) {
        log.info("DrinkInfoController.updateDrinkInfo?drinkInfo={}", drinkInfo);
        boolean result = drinkInfoService.updateDrinkInfo(drinkInfo);
        if (result) {
            return R.success("修改成功");
        }
        return R.fail("修改失败");
    }

    @ApiOperation("删除酒水餐食")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Integer", paramType = "path", required = true)
    })
    @DeleteMapping("/{id}")
    public R<String> deleteDrinkInfo(@PathVariable("id") Integer id) {
        log.info("DrinkInfoController.deleteDrinkInfo?id={}", id);
        boolean result = drinkInfoService.deleteDrinkInfo(id);
        if (result) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }

    @ApiOperation("设置酒水餐食推荐")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Integer", paramType = "path", required = true),
            @ApiImplicitParam(name = "isRecommended", value = "是否推荐（0:否, 1:是）", dataType = "Integer", paramType = "query", required = true)
    })
    @PutMapping("/{id}/recommended")
    public R<String> updateIsRecommended(
            @PathVariable("id") Integer id,
            @RequestParam("isRecommended") Integer isRecommended) {
        log.info("DrinkInfoController.updateIsRecommended?id={}, isRecommended={}", id, isRecommended);
        boolean result = drinkInfoService.updateIsRecommended(id, isRecommended);
        if (result) {
            return R.success("操作成功");
        }
        return R.fail("操作失败");
    }

    @ApiOperation("更新酒水餐食排序")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Integer", paramType = "path", required = true),
            @ApiImplicitParam(name = "sort", value = "排序值", dataType = "Integer", paramType = "query", required = true)
    })
    @PutMapping("/{id}/sort")
    public R<String> updateSort(
            @PathVariable("id") Integer id,
            @RequestParam("sort") Integer sort) {
        log.info("DrinkInfoController.updateSort?id={}, sort={}", id, sort);
        boolean result = drinkInfoService.updateSort(id, sort);
        if (result) {
            return R.success("操作成功");
        }
        return R.fail("操作失败");
    }

    @ApiOperation("根据门店ID查询所有酒水餐食")
    @ApiOperationSupport(order = 9)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "类型（枚举值：酒水/餐食）", dataType = "String", paramType = "query")
    })
    @GetMapping("/listByStoreId")
    public R<List<DrinkInfoVo>> getDrinkInfoListByStoreId(
            @RequestParam("storeId") Integer storeId,
            @RequestParam(value = "type", required = false) Integer type) {
        log.info("DrinkInfoController.getDrinkInfoListByStoreId?storeId={}, type={}", storeId, type);
        List<DrinkInfoVo> drinkInfoList = drinkInfoService.getDrinkInfoListByStoreId(storeId, type);
        return R.data(drinkInfoList);
    }

    @ApiOperation("查询推荐酒水餐食列表")
    @ApiOperationSupport(order = 10)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "createUserId", value = "创建人ID（只查询自己创建的）", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/recommended")
    public R<List<DrinkInfoVo>> getRecommendedList(
            @RequestParam(value = "storeId", required = false) Integer storeId,
            @RequestParam(value = "createUserId", required = false) Integer createUserId) {
        log.info("DrinkInfoController.getRecommendedList?storeId={}, createUserId={}", storeId, createUserId);
        List<DrinkInfoVo> drinkInfoList = drinkInfoService.getRecommendedList(storeId, createUserId);
        return R.data(drinkInfoList);
    }

    @ApiOperation("批量更新酒水餐食排序")
    @ApiOperationSupport(order = 11)
    @PostMapping("/batchSort")
    public R<String> batchUpdateSort(@RequestBody List<DrinkInfo> drinkInfoList) {
        log.info("DrinkInfoController.batchUpdateSort?size={}", drinkInfoList.size());
        boolean result = drinkInfoService.batchUpdateSort(drinkInfoList);
        if (result) {
            return R.success("批量更新排序成功");
        }
        return R.fail("批量更新排序失败");
    }

}
