package shop.alien.dining.controller;

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.StoreCuisine;
import shop.alien.entity.store.StoreCuisineCategory;
import shop.alien.entity.store.StoreTable;
import shop.alien.entity.store.dto.StoreInfoWithHomepageCuisinesDTO;
import shop.alien.entity.store.vo.CategoryWithCuisinesVO;
import shop.alien.dining.service.StoreInfoService;

import java.util.List;

/**
 * 门店信息查询控制器
 *
 * @author system
 * @since 2025-02-02
 */
@Slf4j
@Api(tags = {"小程序-门店信息查询"})
@CrossOrigin
@RestController
@RequestMapping("/store/info")
@RequiredArgsConstructor
public class StoreInfoController {

    private final StoreInfoService storeInfoService;

    @ApiOperation(value = "根据门店ID查询桌号列表", notes = "查询指定门店下的所有桌号")
    @GetMapping("/tables")
    public R<List<StoreTable>> getTablesByStoreId(
            @ApiParam(value = "门店ID", required = true) @RequestParam Integer storeId) {
        log.info("StoreInfoController.getTablesByStoreId?storeId={}", storeId);
        try {
            if (storeId == null) {
                return R.fail("门店ID不能为空");
            }
            List<StoreTable> tables = storeInfoService.getTablesByStoreId(storeId);
            return R.data(tables);
        } catch (Exception e) {
            log.error("查询桌号列表失败: {}", e.getMessage(), e);
            return R.fail("查询桌号列表失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "根据门店ID查询菜品种类列表", notes = "查询指定门店下的所有菜品种类")
    @GetMapping("/categories")
    public R<List<StoreCuisineCategory>> getCategoriesByStoreId(
            @ApiParam(value = "门店ID", required = true) @RequestParam Integer storeId) {
        log.info("StoreInfoController.getCategoriesByStoreId?storeId={}", storeId);
        try {
            if (storeId == null) {
                return R.fail("门店ID不能为空");
            }
            List<StoreCuisineCategory> categories = storeInfoService.getCategoriesByStoreId(storeId);
            return R.data(categories);
        } catch (Exception e) {
            log.error("查询菜品种类列表失败: {}", e.getMessage(), e);
            return R.fail("查询菜品种类列表失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "根据门店ID查询菜品种类及各类别下菜品", notes = "一次返回所有菜品种类及每个分类下的菜品列表；可选 keyword 按菜品名称模糊查询。返回中 category 与 /store/info/categories 单条结构一致，cuisines 与 /store/info/cuisines 返回结构一致，保证字段完整一致。")
    @GetMapping("/categories-with-cuisines")
    public R<List<CategoryWithCuisinesVO>> getCategoriesWithCuisinesByStoreId(
            @ApiParam(value = "门店ID", required = true) @RequestParam Integer storeId,
            @ApiParam(value = "菜品名称模糊查询关键词（可选）") @RequestParam(required = false) String keyword) {
        log.info("StoreInfoController.getCategoriesWithCuisinesByStoreId?storeId={}, keyword={}", storeId, keyword);
        try {
            if (storeId == null) {
                return R.fail("门店ID不能为空");
            }
            List<CategoryWithCuisinesVO> list = storeInfoService.getCategoriesWithCuisinesByStoreId(storeId, keyword);
            return R.data(list);
        } catch (Exception e) {
            log.error("查询菜品种类及菜品失败: {}", e.getMessage(), e);
            return R.fail("查询菜品种类及菜品失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "删除菜品种类", notes = "仅删除绑定关系和菜品种类（逻辑删除）；价目表中的菜品记录不删除、其它字段不变，仅从 category_ids 中移除该分类ID")
    @DeleteMapping("/category/{categoryId}")
    public R<Boolean> deleteCategory(
            @ApiParam(value = "菜品种类ID", required = true) @PathVariable Integer categoryId) {
        log.info("StoreInfoController.deleteCategory?categoryId={}", categoryId);
        try {
            if (categoryId == null) {
                return R.fail("菜品种类ID不能为空");
            }
            boolean result = storeInfoService.deleteCategory(categoryId);
            return R.data(result);
        } catch (Exception e) {
            log.error("删除菜品种类失败: {}", e.getMessage(), e);
            return R.fail("删除菜品种类失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "根据菜品种类ID查询菜品信息列表", notes = "查询指定分类下的所有菜品信息")
    @GetMapping("/cuisines")
    public R<List<StoreCuisine>> getCuisinesByCategoryId(
            @ApiParam(value = "菜品种类ID", required = true) @RequestParam Integer categoryId) {
        log.info("StoreInfoController.getCuisinesByCategoryId?categoryId={}", categoryId);
        try {
            if (categoryId == null) {
                return R.fail("菜品种类ID不能为空");
            }
            List<StoreCuisine> cuisines = storeInfoService.getCuisinesByCategoryId(categoryId);
            return R.data(cuisines);
        } catch (Exception e) {
            log.error("查询菜品信息列表失败: {}", e.getMessage(), e);
            return R.fail("查询菜品信息列表失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "根据商铺ID查询店铺信息和首页展示美食价目表", notes = "查询店铺信息和当前店铺绑定的首页展示美食价目表信息")
    @GetMapping("/detail/{storeId}")
    public R<StoreInfoWithHomepageCuisinesDTO> getStoreInfoWithHomepageCuisines(
            @ApiParam(value = "商铺ID", required = true) @PathVariable Integer storeId) {
        log.info("StoreInfoController.getStoreInfoWithHomepageCuisines?storeId={}", storeId);
        try {
            if (storeId == null) {
                return R.fail("商铺ID不能为空");
            }
            StoreInfoWithHomepageCuisinesDTO result = storeInfoService.getStoreInfoWithHomepageCuisines(storeId);
            return R.data(result);
        } catch (Exception e) {
            log.error("查询店铺信息和首页展示美食价目表失败: {}", e.getMessage(), e);
            return R.fail("查询店铺信息和首页展示美食价目表失败: " + e.getMessage());
        }
    }
}
