package com.xindazhou.business.controller.admin.member;

import com.xindazhou.business.api.member.MemberTagApi;
import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.pojo.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 管理后台 - 会员标签（代理 Controller，转发到用户服务）
 *
 * @author xindazhou
 */
@Slf4j
@Tag(name = "管理后台 - 会员标签")
@RestController
@RequestMapping("/member/tag")
@Validated
public class MemberTagController {

    @Resource
    private MemberTagApi memberTagApi;

    @PostMapping("/create")
    @Operation(summary = "创建会员标签")
    @PreAuthorize("@ss.hasPermission('member:tag:create')")
    public CommonResult<Long> createTag(@Valid @RequestBody Map<String, Object> createReqVO) {
        return memberTagApi.createTag(createReqVO);
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员标签")
    @PreAuthorize("@ss.hasPermission('member:tag:update')")
    public CommonResult<Boolean> updateTag(@Valid @RequestBody Map<String, Object> updateReqVO) {
        return memberTagApi.updateTag(updateReqVO);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员标签")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:tag:delete')")
    public CommonResult<Boolean> deleteTag(@RequestParam("id") Long id) {
        return memberTagApi.deleteTag(id);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员标签")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:tag:query')")
    public CommonResult<Map<String, Object>> getMemberTag(@RequestParam("id") Long id) {
        return memberTagApi.getMemberTag(id);
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "获取会员标签精简信息列表", description = "只包含被开启的会员标签，主要用于前端的下拉选项")
    @PreAuthorize("@ss.hasPermission('member:tag:query')")
    public CommonResult<List<Map<String, Object>>> getSimpleTagList() {
        // 调试：检查是否能获取到 LoginUser
        com.xindazhou.framework.security.core.LoginUser loginUser = 
            com.xindazhou.framework.security.core.util.SecurityFrameworkUtils.getLoginUser();
        if (loginUser == null) {
            log.error("[MemberTagController] getSimpleTagList: LoginUser 为空，无法通过 Feign 传递用户信息");
        } else {
            log.info("[MemberTagController] getSimpleTagList: LoginUser 存在, userId: {}", loginUser.getId());
        }
        log.info("[MemberTagController] 准备调用 Feign: memberTagApi.getSimpleTagList()");
        return memberTagApi.getSimpleTagList();
    }

    @GetMapping("/list")
    @Operation(summary = "获得会员标签列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('member:tag:query')")
    public CommonResult<List<Map<String, Object>>> getMemberTagList(@RequestParam("ids") Collection<Long> ids) {
        return memberTagApi.getMemberTagList(ids);
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员标签分页")
    @PreAuthorize("@ss.hasPermission('member:tag:query')")
    public CommonResult<PageResult<Map<String, Object>>> getTagPage(@Valid @ModelAttribute Map<String, Object> pageVO) {
        return memberTagApi.getTagPage(pageVO);
    }
}

