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

import cn.hutool.core.util.BooleanUtil;
import com.tmerclub.cloud.common.exception.LuckException;
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.common.util.BeanUtil;
import com.tmerclub.cloud.multishop.dto.ShopTemplateDTO;
import com.tmerclub.cloud.multishop.model.ShopTemplate;
import com.tmerclub.cloud.multishop.service.ShopTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController("adminShopTemplateUpDelController")
@RequestMapping("/mp/shop_template")
@Tag(name = "店铺装修模板-修改、删除、设为主页")
public class ShopTemplateUpDelController {

    @Autowired
    private ShopTemplateService shopTemplateService;


    @Value("${tmerclub.expose.operation.auth:}")
    private Boolean permission;

    @PutMapping("/updatePc")
    @Operation(summary = "更新商品PC装修模板表", description = "更新商品PC装修模板表")
    public ServerResponseEntity<Void> update(@Valid @RequestBody ShopTemplateDTO shopTemplateDTO) {
        if (BooleanUtil.isFalse(permission)) {
            throw new LuckException("没有权限进行操作");
        }
        updateTemplate(shopTemplateDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping("/updateH5")
    @Operation(summary = "更新商品H5装修模板表", description = "更新商品H5装修模板表")
    public ServerResponseEntity<Void> updateH5(@Valid @RequestBody ShopTemplateDTO shopTemplateDTO) {
        if (BooleanUtil.isFalse(permission)) {
            throw new LuckException("没有权限进行操作");
        }
        updateTemplate(shopTemplateDTO);
        return ServerResponseEntity.success();
    }

    @DeleteMapping("delete_pc")
    @Operation(summary = "删除商品PC装修模板表", description = "根据商品装修模板表id删除商品PC装修模板表")
    public ServerResponseEntity<Void> delete(@RequestParam Long templateId) {
        if (BooleanUtil.isFalse(permission)) {
            throw new LuckException("没有权限进行操作");
        }
        deleteTemplate(templateId);
        return ServerResponseEntity.success();
    }

    @DeleteMapping("delete_h5")
    @Operation(summary = "删除商品H5装修模板表", description = "根据商品装修模板表id删除商品H5装修模板表")
    public ServerResponseEntity<Void> deleteH5(@RequestParam Long templateId) {
        if (BooleanUtil.isFalse(permission)) {
            throw new LuckException("没有权限进行操作");
        }
        deleteTemplate(templateId);
        return ServerResponseEntity.success();
    }

    private void deleteTemplate(Long templateId) {
        ShopTemplate shopTemplateDb = shopTemplateService.getByTemplateId(templateId);
        if (Objects.isNull(shopTemplateDb)) {
            throw new LuckException("模板不存在，请刷新页面重试");
        }
        if (!Objects.equals(shopTemplateDb.getShopId(), AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        shopTemplateService.deleteById(templateId);
        shopTemplateService.removeCache(templateId);
    }

    private void updateTemplate(ShopTemplateDTO shopTemplateDTO) {
        ShopTemplate shopTemplate = BeanUtil.map(shopTemplateDTO, ShopTemplate.class);
        ShopTemplate shopTemplateDb = shopTemplateService.getByTemplateId(shopTemplate.getTemplateId());
        if (Objects.isNull(shopTemplateDb)) {
            throw new LuckException("模板不存在，请刷新页面重试");
        }
        if (!Objects.equals(shopTemplateDb.getShopId(), AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        shopTemplateService.update(shopTemplate);
        shopTemplateService.removeCache(shopTemplate.getTemplateId());
    }
}
