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.SportsEquipmentFacility;
import shop.alien.entity.store.vo.SportsEquipmentFacilityCategoryVo;
import shop.alien.entity.store.vo.SportsEquipmentFacilityVo;
import shop.alien.store.service.SportsEquipmentFacilityService;

import java.util.List;

/**
 * 运动器材设施Controller
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"运动器材设施管理"})
@ApiSort(1)
@CrossOrigin
@RestController
@RequestMapping("/sports/equipment/facility")
@RequiredArgsConstructor
public class SportsEquipmentFacilityController {

    private final SportsEquipmentFacilityService facilityService;

    @ApiOperation("分页查询运动器材设施列表")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int", paramType = "query", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页大小", dataType = "int", paramType = "query", required = true, defaultValue = "10"),
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "facilityCategory", value = "设施分类(1:有氧区, 2:力量区, 3:单功能机械区)", dataType = "int", paramType = "query")
    })
    @GetMapping("/page")
    public R<IPage<SportsEquipmentFacilityVo>> getPageList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam Integer storeId,
            @RequestParam(required = false) Integer facilityCategory) {
        log.info("SportsEquipmentFacilityController.getPageList?pageNum={},pageSize={},storeId={},facilityCategory={}",
                pageNum, pageSize, storeId, facilityCategory);
        return R.data(facilityService.getPageList(pageNum, pageSize, storeId, facilityCategory));
    }

    @ApiOperation("查询运动器材设施列表")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "facilityCategory", value = "设施分类(1:有氧区, 2:力量区, 3:单功能机械区)", dataType = "int", paramType = "query")
    })
    @GetMapping("/list")
    public R<List<SportsEquipmentFacilityVo>> getList(
            @RequestParam Integer storeId,
            @RequestParam(required = false) Integer facilityCategory) {
        log.info("SportsEquipmentFacilityController.getList?storeId={},facilityCategory={}", storeId, facilityCategory);
        return R.data(facilityService.getList(storeId, facilityCategory));
    }

    @ApiOperation("根据ID查询运动器材设施详情（用户端）")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/detail")
    public R<SportsEquipmentFacilityVo> getDetail(@RequestParam Integer id) {
        log.info("SportsEquipmentFacilityController.getDetail?id={}", id);
        SportsEquipmentFacilityVo vo = facilityService.getDetail(id);
        if (vo == null) {
            return R.fail("数据不存在");
        }
        return R.data(vo);
    }

    @ApiOperation("新增运动器材设施")
    @ApiOperationSupport(order = 4)
    @PostMapping("/save")
    public R<Boolean> saveFacility(@RequestBody SportsEquipmentFacilityVo vo) {
        log.info("SportsEquipmentFacilityController.saveFacility?vo={}", vo);
        try {
            SportsEquipmentFacility facility = new SportsEquipmentFacility();
            facility.setStoreId(vo.getStoreId());
            facility.setFacilityCategory(vo.getFacilityCategory());
            facility.setFacilityName(vo.getFacilityName());
            facility.setQuantity(vo.getQuantity());
            facility.setBrand(vo.getBrand());
            facility.setDescription(vo.getDescription());
            facility.setDisplayInStoreDetail(vo.getDisplayInStoreDetail());
            boolean result = facilityService.saveFacility(facility, vo.getImageList());
            if (result) {
                return R.success("新增成功");
            }
            return R.fail("新增失败");
        } catch (Exception e) {
            log.error("SportsEquipmentFacilityController.saveFacility异常", e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("修改运动器材设施")
    @ApiOperationSupport(order = 5)
    @PostMapping("/update")
    public R<Boolean> updateFacility(@RequestBody SportsEquipmentFacilityVo vo) {
        log.info("SportsEquipmentFacilityController.updateFacility?vo={}", vo);
        try {
            if (vo.getId() == null) {
                return R.fail("主键ID不能为空");
            }
            SportsEquipmentFacility facility = new SportsEquipmentFacility();
            facility.setId(vo.getId());
            facility.setStoreId(vo.getStoreId());
            facility.setFacilityCategory(vo.getFacilityCategory());
            facility.setFacilityName(vo.getFacilityName());
            facility.setQuantity(vo.getQuantity());
            facility.setBrand(vo.getBrand());
            facility.setDescription(vo.getDescription());
            facility.setDisplayInStoreDetail(vo.getDisplayInStoreDetail());
            boolean result = facilityService.updateFacility(facility, vo.getImageList());
            if (result) {
                return R.success("修改成功");
            }
            return R.fail("修改失败");
        } catch (Exception e) {
            log.error("SportsEquipmentFacilityController.updateFacility异常", e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("删除运动器材设施")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query", required = true)
    })
    @PostMapping("/delete")
    public R<Boolean> deleteFacility(@RequestParam Integer id) {
        log.info("SportsEquipmentFacilityController.deleteFacility?id={}", id);
        try {
            boolean result = facilityService.deleteFacility(id);
            if (result) {
                return R.success("删除成功");
            }
            return R.fail("删除失败");
        } catch (Exception e) {
            log.error("SportsEquipmentFacilityController.deleteFacility异常", e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("查询指定店铺按分类汇总的设备信息（包含设备数量、图片列表和设备列表）（用户端）")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/categorySummary")
    public R<List<SportsEquipmentFacilityCategoryVo>> getCategorySummary(@RequestParam Integer storeId) {
        log.info("SportsEquipmentFacilityController.getCategorySummary?storeId={}", storeId);
        return R.data(facilityService.getCategorySummary(storeId));
    }



    @ApiOperation("根据ID查询运动器材设施详情（商户端）")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/storeDetail")
    public R<SportsEquipmentFacilityVo> getStoreDetail(@RequestParam Integer id) {
        log.info("SportsEquipmentFacilityController.getStoreDetail?id={}", id);
        SportsEquipmentFacilityVo vo = facilityService.getStoreDetail(id);
        if (vo == null) {
            return R.fail("数据不存在");
        }
        return R.data(vo);
    }

    @ApiOperation("查询指定店铺按分类汇总的设备信息（包含设备数量、图片列表和设备列表）（商户端）")
    @ApiOperationSupport(order = 9)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/storeCategorySummary")
    public R<List<SportsEquipmentFacilityCategoryVo>> getstoreCategorySummary(@RequestParam Integer storeId) {
        log.info("SportsEquipmentFacilityController.getstoreCategorySummary?storeId={}", storeId);
        return R.data(facilityService.getstoreCategorySummary(storeId));
    }
}

