package com.jeesharp.business.member.controller;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.member.dto.CompleteProfileDTO;
import com.jeesharp.business.modules.member.dto.RealNameAuthDTO;
import com.jeesharp.business.modules.member.dto.UpdateMemberDTO;
import com.jeesharp.business.modules.member.application.MemberApplicationService;
import com.jeesharp.business.modules.member.vo.MemberCodeCardVO;
import com.jeesharp.business.modules.member.vo.MemberInfoVO;
import com.jeesharp.framework.memberauth.utils.MemberAuthUtils;
import com.jeesharp.framework.memberauth.web.BaseMobileController;
import com.jeesharp.framework.pojo.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 移动端会员信息接口
 *
 * @author jeesharp
 */
@Slf4j
@RestController
@RequestMapping("${jeesharp.web.mobilePath}/member/member")
@Tag(name = "移动端-会员信息", description = "会员个人信息管理接口")
@AllArgsConstructor
public class MemberMobileController extends BaseMobileController {

    private final MemberApplicationService memberApplicationService;

    @Operation(summary = "获取当前会员信息")
    @GetMapping("/info")
    public AjaxResult<MemberInfoVO> info() {
         String memberId = MemberAuthUtils.getLoginUserId();
         MemberInfoVO member = memberApplicationService.getMember(memberId);
        return AjaxResult.okData(member);
    }

    @Operation(summary = "获取会员码卡片（编号与二维码内容）")
    @GetMapping("/codeCard")
    public AjaxResult<MemberCodeCardVO> codeCard() {
        String memberId = MemberAuthUtils.getLoginUserId();
        if (StrUtil.isBlank(memberId)) {
            return AjaxResult.needLogin();
        }
        return AjaxResult.okData(memberApplicationService.getMemberCodeCard(memberId));
    }

    @Operation(summary = "更新会员信息")
    @PostMapping("/update")
    public AjaxResult<Void> update(@RequestBody UpdateMemberDTO dto) {
        String memberId = MemberAuthUtils.getLoginUserId();
        if (StrUtil.isBlank(memberId)) {
            return AjaxResult.needLogin();
        }
        memberApplicationService.updateMemberByMobile(memberId, dto);
        return AjaxResult.ok("更新成功");
    }

    @Operation(summary = "完善个人信息（姓名 + 手机号）")
    @PostMapping("/completeProfile")
    public AjaxResult<Void> completeProfile(@RequestBody @Valid CompleteProfileDTO dto) {
        String memberId = MemberAuthUtils.getLoginUserId();
        if (StrUtil.isBlank(memberId)) {
            return AjaxResult.needLogin();
        }
        memberApplicationService.completeProfile(memberId, dto);
        return AjaxResult.ok("完善成功");
    }

    @Operation(summary = "实名认证")
    @PostMapping("/realNameAuth")
    public AjaxResult<Map<String, Object>> realNameAuth(@RequestBody @Valid RealNameAuthDTO dto) {
        // TODO: 实现实名认证逻辑
        // String memberId = SecurityUtils.getMemberId();
        // memberApplicationService.realNameAuth(memberId, dto);
        log.info("实名认证: realName={}", dto.getRealName());
        Map<String, Object> data = new HashMap<>();
        data.put("isRealName", 1);
        return AjaxResult.ok("实名认证成功", data);
    }

    @Operation(summary = "获取会员等级详情")
    @GetMapping("/levelInfo")
    public AjaxResult<Map<String, Object>> levelInfo() {
        // TODO: 实现获取等级详情逻辑
        Map<String, Object> data = new HashMap<>();
        
        // 当前等级
        Map<String, Object> currentLevel = new HashMap<>();
        currentLevel.put("levelCode", "GOLD");
        currentLevel.put("levelName", "黄金会员");
        currentLevel.put("levelIcon", "https://xxx.com/gold.png");
        currentLevel.put("discountRate", 0.95);
        currentLevel.put("pointsRate", 1.5);
        currentLevel.put("privileges", Arrays.asList("95折优惠", "1.5倍积分", "生日200积分"));
        data.put("currentLevel", currentLevel);
        
        data.put("growthValue", 5680);
        
        // 下一等级
        Map<String, Object> nextLevel = new HashMap<>();
        nextLevel.put("levelCode", "PLATINUM");
        nextLevel.put("levelName", "铂金会员");
        nextLevel.put("levelIcon", "https://xxx.com/platinum.png");
        nextLevel.put("minGrowthValue", 20000);
        nextLevel.put("remainGrowth", 14320);
        data.put("nextLevel", nextLevel);
        
        // 所有等级
        List<Map<String, Object>> allLevels = new ArrayList<>();
        allLevels.add(Map.of("levelCode", "NORMAL", "levelName", "普通会员", "minGrowthValue", 0));
        allLevels.add(Map.of("levelCode", "SILVER", "levelName", "白银会员", "minGrowthValue", 1000));
        allLevels.add(Map.of("levelCode", "GOLD", "levelName", "黄金会员", "minGrowthValue", 5000));
        allLevels.add(Map.of("levelCode", "PLATINUM", "levelName", "铂金会员", "minGrowthValue", 20000));
        allLevels.add(Map.of("levelCode", "DIAMOND", "levelName", "钻石会员", "minGrowthValue", 50000));
        data.put("allLevels", allLevels);
        
        return AjaxResult.okData(data);
    }

    @Operation(summary = "获取邀请信息")
    @GetMapping("/inviteInfo")
    public AjaxResult<Map<String, Object>> inviteInfo() {
        // TODO: 实现获取邀请信息逻辑
        Map<String, Object> data = new HashMap<>();
        data.put("inviteCode", "ABC123");
        data.put("inviteUrl", "https://xxx.com/invite?code=ABC123");
        data.put("inviteQrCode", "https://xxx.com/qrcode/ABC123.png");
        data.put("inviteCount", 12);
        data.put("inviteRewardPoints", 1200);
        data.put("inviteRules", Arrays.asList(
            "邀请好友注册成功，双方各获100积分",
            "被邀请人首单消费后，邀请人额外获200积分"
        ));
        return AjaxResult.okData(data);
    }

    @Operation(summary = "获取邀请记录")
    @PostMapping("/inviteList")
    public AjaxResult<Map<String, Object>> inviteList(@RequestBody Map<String, Object> params) {
        // TODO: 实现获取邀请记录逻辑
        Integer pageNo = (Integer) params.getOrDefault("pageNo", 1);
        Integer pageSize = (Integer) params.getOrDefault("pageSize", 10);
        
        Map<String, Object> data = new HashMap<>();
        data.put("pageNo", pageNo);
        data.put("pageSize", pageSize);
        data.put("totalCount", 12);
        data.put("totalPage", 2);
        
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        item.put("memberId", "xxx");
        item.put("nickname", "被邀请人1");
        item.put("avatar", "https://xxx.com/avatar1.jpg");
        item.put("registerDate", "2026-01-20 15:30:00");
        item.put("rewardPoints", 100);
        item.put("hasFirstOrder", true);
        list.add(item);
        data.put("list", list);
        
        return AjaxResult.okData(data);
    }
}
