package com.jeesharp.business.member.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.jeesharp.business.member.vo.PointsSummaryVO;
import com.jeesharp.business.modules.member.application.MemberApplicationService;
import com.jeesharp.business.modules.member.application.MemberSignInApplicationService;
import com.jeesharp.business.modules.member.vo.MemberSignInRecordItemVO;
import com.jeesharp.business.modules.member.vo.MemberSignInRecordQueryVO;
import com.jeesharp.business.modules.member.vo.MemberSignInResultVO;
import com.jeesharp.business.modules.member.vo.MemberSignInStatusVO;
import com.jeesharp.framework.memberauth.utils.MemberAuthUtils;
import com.jeesharp.framework.memberauth.web.BaseMobileController;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.query.PageRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 移动端积分与签到接口，提供可用积分与积分流水记录、每日签到及签到记录分页。
 *
 * @author jeesharp
 * @since 2026-05-08
 */
@Slf4j
@RestController
@RequestMapping("${jeesharp.web.mobilePath}/member/points")
@Tag(name = "移动端-积分", description = "积分查询、签到等接口")
@AllArgsConstructor
public class PointsMobileController extends BaseMobileController {

    private final MemberSignInApplicationService memberSignInApplicationService;
    private final MemberApplicationService memberApplicationService;

    @Operation(summary = "获取积分概览")
    @GetMapping("/summary")
    public AjaxResult<PointsSummaryVO> summary() {
        // TODO: 实现获取积分概览逻辑
        PointsSummaryVO summary = new PointsSummaryVO();
        summary.setAvailablePoints(1280);
        summary.setFrozenPoints(100);
        summary.setTotalPoints(5680);
        summary.setUsedPoints(4200);
        summary.setExpiredPoints(100);
        summary.setExpiringSoonPoints(200);
        summary.setExpiringSoonDate("2026-03-31");
        return AjaxResult.okData(summary);
    }

    @Operation(summary = "获取积分流水")
    @PostMapping("/list")
    @SaCheckLogin
    public AjaxResult<Map<String, Object>> list(@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", 56);
        data.put("totalPage", 6);

        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        item.put("id", "xxx");
        item.put("pointsType", 1);
        item.put("pointsTypeName", "获取");
        item.put("points", 100);
        item.put("bizType", "CONSUME");
        item.put("bizTypeName", "消费获取");
        item.put("bizDesc", "订单消费获得积分");
        item.put("createDate", "2026-01-24 10:30:00");
        list.add(item);
        data.put("list", list);

        return AjaxResult.okData(data);
    }

    @Operation(summary = "签到")
    @PostMapping("/signIn")
    @SaCheckLogin
    public AjaxResult<MemberSignInResultVO> signIn() {
        String memberId = MemberAuthUtils.getLoginUserId();
        MemberSignInResultVO result = memberSignInApplicationService.signIn(memberId);
        return AjaxResult.ok("签到成功", result);
    }

    @Operation(summary = "获取签到状态")
    @GetMapping("/signInStatus")
    @SaCheckLogin
    public AjaxResult<MemberSignInStatusVO> signInStatus() {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberSignInApplicationService.getSignInStatus(memberId));
    }

    @Operation(summary = "签到记录（分页）")
    @PostMapping("/signInRecord/list")
    @SaCheckLogin
    public AjaxResult<PageRespVO<MemberSignInRecordItemVO>> signInRecordList(
            @RequestBody(required = false) MemberSignInRecordQueryVO query) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberSignInApplicationService.pageSignInRecords(memberId, query));
    }

    @Operation(summary = "积分记录（仅积分流水 app_member_points_log）")
    @GetMapping("/records")
    @SaCheckLogin
    public AjaxResult<List<Map<String, Object>>> records() {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberApplicationService.listMiniProgramPointsRecords(memberId));
    }

    @Operation(summary = "当前可用积分（小程序简化版）")
    @GetMapping("/available")
    @SaCheckLogin
    public AjaxResult<Integer> available() {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberApplicationService.getAvailablePoints(memberId));
    }
}
