package com.haha.miniapp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.haha.common.constant.ResponseEnum;
import com.haha.common.vo.PageResult;
import com.haha.common.vo.Result;
import com.haha.entity.InviteActivity;
import com.haha.entity.InviteRecord;
import com.haha.entity.dto.BindInviteDTO;
import com.haha.service.InviteActivityService;
import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 小程序端邀请活动控制器
 * 提供邀请码获取、邀请关系绑定、我的邀请记录、邀请统计、海报生成等功能
 */
@Slf4j
@RestController
@RequestMapping("/invite")
@RequiredArgsConstructor
public class InviteController {

    private final InviteActivityService inviteActivityService;

    @GetMapping("/code")
    public Result<Map<String, Object>> getInviteCode() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[邀请] 用户请求邀请码 - userId: {}", userId);
        Map<String, Object> result = inviteActivityService.getInviteCodeInfo(userId);
        if (result == null) {
            log.info("[邀请] 当前无进行中的邀请活动 - userId: {}", userId);
            return Result.error("暂无进行中的邀请活动");
        }
        log.info("[邀请] 生成邀请码成功 - userId: {}, inviteCode: {}", userId, result.get("inviteCode"));
        return Result.success("查询成功", result);
    }

    @PostMapping("/bind")
    public Result<Boolean> bindInviteRelation(@RequestBody BindInviteDTO dto) {
        Long userId = StpUtil.getLoginIdAsLong();

        if (dto.getInviteCode() == null || dto.getInviteCode().isEmpty()) {
            return ResponseEnum.PARAM_REQUIRED.toResult("参数错误：inviteCode 不能为空");
        }

        String inviteCode = dto.getInviteCode();
        String sourceChannel = dto.getSourceChannel() != null ? dto.getSourceChannel() : "";
        String deviceInfo = dto.getDeviceInfo() != null ? dto.getDeviceInfo() : "";

        log.info("[邀请] 绑定邀请关系 - userId: {}, inviteCode: {}, sourceChannel: {}",
                userId, inviteCode, sourceChannel);

        boolean success = inviteActivityService.bindInviteRelation(userId, inviteCode, sourceChannel, deviceInfo);
        if (!success) {
            return Result.error("绑定邀请关系失败");
        }

        log.info("[邀请] 绑定邀请关系成功 - userId: {}, inviteCode: {}", userId, inviteCode);
        return Result.success("绑定成功", true);
    }

    @GetMapping("/my-records")
    public Result<PageResult<InviteRecord>> getMyInviteRecords(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[邀请] 查询我的邀请记录 - userId: {}, page: {}, pageSize: {}", userId, page, pageSize);
        IPage<InviteRecord> records = inviteActivityService.getMyInviteRecords(userId, page, pageSize);
        log.info("[邀请] 我的邀请记录查询完成 - userId: {}, total: {}", userId, records.getTotal());
        return Result.success("查询成功", PageResult.of(records));
    }

    @GetMapping("/my-statistics")
    public Result<Map<String, Object>> getMyInviteStatistics() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[邀请] 查询我的邀请统计 - userId: {}", userId);
        Map<String, Object> statistics = inviteActivityService.getMyInviteStatistics(userId);
        log.info("[邀请] 我的邀请统计数据 - userId: {}, statistics: {}", userId, statistics);
        return Result.success("查询成功", statistics);
    }

    @GetMapping("/activity-info")
    public Result<InviteActivity> getActivityInfo() {
        log.info("[邀请] 查询当前邀请活动信息");
        InviteActivity activity = inviteActivityService.getOngoingActivity();
        return Result.success("查询成功", activity);
    }

    @GetMapping("/poster")
    public Result<Map<String, Object>> generatePoster() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[邀请] 生成邀请海报 - userId: {}", userId);
        Map<String, Object> result = inviteActivityService.generatePosterInfo(userId);
        if (result == null) {
            log.info("[邀请] 当前无进行中的邀请活动 - userId: {}", userId);
            return Result.error("暂无进行中的邀请活动");
        }
        log.info("[邀请] 生成邀请海报成功 - userId: {}, inviteCode: {}", userId, result.get("inviteCode"));
        return Result.success("查询成功", result);
    }
}
