package com.gxwebsoft.cms.controller;

import com.gxwebsoft.cms.param.CustomerLeadParam;
import com.gxwebsoft.cms.param.LeadReferralParam;
import com.gxwebsoft.cms.service.LeadReferralService;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 推荐人控制器（全民推荐）
 *
 * @author 科技小王子
 * @since 2026-04-14
 */
@Slf4j
@RestController
@RequestMapping("/lead/referral")
@Tag(name = "全民推荐", description = "推荐人报备相关接口")
public class LeadReferralController extends BaseController {

    @Autowired
    private LeadReferralService leadReferralService;

    @Operation(summary = "匿名用户报备客户")
    @PostMapping("/anonymous")
    public ApiResult<?> anonymousReferral(@RequestBody LeadReferralParam param) {
        return success(leadReferralService.anonymousReferral(param));
    }

    @Operation(summary = "注册用户报备客户")
    @PostMapping("/user")
    @PreAuthorize("@pms.hasPermission('referral:user') or true")
    public ApiResult<?> userReferral(@RequestBody LeadReferralParam param) {
        return success(leadReferralService.userReferral(param));
    }

    @Operation(summary = "获取推荐人的推荐记录")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('referral:page') or @pms.hasPermission('referral:view')")
    public ApiResult<Map<String, Object>> getReferralPage(CustomerLeadParam param) {
        return success(leadReferralService.getReferralPage(param));
    }

    @Operation(summary = "获取推荐人统计")
    @GetMapping("/stats/{userId}")
    @PreAuthorize("@pms.hasPermission('referral:view')")
    public ApiResult<?> getReferralStats(
            @Parameter(description = "用户ID") @PathVariable Integer userId) {
        return success(leadReferralService.getReferralStats(userId));
    }

    @Operation(summary = "生成推荐码")
    @GetMapping("/generateCode")
    @PreAuthorize("@pms.hasPermission('referral:generate')")
    public ApiResult<?> generateReferralCode() {
        return success(leadReferralService.generateReferralCode());
    }

    @Operation(summary = "根据推荐码获取推荐人信息")
    @GetMapping("/referrer/{code}")
    public ApiResult<?> getReferrerByCode(
            @Parameter(description = "推荐码") @PathVariable String code) {
        return success(leadReferralService.getReferrerByCode(code));
    }

    @Operation(summary = "确认推荐有效（管理员）")
    @PutMapping("/confirm/{referralId}")
    @PreAuthorize("@pms.hasPermission('referral:confirm')")
    public ApiResult<?> confirmReferral(
            @Parameter(description = "推荐ID") @PathVariable Integer referralId) {
        return success(leadReferralService.confirmReferral(referralId));
    }

    @Operation(summary = "作废推荐（管理员）")
    @PutMapping("/invalidate/{referralId}")
    @PreAuthorize("@pms.hasPermission('referral:invalidate')")
    public ApiResult<?> invalidateReferral(
            @Parameter(description = "推荐ID") @PathVariable Integer referralId,
            @Parameter(description = "原因") @RequestParam(required = false) String reason) {
        return success(leadReferralService.invalidateReferral(referralId, reason));
    }

    @Operation(summary = "结算推荐费（管理员）")
    @PutMapping("/settle/{referralId}")
    @PreAuthorize("@pms.hasPermission('referral:settle')")
    public ApiResult<?> settleReferral(
            @Parameter(description = "推荐ID") @PathVariable Integer referralId) {
        return success(leadReferralService.settleReferral(referralId));
    }

    @Operation(summary = "批量结算推荐费")
    @PutMapping("/settle/batch")
    @PreAuthorize("@pms.hasPermission('referral:settle')")
    public ApiResult<?> batchSettleReferrals(@RequestBody Integer[] referralIds) {
        return success(leadReferralService.batchSettleReferrals(referralIds));
    }
}
