package com.ai.da.controller;


import com.ai.da.common.context.UserContext;
import com.ai.da.common.response.PageBaseResponse;
import com.ai.da.common.response.Response;
import com.ai.da.mapper.primary.DesignMapper;
import com.ai.da.mapper.primary.entity.Account;
import com.ai.da.mapper.primary.entity.Organization;
import com.ai.da.mapper.primary.entity.TrialOrder;
import com.ai.da.model.dto.*;
import com.ai.da.model.vo.PaymentInfoVO;
import com.ai.da.model.vo.QuestionnaireFeedbackVO;
import com.ai.da.model.vo.QuestionnaireVO;
import com.ai.da.model.vo.QueryUserConditionsVO;
import com.ai.da.service.AccountService;
import com.ai.da.service.ConvenientInquiryService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Nullable;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.*;

@Api(tags = "便利查询")
@Slf4j
@RestController
@RequestMapping("/api/inquiry")
public class ConvenientInquiryController {
    @Resource
    private DesignMapper designMapper;
    @Resource
    private ConvenientInquiryService convenientInquiryService;
    @Resource
    private AccountService accountService;


    @ApiOperation("获取当前所有试用用户")
    @PostMapping("/getTrial")
    public Response<IPage<TrialOrder>> getTrial(@Valid @RequestBody QueryUserConditionsVO queryUserConditionsVO) {
        return Response.success(convenientInquiryService.getTrial(queryUserConditionsVO));
    }

    @ApiOperation("获取指定时间区间内所有用户design的使用情况")
    @GetMapping("/getDesignStatistic")
    public Response<List<UserDesignStatisticDTO>> getDesignStatistic(@RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime,
                                                                     @RequestParam(required = false) List<Long> ids, @RequestParam(required = false) String email,
                                                                     @RequestParam(required = false) String organizationName) {
        /*Long accountId = UserContext.getUserHolder().getId();
        String userEmail = accountService.getById(accountId).getUserEmail();
        if (accountId.equals(31L) || accountId.equals(87L) || accountId.equals(83L)
                || accountId.equals(6L) || accountId.equals(4L) || accountId.equals(73L)
                || userEmail.equals("joho8228@hotmail.com")
                || userEmail.equals("chelseayu@code-create.com.hk")
                || userEmail.equals("cheungzt007@gmail.com")
        ) {
            if (StringUtil.isNullOrEmpty(startTime)) startTime = "2024-02-01 00:00:00";
            if (StringUtil.isNullOrEmpty(endTime)) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                Date date = new Date();
                endTime = simpleDateFormat.format(date);
            }
            if (!StringUtil.isNullOrEmpty(email)){
                email = email.trim();
            }
            List<UserDesignStatisticDTO> designStatistic = designMapper.getDesignStatistic(startTime, endTime, ids, email,"prsn", null);
            return Response.success(designStatistic);
        } else {
            return Response.fail("Sorry, you don't have permission");
        }*/
        return Response.success(convenientInquiryService.getDesignStatistic(startTime, endTime, ids, email, organizationName));
    }


    //调查问卷
    @ApiOperation("获取调查问卷统计详情")
    @GetMapping("/getQuestionnaireStatistic")
    public Response<QuestionnaireFeedbackVO> getQuestionnaire() {
        return Response.success(convenientInquiryService.getQuestionnaireInfo());
    }

    @ApiOperation("获取所有调查问卷")
    @GetMapping("/getAllQuestionnaire")
    public Response<List<QuestionnaireVO>> getAllQuestionnaire() {
        return Response.success(convenientInquiryService.getAllQuestionnaire());
    }

    @ApiOperation("获取近期新用户")
    @PostMapping("/recentNewUser")
    public Response<IPage<Account>> recentNewUser(@Valid @RequestBody QueryUserConditionsVO queryUserConditionsVO) {
        return Response.success(convenientInquiryService.recentNewUser(queryUserConditionsVO));
    }

    @ApiOperation("获取近期新用户图表数据")
    @GetMapping("/recentNewUserChart")
    public Response<Map<String, Object>> recentNewUserChart(@ApiParam(value = "startTime") @RequestParam @Nullable String startTime,
                                                            @ApiParam(value = "endTime") @RequestParam @Nullable String endTime,
                                                            @ApiParam("userType") @RequestParam Integer userType) {
        return Response.success(convenientInquiryService.recentNewUserChart(startTime, endTime, userType));
    }

    @ApiOperation("获取近期活跃用户")
    @PostMapping("/recentActiveUser")
    public Response<IPage<Account>> recentActiveUser(@Valid @RequestBody QueryUserConditionsVO queryUserConditionsVO) {
        return Response.success(convenientInquiryService.recentActiveUser(queryUserConditionsVO));
    }

    @ApiOperation("获取近期活跃用户图表数据")
    @GetMapping("/recentActiveUserChart")
    public Response<Integer> recentActiveUserChart(@ApiParam(value = "startTime") @RequestParam @Nullable String startTime,
                                                   @ApiParam(value = "endTime") @RequestParam @Nullable String endTime) {
        return Response.success(convenientInquiryService.recentActiveUserChart(startTime, endTime));
    }

    @ApiOperation("获取用户的各模块功能使用详情")
    @GetMapping("/getActiveUserFunc")
    public Response<Map<String, List<Object>>> getActiveUserFunc(@ApiParam(value = "startTime") @RequestParam @Nullable String startTime,
                                                                 @ApiParam(value = "endTime") @RequestParam @Nullable String endTime,
                                                                 @ApiParam("userIdList") @RequestParam(required = false) @Nullable List<Long> userIdList) {
        return Response.success(convenientInquiryService.getActiveUserFunc(startTime, endTime, userIdList));
    }

    @ApiOperation("试用用户到正式用户的转化率")
    @GetMapping("/conversionRate")
    public Response<Map<String, Object>> conversionRate(@ApiParam(value = "startTime") @RequestParam(required = false) @Nullable String startTime,
                                                        @ApiParam(value = "endTime") @RequestParam(required = false) @Nullable String endTime) {
        return Response.success(convenientInquiryService.conversionRate(startTime, endTime));
    }

    @ApiOperation("试用用户国家/城市分布")
    @GetMapping("/trialUserCountry")
    public Response<Map<String, List<Object>>> trialUserCountry(@ApiParam(value = "startTime") @RequestParam(required = false) @Nullable String startTime,
                                                                @ApiParam(value = "endTime") @RequestParam(required = false) @Nullable String endTime) {
        return Response.success(convenientInquiryService.trialUserCountry(startTime, endTime));
    }

    @ApiOperation("添加用户")
    @PostMapping("/addUser")
    public Response<Boolean> addUser(@Valid @RequestBody AccountAddDTO accountAddDTO) {
        Long userAccountId = UserContext.getUserHolder().getId();
        if (userAccountId.equals(31L) || userAccountId.equals(87L) || userAccountId.equals(83L)
                || userAccountId.equals(6L) || userAccountId.equals(4L) || userAccountId.equals(73L)
        ) {
            return Response.success(convenientInquiryService.addUser(accountAddDTO));
        } else {
            return Response.fail("Sorry, you don't have permission");
        }
    }

    @ApiOperation("修改用户信息")
    @PostMapping("/modifyUser")
    public Response<Boolean> modifyUser(@ApiParam(value = "用户id") @RequestParam @Nullable Long accountId,
                                        @ApiParam(value = "有效期截止时间的毫秒级unix格式") @RequestParam @Nullable Long validEndTime,
                                        @ApiParam(value = "用户类型 1/2/3/0 -> yearly/monthly/trial/visitor") @RequestParam @Nullable Integer systemUser,
                                        @ApiParam("积分") @RequestParam @Nullable Long credits) {
        Long userAccountId = UserContext.getUserHolder().getId();
        if (userAccountId.equals(31L) || userAccountId.equals(87L) || userAccountId.equals(83L)
                || userAccountId.equals(6L) || userAccountId.equals(4L) || userAccountId.equals(73L)
        ) {
            return Response.success(convenientInquiryService.modifyUser(accountId, validEndTime, systemUser, credits));
        } else {
            return Response.fail("Sorry, you don't have permission");
        }
    }

    @ApiOperation("获取用户信息")
    @PostMapping("/getUserInfo")
    public Response<IPage<Account>> getUserInfo(@Valid @RequestBody QueryUserConditionsVO queryUserConditionsVO) {
        return Response.success(convenientInquiryService.getUserInfo(queryUserConditionsVO));
    }

    @ApiOperation("获取所有用户id")
    @GetMapping("/getAllUserId")
    public Response<List<Map<String, Object>>> getAllUsrIdList() {
        return Response.success(convenientInquiryService.getAllUserIdList());
    }

    @ApiOperation("获取所有交易信息")
    @PostMapping("/queryTransaction")
    public Response<PageBaseResponse<PaymentInfoVO>>  queryTransactionRecords(@Valid @RequestBody QueryPaymentInfoDTO queryPaymentInfoDTO){
        return Response.success(convenientInquiryService.queryTransactionRecords(queryPaymentInfoDTO));
    }

    @ApiOperation("获取所有国家、城市")
    @GetMapping("/getCities")
    public Response<Map<String, List<String>>> getCities(){
        return Response.success(convenientInquiryService.getCities());
    }

    @ApiOperation("下载交易记录")
    @PostMapping("/queryTransaction/download")
    public Response<String> exportTransactionRecords(@Valid @RequestBody QueryPaymentInfoDTO queryPaymentInfoDTO, HttpServletResponse response){
        return Response.success(convenientInquiryService.exportTransactionRecords(queryPaymentInfoDTO, response));
    }

    @ApiOperation("获取生成功能使用频次")
    @PostMapping("/getGenerateFrequency")
    public Response<PageBaseResponse<AccountCreditsUsageDTO>> getGenerateFrequency(@Valid @RequestBody AccountCreditsUsageQueryDTO queryDTO){
       return Response.success(convenientInquiryService.getGenerateFrequency(queryDTO));
    }

    @ApiOperation("获取所有生成功能的名字")
    @GetMapping("/getAllGenerateFuncName")
    public Response<List<String>> getAllGenerateFuncName(){
        return Response.success(convenientInquiryService.getAllGenerateFuncName());
    }

    @ApiOperation("添加组织机构")
    @GetMapping("/addOrganization")
    public Response<String> addOrganization(@ApiParam(value = "机构名") @RequestParam String name, @ApiParam(value = "Enterprise || Education") @RequestParam String type){
        convenientInquiryService.addOrganization(name, type);
        return Response.success();
    }

    @ApiOperation("查询所有企业或教育机构")
    @PostMapping("/queryOrganization")
    public Response<IPage<Organization>> queryOrganization(@RequestBody QueryOrganizationPageDTO queryOrganizationPageDTO){
        return Response.success(convenientInquiryService.queryOrganization(queryOrganizationPageDTO));
    }

}
