package shop.alien.store.controller;

import io.swagger.annotations.*;
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.SportsFacilityArea;
import shop.alien.entity.store.vo.BatchDeleteAreaRequestVo;
import shop.alien.entity.store.vo.CreateAreaRequestVo;
import shop.alien.entity.store.vo.DeleteAreaRequestVo;
import shop.alien.entity.store.vo.UpdateAreaHeadUrlRequestVo;
import shop.alien.entity.store.vo.UpdateAreaRequestVo;
import shop.alien.store.service.SportsFacilityAreaService;

import java.util.List;

/**
 * 运动设施区域Controller
 *
 * @author assistant
 * @since 2025-12-19
 */
@Slf4j
@Api(tags = {"商户端-运动设施区域管理"})
@ApiSort(1)
@CrossOrigin
@RestController
@RequestMapping("/sports/facility/area")
@RequiredArgsConstructor
public class SportsFacilityAreaController {

    private final SportsFacilityAreaService areaService;

    /**
     * 最小有效值
     */
    private static final int MIN_VALID_VALUE = 1;

    /**
     * 门店ID无效消息
     */
    private static final String INVALID_STORE_ID_MSG = "门店ID不能为空且必须大于0";

    @ApiOperation("新建区域")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "新建区域请求参数", dataType = "CreateAreaRequestVo", paramType = "body", required = true)
    })
    @PostMapping("/create")
    public R<Integer> createArea(@RequestBody CreateAreaRequestVo request) {
        log.info("新建区域，request={}", request);
        try {
            // 参数验证
            if (request == null) {
                log.warn("新建区域失败，请求参数为空");
                return R.fail("请求参数不能为空");
            }

            if (request.getStoreId() == null || request.getStoreId() < MIN_VALID_VALUE) {
                log.warn("新建区域失败，门店ID无效：{}", request.getStoreId());
                return R.fail(INVALID_STORE_ID_MSG);
            }

            if (StringUtils.isBlank(request.getFacilityCategoryName())) {
                log.warn("新建区域失败，区域名称为空");
                return R.fail("区域名称不能为空");
            }

            // 调用Service创建区域
            Integer areaId = areaService.createArea(request.getStoreId(), request.getFacilityCategoryName());
            log.info("新建区域成功，storeId={}，areaName={}，区域ID={}", 
                    request.getStoreId(), request.getFacilityCategoryName(), areaId);
            return R.data(areaId, "新建区域成功");
        } catch (IllegalArgumentException e) {
            log.warn("新建区域失败，参数验证失败：{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("新建区域异常，request={}，异常信息：{}", request, e.getMessage(), e);
            return R.fail("新建区域失败：" + e.getMessage());
        }
    }

    @ApiOperation("查询指定门店下的所有区域列表")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/list")
    public R<List<SportsFacilityArea>> listAreas(@RequestParam("storeId") Integer storeId) {
        log.info("查询区域列表，storeId={}", storeId);
        try {
            // 参数验证
            if (storeId == null || storeId < MIN_VALID_VALUE) {
                log.warn("查询区域列表失败，门店ID无效：{}", storeId);
                return R.fail(INVALID_STORE_ID_MSG);
            }

            List<SportsFacilityArea> areaList = areaService.listAreas(storeId);
            log.info("查询区域列表成功，storeId={}，区域数量：{}", storeId, areaList.size());
            return R.data(areaList);
        } catch (IllegalArgumentException e) {
            log.warn("查询区域列表失败，参数验证失败：{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("查询区域列表异常，storeId={}，异常信息：{}", storeId, e.getMessage(), e);
            return R.fail("查询区域列表失败：" + e.getMessage());
        }
    }

    @ApiOperation("更新区域信息")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "更新区域请求参数", dataType = "UpdateAreaRequestVo", paramType = "body", required = true)
    })
    @PostMapping("/update")
    public R<Boolean> updateArea(@RequestBody UpdateAreaRequestVo request) {
        log.info("更新区域，request={}", request);
        try {
            // 参数验证
            if (request == null) {
                log.warn("更新区域失败，请求参数为空");
                return R.fail("请求参数不能为空");
            }

            if (request.getAreaId() == null || request.getAreaId() < MIN_VALID_VALUE) {
                log.warn("更新区域失败，区域ID无效：{}", request.getAreaId());
                return R.fail("区域ID不能为空且必须大于0");
            }

            boolean result = areaService.updateArea(request.getAreaId(), request.getAreaName(), request.getSortOrder());
            if (result) {
                log.info("更新区域成功，areaId={}", request.getAreaId());
                return R.success("更新区域成功");
            }
            return R.fail("更新区域失败");
        } catch (IllegalArgumentException e) {
            log.warn("更新区域失败，参数验证失败：{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("更新区域异常，request={}，异常信息：{}", request, e.getMessage(), e);
            return R.fail("更新区域失败：" + e.getMessage());
        }
    }

    @ApiOperation("更新区域头图")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "更新区域头图请求参数", dataType = "UpdateAreaHeadUrlRequestVo", paramType = "body", required = true)
    })
    @PostMapping("/updateHeadUrl")
    public R<Boolean> updateAreaHeadUrl(@RequestBody UpdateAreaHeadUrlRequestVo request) {
        log.info("更新区域头图，request={}", request);
        try {
            // 参数验证
            if (request == null) {
                log.warn("更新区域头图失败，请求参数为空");
                return R.fail("请求参数不能为空");
            }

            if (request.getAreaId() == null || request.getAreaId() < MIN_VALID_VALUE) {
                log.warn("更新区域头图失败，区域ID无效：{}", request.getAreaId());
                return R.fail("区域ID不能为空且必须大于0");
            }

            if (request.getAreaHeadUrl() == null) {
                log.warn("更新区域头图失败，区域头图URL为空");
                return R.fail("区域头图URL不能为空");
            }

            boolean result = areaService.updateAreaHeadUrl(request.getAreaId(), request.getAreaHeadUrl());
            if (result) {
                log.info("更新区域头图成功，areaId={}", request.getAreaId());
                return R.success("更新区域头图成功");
            }
            return R.fail("更新区域头图失败");
        } catch (IllegalArgumentException e) {
            log.warn("更新区域头图失败，参数验证失败：{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("更新区域头图异常，request={}，异常信息：{}", request, e.getMessage(), e);
            return R.fail("更新区域头图失败：" + e.getMessage());
        }
    }

    @ApiOperation("删除区域")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "删除区域请求参数", dataType = "DeleteAreaRequestVo", paramType = "body", required = true)
    })
    @PostMapping("/delete")
    public R<Boolean> deleteArea(@RequestBody DeleteAreaRequestVo request) {
        log.info("删除区域，request={}", request);
        try {
            // 参数验证
            if (request == null) {
                log.warn("删除区域失败，请求参数为空");
                return R.fail("请求参数不能为空");
            }

            if (request.getAreaId() == null || request.getAreaId() < MIN_VALID_VALUE) {
                log.warn("删除区域失败，区域ID无效：{}", request.getAreaId());
                return R.fail("区域ID不能为空且必须大于0");
            }

            boolean result = areaService.deleteArea(request.getAreaId());
            if (result) {
                log.info("删除区域成功，areaId={}", request.getAreaId());
                return R.success("删除区域成功");
            }
            return R.fail("删除区域失败");
        } catch (IllegalArgumentException e) {
            log.warn("删除区域失败，参数验证失败：{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("删除区域异常，request={}，异常信息：{}", request, e.getMessage(), e);
            return R.fail("删除区域失败：" + e.getMessage());
        }
    }

    @ApiOperation("批量删除区域")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "批量删除区域请求参数", dataType = "BatchDeleteAreaRequestVo", paramType = "body", required = true)
    })
    @PostMapping("/batchDelete")
    public R<Boolean> batchDeleteAreas(@RequestBody BatchDeleteAreaRequestVo request) {
        log.info("批量删除区域，request={}", request);
        try {
            // 参数验证
            if (request == null) {
                log.warn("批量删除区域失败，请求参数为空");
                return R.fail("请求参数不能为空");
            }

            if (request.getStoreId() == null || request.getStoreId() < MIN_VALID_VALUE) {
                log.warn("批量删除区域失败，门店ID无效：{}", request.getStoreId());
                return R.fail(INVALID_STORE_ID_MSG);
            }

            if (request.getAreaIds() == null || request.getAreaIds().isEmpty()) {
                log.warn("批量删除区域失败，区域ID列表为空");
                return R.fail("区域ID列表不能为空");
            }

            boolean result = areaService.batchDeleteAreas(request.getStoreId(), request.getAreaIds());
            if (result) {
                log.info("批量删除区域成功，storeId={}，区域数量：{}", 
                        request.getStoreId(), request.getAreaIds().size());
                return R.success("批量删除区域成功");
            }
            return R.fail("批量删除区域失败");
        } catch (IllegalArgumentException e) {
            log.warn("批量删除区域失败，参数验证失败：{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("批量删除区域异常，request={}，异常信息：{}", 
                    request, e.getMessage(), e);
            return R.fail("批量删除区域失败：" + e.getMessage());
        }
    }
}

