package shop.alien.storeplatform.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.StoreInfoDraft;
import shop.alien.entity.store.dto.StoreInfoDto;
import shop.alien.entity.store.vo.StoreInfoVo;
import shop.alien.entity.store.vo.StoreMainInfoVo;
import shop.alien.storeplatform.service.StoreManageService;
import shop.alien.storeplatform.util.LoginUserUtil;

/**
 * web端商户店铺管理 前端控制器
 *
 * @author ssk
 * @since 2025-01-xx
 */
@Slf4j
@Api(tags = {"web端商户店铺管理"})
@ApiSort(4)
@CrossOrigin
@RestController
@RequestMapping("/storeManage")
@RequiredArgsConstructor
public class StoreManageController {

    private final StoreManageService storeManageService;

    @ApiOperation("新增店铺入驻申请")
    @ApiOperationSupport(order = 1)
    @PostMapping("/applyStore")
    public R<StoreInfoVo> applyStore(@RequestBody StoreInfoDto storeInfoDto) {
        log.info("StoreManageController.applyStore?storeInfoDto={}", storeInfoDto);
        try {
            StoreInfoVo result = storeManageService.applyStore(storeInfoDto);
            if (result != null) {
                return R.data(result, "店铺入驻申请提交成功");
            }
            return R.fail("申请失败");
        } catch (Exception e) {
            log.error("StoreManageController.applyStore ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("保存店铺入驻申请草稿")
    @ApiOperationSupport(order = 2)
    @PostMapping("/saveStoreDraft")
    public R<Boolean> saveStoreDraft(@RequestBody StoreInfoDraft storeInfoDraft) {
        log.info("StoreManageController.saveStoreDraft?storeInfoDraft={}", storeInfoDraft);
        try {
            int result = storeManageService.saveStoreDraft(storeInfoDraft);
            if (result > 0) {
                return R.data(true, "草稿保存成功");
            }
            return R.fail("草稿保存失败");
        } catch (Exception e) {
            log.error("StoreManageController.saveStoreDraft ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("查询店铺入驻申请草稿")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeUserId", value = "商户用户ID", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/getStoreDraft")
    public R<StoreInfoDraft> getStoreDraft(@RequestParam("storeUserId") int storeUserId) {
        log.info("StoreManageController.getStoreDraft?storeUserId={}", storeUserId);
        try {
            StoreInfoDraft result = storeManageService.getStoreDraft(storeUserId);
            return R.data(result);
        } catch (Exception e) {
            log.error("StoreManageController.getStoreDraft ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("获取店铺详细信息")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "店铺ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getStoreDetail")
    public R<StoreMainInfoVo> getStoreDetail(@RequestParam("id") Integer id) {
        log.info("StoreManageController.getStoreDetail?id={}", id);
        try {
            StoreMainInfoVo result = storeManageService.getStoreDetail(id);
            if (result != null) {
                return R.data(result);
            }
            return R.fail("店铺不存在");
        } catch (Exception e) {
            log.error("StoreManageController.getStoreDetail ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("获取店铺今日收益")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "店铺ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getTodayIncome")
    public R<String> getTodayIncome() {
        try {
            Integer storeId = LoginUserUtil.getCurrentStoreId();
            String income = storeManageService.getTodayIncome(storeId);
            return R.data(income, "查询成功");
        } catch (Exception e) {
            log.error("StoreManageController.getTodayIncome ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("获取店铺今日订单数")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "店铺ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getTodayOrderCount")
    public R<Integer> getTodayOrderCount() {
        try {
            Integer storeId = LoginUserUtil.getCurrentStoreId();
            Integer count = storeManageService.getTodayOrderCount(storeId);
            return R.data(count, "查询成功");
        } catch (Exception e) {
            log.error("StoreManageController.getTodayOrderCount ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }
}

