package shop.alien.store.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.StoreOfficialAlbum;
import shop.alien.entity.store.vo.StoreOfficialAlbumVo;
import shop.alien.store.service.StoreOfficialAlbumService;
import shop.alien.entity.store.vo.StoreAlbumNameVo;
import shop.alien.entity.store.vo.StoreOfficialAlbumImgVo;

import java.util.List;

/**
 * 官方相册前端控制器
 *
 * @author zhangchen
 * @since 2025-07-16
 */
@Slf4j
@Api(tags = {"二期-官方相册"})
@ApiSort(1)
@CrossOrigin
@RestController
@RequestMapping("/store/official")
@RequiredArgsConstructor
public class StoreOfficialAlbumController {
    private final StoreOfficialAlbumService storeOfficialAlbumService;

    @ApiOperation("创建/更新官方相册")
    @ApiOperationSupport(order = 1)
    @PostMapping("/createOrUpdateOfficialAlbum")
    public R<StoreOfficialAlbum> createOrUpdateOfficialAlbum(@RequestBody StoreOfficialAlbum storeOfficialAlbum) {
        log.info("StoreOfficialAlbumController.createOfficialAlbum?storeOfficialAlbum={}", storeOfficialAlbum);
        try {
            StoreOfficialAlbum storeOfficial = storeOfficialAlbumService.createOrUpdateOfficialAlbum(storeOfficialAlbum);
            return R.data(storeOfficial);
        } catch (IllegalArgumentException e) {
            log.warn("创建/更新官方相册失败，参数错误：{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (RuntimeException e) {
            log.error("创建/更新官方相册失败，业务错误：{}", e.getMessage(), e);
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("创建/更新官方相册失败，系统异常：{}", e.getMessage(), e);
            return R.fail("操作失败：" + e.getMessage());
        }
    }

    @ApiOperation("获取官方相册列表")
    @ApiOperationSupport(order = 2)
    @GetMapping("/getOfficialAlbumList")
    public R<List<StoreOfficialAlbumVo>> getOfficialAlbumList(String storeId) {
        log.info("StoreOfficialAlbumController.getOfficialAlbumList");
        List<StoreOfficialAlbumVo> storeOfficialAlbumList = storeOfficialAlbumService.getOfficialAlbumList(storeId);
        return R.data(storeOfficialAlbumList);
    }

    @ApiOperation("删除官方相册")
    @ApiOperationSupport(order = 3)
    @PostMapping("/deleteOfficialAlbum")
    public  R<Boolean> deleteOfficialAlbum(@RequestBody List<StoreOfficialAlbum> storeOfficialAlbumList) {
        log.info("StoreOfficialAlbumController.deleteOfficialAlbum");
        int result = storeOfficialAlbumService.deleteOfficialAlbum(storeOfficialAlbumList);
        if (result > 0) {
            return R.data(true);
        }
        return R.fail("失败");
    }
    /**
     * 获取官方相册图片列表（客户端）
     * <p>
     * 根据门店ID和相册名称查询官方相册中的图片列表
     * 查询条件：imgType = 2（官方相册），通过 business_id 关联到 store_official_album 表，按 albumName 筛选
     * </p>
     *
     * @param storeId   门店ID，必填，必须大于0
     * @param albumName 相册名称，可选。例如：酒水、餐食、环境、全部等。当为null或空字符串时，查询全部
     * @param type      类型，必填。1：视频，2：相册，3：环境
     * @param pageNum   页码，可选，默认1
     * @param pageSize  每页数量，可选，默认10
     * @return 统一返回结果，包含图片列表和总数
     */
    @ApiOperation("获取官方相册图片列表（客户端）")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "albumName", value = "相册名称，例如：酒水、餐食、环境、全部等。不传或传空字符串时查询全部", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "1：视频，2：相册，3：封面, 4:环境", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageNum", value = "页码，默认1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量，默认10", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/getOfficialAlbumImgList")
    public R<StoreOfficialAlbumImgVo> getOfficialAlbumImgList(
            @RequestParam(value = "storeId", required = true) Integer storeId,
            @RequestParam(value = "albumName", required = false) String albumName,
            @RequestParam(value = "type", required = true) Integer type,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {
        log.info("获取官方相册图片列表，入参：storeId={}, albumName={}, type={}, pageNum={}, pageSize={}", 
                storeId, albumName, type, pageNum, pageSize);

        // 参数校验
        if (storeId == null || storeId <= 0) {
            log.warn("获取官方相册图片列表失败，门店ID无效：storeId={}", storeId);
            return R.fail("门店ID不能为空且必须大于0");
        }

        try {
            // 调用服务层获取图片列表
            StoreOfficialAlbumImgVo result = storeOfficialAlbumService.getOfficialAlbumImgList(storeId, albumName, type, pageNum, pageSize);

            // 记录返回结果
            log.info("获取官方相册图片列表成功，门店ID：{}，相册名称：{}，返回图片数量：{}",
                    storeId, albumName, result.getTotalCount());

            return R.data(result);
        } catch (Exception e) {
            log.error("获取官方相册图片列表异常，门店ID：{}，异常信息：{}", storeId, e.getMessage(), e);
            return R.fail("获取图片列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取官方相册名称列表（客户端）
     * <p>
     * 根据门店ID查询所有可用的相册名称列表，用于前端展示筛选选项
     * 返回每个相册名称及其对应的图片数量
     * </p>
     *
     * @param storeId 门店ID，必填，必须大于0
     * @return 统一返回结果，包含相册名称列表和每个相册的图片数量
     * @throws IllegalArgumentException 当门店ID为空或小于等于0时抛出
     */
    @ApiOperation("获取官方相册名称列表（客户端）")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getAlbumNameList")
    public R<List<StoreAlbumNameVo>> getAlbumNameList(
            @RequestParam(value = "storeId", required = true) Integer storeId) {
        // 记录请求入参
        log.info("获取官方相册名称列表，入参：storeId={}", storeId);

        // 参数校验
        if (storeId == null || storeId <= 0) {
            log.warn("获取官方相册名称列表失败，门店ID无效：storeId={}", storeId);
            return R.fail("门店ID不能为空且必须大于0");
        }

        try {
            // 调用服务层获取相册名称列表
            List<StoreAlbumNameVo> result = storeOfficialAlbumService.getAlbumNameList(storeId);

            // 记录返回结果
            log.info("获取官方相册名称列表成功，门店ID：{}，返回相册数量：{}", storeId, result.size());

            return R.data(result);
        } catch (Exception e) {
            log.error("获取官方相册名称列表异常，门店ID：{}，异常信息：{}", storeId, e.getMessage(), e);
            return R.fail("获取相册名称列表失败：" + e.getMessage());
        }
    }
}
