package com.haha.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.haha.common.exception.BusinessException;
import com.haha.common.vo.Result;
import com.haha.entity.DistributorReferral;
import com.haha.entity.dto.WithdrawalApplyDTO;
import com.haha.entity.vo.DistributorMpStatsVO;
import com.haha.service.DistributorReferralService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Collections;

@Slf4j
@RestController
@RequestMapping("/mp/distribution")
@RequiredArgsConstructor
public class DistributorMpController {

    private final DistributorReferralService referralService;

    @PostMapping("/bind")
    public Result<DistributorReferral> bindReferral(@RequestParam String distributorCode) {
        Long userId = StpUtil.getLoginIdAsLong();
        DistributorReferral referral = referralService.bindReferral(distributorCode, userId);
        return Result.success("绑定成功", referral);
    }

    @GetMapping("/my-stats")
    public Result<DistributorMpStatsVO> getMyStats() {
        Long userId = StpUtil.getLoginIdAsLong();
        // TODO: 需要建立小程序用户与分销员的关联关系
        DistributorMpStatsVO stats = DistributorMpStatsVO.builder()
                .totalReferrals(0L)
                .validReferrals(0L)
                .commissionBalance(BigDecimal.ZERO)
                .frozenAmount(BigDecimal.ZERO)
                .totalCommission(BigDecimal.ZERO)
                .totalWithdrawn(BigDecimal.ZERO)
                .build();
        return Result.success("查询成功", stats);
    }

    @PostMapping("/withdrawal")
    public Result<Void> applyWithdrawal(@RequestBody @Valid WithdrawalApplyDTO dto) {
        Long userId = StpUtil.getLoginIdAsLong();
        // TODO: 需要建立小程序用户与分销员的关联关系
        throw new BusinessException(500, "功能开发中");
    }

    @GetMapping("/withdrawal/list")
    public Result<Object> getMyWithdrawals() {
        Long userId = StpUtil.getLoginIdAsLong();
        // TODO: 需要建立小程序用户与分销员的关联关系
        return Result.success("查询成功", Collections.emptyList());
    }
}
