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.constant.RenovationType;
import com.tmerclub.cloud.multishop.dto.ShopRenovationDTO;
import com.tmerclub.cloud.multishop.model.ShopRenovation;
import com.tmerclub.cloud.multishop.service.ShopRenovationService;
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;

/**
 * @author 菠萝凤梨
 */
@RestController("adminShopRenovationUpDelController")
@RequestMapping("/mp/shopRenovation")
@Tag(name = "店铺页面-修改、删除、设为主页")
public class ShopRenovationUpDelController {

    @Autowired
    private ShopRenovationService shopRenovationService;


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


    @PutMapping("/updatePc")
    @Operation(summary = "更新店铺pc端装修信息", description = "更新店铺装修信息")
    public ServerResponseEntity<Void> update(@Valid @RequestBody ShopRenovationDTO shopRenovationDTO) {
        shopRenovationDTO.setRenovationType(RenovationType.PC.value());
        updateShopRenovation(shopRenovationDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping("/updateH5")
    @Operation(summary = "更新店铺移动端装修信息", description = "更新店铺装修信息")
    public ServerResponseEntity<Void> updateH5(@Valid @RequestBody ShopRenovationDTO shopRenovationDTO) {
        shopRenovationDTO.setRenovationType(RenovationType.H5.value());
        updateShopRenovation(shopRenovationDTO);
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/deletePc")
    @Operation(summary = "删除pc店铺装修信息", description = "根据店铺装修信息id删除店铺pc装修信息")
    public ServerResponseEntity<Void> delete(@RequestParam Long renovationId) {
        deleteRenovation(renovationId);
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/deleteH5")
    @Operation(summary = "删除h5店铺装修信息", description = "根据店铺装修信息id删除店铺h5装修信息")
    public ServerResponseEntity<Void> deleteH5(@RequestParam Long renovationId) {
        deleteRenovation(renovationId);
        return ServerResponseEntity.success();
    }

    @PutMapping("/updateHomeStatus")
    @Operation(summary = "PC端修改主页标识", description = "PC端修改主页标识")
    public ServerResponseEntity<Void> updateHomeStatus(@RequestParam Long renovationId) {
        updateShopRenovationStatus(renovationId);
        return ServerResponseEntity.success();
    }

    @PutMapping("/updateH5HomeStatus")
    @Operation(summary = "移动端修改主页标识", description = "移动端修改主页标识")
    public ServerResponseEntity<Void> updateH5HomeStatus(@RequestParam Long renovationId) {
        updateShopRenovationStatus(renovationId);
        return ServerResponseEntity.success();
    }

    private void updateShopRenovationStatus(Long renovationId) {
        if (BooleanUtil.isFalse(permission)) {
            throw new LuckException("抱歉，当前无操作权限");
        }
        ShopRenovation shopRenovation = shopRenovationService.getByRenovationId(renovationId);
        if (!Objects.equals(shopRenovation.getShopId(), AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        shopRenovationService.updateToHomePage(shopRenovation);
        shopRenovationService.removeCache(shopRenovation.getShopId(), shopRenovation.getRenovationType(), shopRenovation.getRenovationId());
    }

    private void updateShopRenovation(ShopRenovationDTO shopRenovationDTO) {
        if (BooleanUtil.isFalse(permission)) {
            throw new LuckException("抱歉，当前无操作权限");
        }
        ShopRenovation shopRenovation = BeanUtil.map(shopRenovationDTO, ShopRenovation.class);
        ShopRenovation shopRenovationDb = shopRenovationService.getByRenovationId(shopRenovation.getRenovationId());
        if (Objects.isNull(shopRenovationDb)) {
            throw new LuckException("该装修不存在，请刷新页面重试");
        }
        if (!Objects.equals(shopRenovationDb.getShopId(), AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        shopRenovationService.update(shopRenovation);
        shopRenovationService.removeCache(shopRenovationDb.getShopId(), shopRenovationDTO.getRenovationType(), shopRenovationDb.getRenovationId());
    }

    private void deleteRenovation(Long renovationId) {
        if (BooleanUtil.isFalse(permission)) {
            throw new LuckException("抱歉，当前无操作权限");
        }
        ShopRenovation shopRenovation = shopRenovationService.getByRenovationId(renovationId);
        if (!Objects.equals(shopRenovation.getShopId(), AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        shopRenovationService.deleteById(renovationId, shopRenovation.getRenovationType(), shopRenovation.getShopId());
        shopRenovationService.removeCache(shopRenovation.getShopId(), shopRenovation.getRenovationType(), shopRenovation.getRenovationId());
    }
}
