
package com.tmerclub.cloud.product.controller.multishop;

import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.api.product.dto.CategoryShopDTO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.i18n.I18nMessage;
import com.tmerclub.cloud.common.product.vo.CategoryVO;
import com.tmerclub.cloud.common.product.vo.app.CategoryAppVO;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.product.model.CategoryShop;
import com.tmerclub.cloud.product.service.CategoryService;
import com.tmerclub.cloud.product.service.CategoryShopService;
import com.tmerclub.cloud.product.vo.CategoryShopVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author lth
 */
@RestController("multishopCategoryController")
@RequestMapping("/m/apply_shop/category")
@Tag(name = "multishop-分类信息")
public class CategoryController {

    @Autowired
    private CategoryShopService categoryShopService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping("/signing")
    @Operation(summary = "签约分类", description = "签约分类")
    public ServerResponseEntity<Void> signingCategory(@Valid @RequestBody List<CategoryShopDTO> categoryShopDTOList) {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        categoryShopService.signingCategory(categoryShopDTOList, shopId, sysType);
        return ServerResponseEntity.success();
    }

    @GetMapping("/list_by_shop")
    @Operation(summary = "获取签约的分类列表(返回所有)", description = "获取签约的分类列表(返回所有)")
    public ServerResponseEntity<List<CategoryShopVO>> listByShopId() {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        List<CategoryShopVO> categoryShopVOList = categoryShopService.listByShopId(shopId, sysType, I18nMessage.getLang());
        return ServerResponseEntity.success(categoryShopVOList);
    }

    @GetMapping("/platform_categories")
    @Operation(summary = "获取平台所有的分类信息", description = "获取所有的分类列表信息")
    public ServerResponseEntity<List<CategoryAppVO>> platformCategories() {
        return ServerResponseEntity.success(categoryService.platformCategories());
    }

    @GetMapping("/list_signing_category")
    @Schema(description = "获取签约的可用分类列表(发布商品时使用,包含对应的上级分类）")
    @Parameter(name = "status", description = "签约分类状态 1:enable, 0:disable, -1:deleted")
    public ServerResponseEntity<List<CategoryAppVO>> listSigningCategory(@RequestParam(value = "status", required = false) Integer status) {
        Long shopId = AuthUserContext.get().getTenantId();
        List<CategoryAppVO> categoryList = categoryService.listSigningCategory(shopId, AuthUserContext.get().getSysType(), I18nMessage.getLang());
        if (Objects.nonNull(status)) {
            categoryList = categoryList.stream().filter(categoryAppVO -> Objects.equals(categoryAppVO.getStatus(), status)).collect(Collectors.toList());
        }
        return ServerResponseEntity.success(categoryList);
    }

    @DeleteMapping("/delete_signing_category")
    @Operation(summary = "根据签约分类id删除签约信息", description = "根据签约分类id删除签约信息")
    public ServerResponseEntity<Void> deleteSigningCategory(@RequestParam("categoryShopId") Long categoryShopId) {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        CategoryShop categoryShop = categoryShopService.getById(categoryShopId);
        if (Objects.isNull(categoryShop)) {
            throw new LuckException("找不到当前签约分类，请刷新后重试");
        }
        if (!Objects.equals(shopId, categoryShop.getShopId()) && !Objects.equals(sysType, SysTypeEnum.MULTISHOP.value())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        categoryShopService.deleteSigningCategory(categoryShop);
        return ServerResponseEntity.success();
    }

    @GetMapping("/list_apply_signing_category")
    @Operation(summary = "获取可以签约的平台分类列表（已经签约的平台分类不会返回）", description = "获取可以签约的平台分类列表（已经签约的平台分类不会返回）")
    public ServerResponseEntity<List<CategoryVO>> listApplySigningCategory() {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        return ServerResponseEntity.success(categoryService.listApplySigningCategory(shopId, sysType));
    }

    @PostMapping("/add_signing_category")
    @Operation(summary = "新增签约分类", description = "新增签约分类")
    public ServerResponseEntity<Void> applyCategory(@Valid @RequestBody List<CategoryShopDTO> categoryShopDTOList) {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        categoryShopService.addSigningCategory(categoryShopDTOList, shopId, sysType);
        return ServerResponseEntity.success();
    }
}
