package com.ruoyi.web.modules.home.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.modules.account.support.MemberContext;
import com.ruoyi.web.modules.home.dto.ShopGoodsQuery;
import com.ruoyi.web.modules.home.service.IShopAppService;
import com.ruoyi.web.modules.home.vo.HomeHotGoodsVO;

/**
 * C 端店铺主页（只读 + 关注）
 */
@RestController
@RequestMapping("/api/shop")
public class ShopAppController extends BaseController
{
    @Autowired
    private IShopAppService shopAppService;

    @Autowired
    private TokenService tokenService;

    @Anonymous
    @GetMapping("/{shopId}")
    public AjaxResult profile(@PathVariable("shopId") Long shopId, HttpServletRequest request)
    {
        return success(shopAppService.getProfile(shopId, resolveOptionalMemberId(request)));
    }

    @Anonymous
    @GetMapping("/{shopId}/categories")
    public AjaxResult categories(@PathVariable("shopId") Long shopId)
    {
        return success(shopAppService.listCategories(shopId));
    }

    @Anonymous
    @GetMapping("/{shopId}/categories/{level1Id}/level2-tabs")
    public AjaxResult level2Tabs(@PathVariable("shopId") Long shopId,
            @PathVariable("level1Id") Long level1Id)
    {
        return success(shopAppService.listLevel2Tabs(shopId, level1Id));
    }

    @Anonymous
    @GetMapping("/{shopId}/goods")
    public TableDataInfo goods(@PathVariable("shopId") Long shopId, ShopGoodsQuery query)
    {
        startPage();
        List<HomeHotGoodsVO> list = shopAppService.listGoods(shopId, query);
        return getDataTable(list);
    }

    @PostMapping("/{shopId}/follow")
    public AjaxResult follow(@PathVariable("shopId") Long shopId)
    {
        shopAppService.follow(shopId, requireMemberId());
        return success();
    }

    @DeleteMapping("/{shopId}/follow")
    public AjaxResult unfollow(@PathVariable("shopId") Long shopId)
    {
        shopAppService.unfollow(shopId, requireMemberId());
        return success();
    }

    private Long requireMemberId()
    {
        Long memberId = MemberContext.getMemberId();
        if (memberId == null)
        {
            throw new ServiceException("请先登录", 401);
        }
        return memberId;
    }

    private Long resolveOptionalMemberId(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (loginUser == null || loginUser.getUserId() == null)
        {
            return null;
        }
        return loginUser.getUserId();
    }
}
