package com.zsElectric.boot.business.controller.applet;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zsElectric.boot.business.model.entity.UserFeedback;
import com.zsElectric.boot.business.model.form.UserFeedbackForm;
import com.zsElectric.boot.business.model.form.applet.AppFeedbackForm;
import com.zsElectric.boot.business.model.form.applet.AppGainCouponForm;
import com.zsElectric.boot.business.model.query.CouponTemplateQuery;
import com.zsElectric.boot.business.model.query.applet.AppCouponQuery;
import com.zsElectric.boot.business.model.vo.CouponTemplateVO;
import com.zsElectric.boot.business.model.vo.CouponVO;
import com.zsElectric.boot.business.model.vo.applet.AppCouponStatusNumVO;
import com.zsElectric.boot.business.model.vo.applet.AppUserInfoVO;
import com.zsElectric.boot.business.service.CouponService;
import com.zsElectric.boot.business.service.CouponTemplateService;
import com.zsElectric.boot.business.service.UserFeedbackService;
import com.zsElectric.boot.business.service.UserInfoService;
import com.zsElectric.boot.common.annotation.Log;
import com.zsElectric.boot.common.constant.SystemConstants;
import com.zsElectric.boot.common.enums.LogModuleEnum;
import com.zsElectric.boot.core.web.Result;
import com.zsElectric.boot.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户相关接口")
@RestController
@RequestMapping("/applet/v1/user")
@RequiredArgsConstructor
public class AppletUserController {

    private final UserInfoService userInfoService;

    private final UserFeedbackService userFeedbackService;

    private final CouponService couponService;

    private final CouponTemplateService couponTemplateService;

    @Operation(summary = "微信小程序获取当前登录信息")
    @GetMapping("/getUserInfo")
    public Result<AppUserInfoVO> getUserInfo() {
        AppUserInfoVO userInfoVO = userInfoService.getAppletUserInfo();
        return Result.success(userInfoVO);
    }

    @Operation(summary = "用户绑定企业")
    @PostMapping("/userBinding/{userId}/{firmId}")
    @Log(value = "用户绑定企业", module = LogModuleEnum.APP_OTHER)
    public Result<Boolean> userBinding(@PathVariable("userId") Long userId, @PathVariable("firmId") Long firmId) {
        Boolean result = userInfoService.userBinding(userId, firmId);
        return Result.judge(result);
    }

    @Operation(summary = "小程序用户反馈")
    @PostMapping("/addUserFeedback")
    @Log(value = "小程序用户反馈", module = LogModuleEnum.APP_OTHER)
    public Result<Void> addUserFeedback(@RequestBody @Valid AppFeedbackForm formData ) {
        UserFeedbackForm userFeedbackForm = new UserFeedbackForm();
        userFeedbackForm.setType(formData.getType());
        userFeedbackForm.setUserId(SecurityUtils.getUserId());
        userFeedbackForm.setDescription(formData.getDescription());
        userFeedbackForm.setImages(formData.getImages());
        userFeedbackForm.setContactWay(formData.getContactWay());
        boolean result = userFeedbackService.saveUserFeedback(userFeedbackForm);
        return Result.judge(result);
    }

    @Operation(summary = "用户反馈答复")
    @GetMapping("/myFeedback")
    @Log(value = "用户反馈答复", module = LogModuleEnum.OTHER)
    public Result<List<UserFeedback>> addUserFeedback() {
        List<UserFeedback> list = userFeedbackService.list(Wrappers.<UserFeedback>lambdaQuery().eq(UserFeedback::getUserId, SecurityUtils.getUserId()));
        return Result.success(list);
    }

    @Operation(summary = "个人优惠券列表")
    @PostMapping("/getCouponPage")
    public Result<IPage<CouponVO>> getCouponPage(@RequestBody AppCouponQuery queryParams) {
        IPage<CouponVO> result = couponService.getUserCouponPage(queryParams);
        return Result.success(result);
    }

    @Operation(summary = "获取个人优惠券数量")
    @GetMapping("/getCouponStatusNum")
    public Result<AppCouponStatusNumVO> getCouponStatusNum() {
        AppCouponStatusNumVO result = couponService.getCouponStatusNum(SecurityUtils.getUserId());
        return Result.success(result);
    }

    @Operation(summary = "待领取优惠券列表")
    @GetMapping("/getCouponTemplatePage")
    public Result<IPage<CouponTemplateVO>> getAppCouponTemplatePage() {
        CouponTemplateQuery CouponTemplateQuery = new CouponTemplateQuery();
        CouponTemplateQuery.setStatus(SystemConstants.STATUS_ONE);
        IPage<CouponTemplateVO> result = couponTemplateService.getCouponTemplatePage(CouponTemplateQuery);
        return Result.success(result);
    }

    @Operation(summary = "领取优惠券")
    @PostMapping("/gainCoupon")
    @Log(value = "领取优惠券", module = LogModuleEnum.APP_OTHER)
    public Result<Boolean> gainCoupon(@RequestBody AppGainCouponForm formData) {
        Boolean result = couponService.gainCoupon(formData);
        return Result.judge(result);
    }
}
