package com.ai.da.controller;


import com.ai.da.common.response.PageBaseResponse;
import com.ai.da.common.response.Response;
import com.ai.da.model.dto.AffiliateQueryDTO;
import com.ai.da.model.dto.EditReferralDTO;
import com.ai.da.model.dto.ReferralPageQueryDTO;
import com.ai.da.model.vo.AffiliateInvitationDetailsVO;
import com.ai.da.model.vo.AffiliateVO;
import com.ai.da.model.vo.ReferralPageQueryVO;
import com.ai.da.service.AffiliateService;
import com.ai.da.service.ReferralService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/affiliate")
@Api(tags = "Affiliate模块")
public class AffiliateController {

    @Resource
    private AffiliateService affiliateService;

    @Resource
    private ReferralService referralService;

    @ApiOperation(value = "注册成为affiliate")
    @GetMapping("/registration")
    public Response<Boolean> completeGuidance(@RequestParam(value = "promotionMethod", required = false) String promotionMethod) {
        return Response.success(affiliateService.registerAsAnAffiliate(promotionMethod));
    }

    @ApiOperation(value = "获取affiliate列表")
    @PostMapping("/list")
    public Response<PageBaseResponse<AffiliateVO>> getAffiliateList(@Validated @RequestBody AffiliateQueryDTO affiliateQueryDTO) {
        return Response.success(affiliateService.getAffiliateList(affiliateQueryDTO));
    }

    @ApiOperation(value = "获取affiliate个人中心")
    @GetMapping("/personalCenter")
    public Response<AffiliateVO> personalAffiliateCenter() {
        return Response.success(affiliateService.personalAffiliateCenter());
    }

    @ApiOperation(value = "获取个人佣金图表数据")
    @GetMapping("/getPersonalMonthlyIncome")
    public Response<BigDecimal[]> getPersonalMonthlyIncome(@RequestParam("year") int year) {
        return Response.success(affiliateService.getPersonalMonthlyIncome(year));
    }

    @ApiOperation(value = "审批affiliate申请")
    @GetMapping("/approval")
    public Response<Boolean> applicationApproval(@RequestParam("id") Long id,
                                                 @RequestParam("isApproved") Boolean isApproved,
                                                 @RequestParam(value = "commission", required = false) Float commission) {
        return Response.success(affiliateService.applicationApproval(id, isApproved, commission));
    }

    @ApiOperation(value = "编辑affiliate")
    @GetMapping("/editAffiliate")
    public Response<String> editAffiliate(@RequestParam("id") Long id,
                                          @ApiParam(value = "佣金比例", example = "25")
                                          @RequestParam(value = "commission", required = false) Float commission,
                                          @ApiParam(value = "操作类型", example = "Active",
                                                  allowableValues = "Active,Inactive,Delete")
                                          @RequestParam(value = "operationType", required = false) String operationType) {
        affiliateService.editAffiliate(id, commission, operationType);
        return Response.success("success");
    }

    /*@ApiOperation(value = "定时计算佣金")
    @GetMapping("/testTask")
    public Response<String> testTask() {
        affiliateService.updateAffiliateInfoWithPayment();
        return Response.success("success ");
    }*/

    /*@ApiOperation(value = "每月发送结算邮件")
    @GetMapping("/commissionCalculation")
    public Response<String> commissionCalculation(@RequestParam("year") Integer year, @RequestParam("month") Integer month) {
        affiliateService.commissionCalculation(year, month);
        return Response.success("success ");
    }*/

    @ApiOperation(value = "affiliate链接浏览量增加")
    @GetMapping("/viewsIncrease")
    public Response<Boolean> viewsGet(@RequestParam("id") Long id) {
        return Response.success(affiliateService.affiliateLinkViewsIncrease(id));
    }

    @ApiOperation(value = "获取每个affiliate产生的收入")
    @PostMapping("/getEachAffiliateGeneratedRevenue")
    public Response<IPage<AffiliateInvitationDetailsVO>> getEachAffiliateGeneratedRevenue(@Validated @RequestBody AffiliateQueryDTO affiliateQueryDTO) {
        return Response.success(affiliateService.getEachAffiliateGeneratedRevenue(affiliateQueryDTO));
    }

    @ApiOperation(value = "分页获取所有的referral")
    @PostMapping("/getReferrals")
    public Response<IPage<ReferralPageQueryVO>> getReferrals(@RequestBody ReferralPageQueryDTO referralPageQueryDTO) {
        return Response.success(referralService.queryByPage(referralPageQueryDTO));
    }

    @ApiOperation(value = "编辑单个referral")
    @PostMapping("/editReferral")
    public Response<String> editReferral(@Validated @RequestBody EditReferralDTO editReferralDTO) {
        referralService.editReferral(editReferralDTO);
        return Response.success();
    }

    @ApiOperation(value = "（批量）删除referral")
    @PostMapping("/batchDeleteReferral")
    public Response<String> batchDeleteReferral(@RequestBody List<Long> idList) {
        referralService.deleteReferral(idList);
        return Response.success();
    }

    @ApiOperation(value = "获取所有affiliate用户名")
    @GetMapping("/getAllAffiliateUsername")
    public Response<List<Map<String, Object>>> getAllAffiliateUsername() {
        return Response.success(affiliateService.getAllAffiliateUsername());
    }

}
