package com.ruoyi.web.modules.store.service.impl;

import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.merchant.vo.MerchantShopBriefVO;
import com.ruoyi.web.modules.store.constant.ShopConstants;
import com.ruoyi.web.modules.store.domain.BizMerchantAccount;
import com.ruoyi.web.modules.store.domain.BizShop;
import com.ruoyi.web.modules.store.dto.SellerShopSwitchDTO;
import com.ruoyi.web.modules.store.mapper.BizShopMapper;
import com.ruoyi.web.modules.store.service.ISellerShopContextService;
import com.ruoyi.web.modules.store.support.SellerShopContextSupport;
import com.ruoyi.web.modules.store.vo.SellerContextVO;

/**
 * 商家端店铺上下文 Service 实现
 */
@Service
public class SellerShopContextServiceImpl implements ISellerShopContextService
{
    @Autowired
    private SellerShopContextSupport sellerShopContextSupport;

    @Autowired
    private BizShopMapper shopMapper;

    @Override
    public SellerContextVO selectContext(Long userId, String sessionToken)
    {
        BizMerchantAccount account = sellerShopContextSupport.requireSellerAccount(userId);
        Long shopId = sellerShopContextSupport.resolveCurrentShopId(sessionToken, account);
        return buildContext(account, shopId);
    }

    @Override
    public SellerContextVO switchShop(Long userId, String sessionToken, SellerShopSwitchDTO dto)
    {
        BizMerchantAccount account = sellerShopContextSupport.requireSellerAccount(userId);
        if (!sellerShopContextSupport.isMerchantAdmin(account))
        {
            throw new ServiceException(ShopConstants.MSG_SHOP_SWITCH_FORBIDDEN);
        }
        if (dto == null || dto.getShopId() == null)
        {
            throw new ServiceException(ShopConstants.MSG_SHOP_CONTEXT_REQUIRED);
        }
        sellerShopContextSupport.assertShopAccessible(account, dto.getShopId());
        sellerShopContextSupport.saveSelectedShopId(sessionToken, dto.getShopId());
        return buildContext(account, dto.getShopId());
    }

    private SellerContextVO buildContext(BizMerchantAccount account, Long shopId)
    {
        SellerContextVO vo = new SellerContextVO();
        vo.setMerchantId(account.getMerchantId());
        vo.setShopId(shopId);
        vo.setShopName(resolveShopName(shopId));
        boolean admin = sellerShopContextSupport.isMerchantAdmin(account);
        vo.setAccountType(admin ? ShopConstants.ACCOUNT_TYPE_ADMIN : ShopConstants.ACCOUNT_TYPE_EMPLOYEE);
        vo.setSwitchable(admin);
        if (admin)
        {
            List<MerchantShopBriefVO> shops = shopMapper.selectBriefByMerchantId(account.getMerchantId());
            vo.setShops(shops);
        }
        else
        {
            vo.setShops(Collections.emptyList());
        }
        return vo;
    }

    private String resolveShopName(Long shopId)
    {
        if (shopId == null)
        {
            return "";
        }
        BizShop shop = shopMapper.selectById(shopId);
        return shop != null ? shop.getShopName() : "";
    }
}
