package com.bizzan.bitrade.controller.activity;

import com.bizzan.bitrade.annotation.AccessLog;
import com.bizzan.bitrade.constant.AdminModule;
import com.bizzan.bitrade.controller.common.BaseAdminController;
import com.bizzan.bitrade.entity.Admin;
import com.bizzan.bitrade.service.ExperienceGiftProductService;
import com.bizzan.bitrade.util.MessageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import com.bizzan.bitrade.entity.ExperienceGiftProduct;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 体验金产品管理接口
 */
@RestController
@RequestMapping("activity/experience-gift/product")
public class ExperienceGiftProductController extends BaseAdminController {

    @Autowired
    private ExperienceGiftProductService productService;

    /**
     * 启用的手动赠送产品列表（用于赠送下拉框）
     */
    @GetMapping("list-manual")
    @AccessLog(module = AdminModule.ACTIVITY, operation = "手动赠送产品列表")
    public MessageResult listManual() {
        List<ExperienceGiftProduct> list = productService.findManualEnabled();
        return success(list);
    }

    /**
     * 分页列表
     */
    @PostMapping("page-query")
    @AccessLog(module = AdminModule.ACTIVITY, operation = "体验金产品列表查询")
    public MessageResult pageQuery(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) Integer grantType,
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "15") int pageSize) {

        Page<Map> page = productService.pageQuery(name, status, grantType, pageNo, pageSize);
        return success(page);
    }

    /**
     * 产品详情（用于编辑回填）
     */
    @GetMapping("detail/{id}")
    @AccessLog(module = AdminModule.ACTIVITY, operation = "体验金产品详情")
    public MessageResult detail(@PathVariable Long id) {
        Map<String, Object> detail = productService.getDetail(id);
        if (detail == null) return error("产品不存在");
        return success(detail);
    }

    /**
     * 新增 / 修改产品
     */
    @PostMapping("save")
    @AccessLog(module = AdminModule.ACTIVITY, operation = "体验金产品保存")
    public MessageResult save(
            @RequestParam(required = false) Long id,
            @RequestParam String name,
            @RequestParam BigDecimal amount,
            @RequestParam Integer validityDays,
            @RequestParam(defaultValue = "2") Integer leverage,
            @RequestParam(required = false) Integer totalQuantity,
            @RequestParam(required = false) Integer perUserLimit,
            @RequestParam(required = false) BigDecimal maxProfitPerCard,
            @RequestParam(defaultValue = "0") Integer grantType,
            @RequestParam(required = false) Integer triggerType,
            @RequestParam(required = false) Boolean hasMinRecharge,
            @RequestParam(required = false) BigDecimal minRechargeAmount,
            @RequestParam(required = false) Boolean hasMinOpenValue,
            @RequestParam(required = false) BigDecimal minOpenValue,
            @RequestParam(required = false) Boolean hasMinHoldMinutes,
            @RequestParam(required = false) Integer minHoldMinutes,
            @RequestParam(required = false) String activityStartTime,
            @RequestParam(required = false) String activityEndTime,
            @RequestParam(required = false) String coins,
            @RequestParam(defaultValue = "0") Integer status,
            HttpServletRequest request) {

        Admin admin = getAdmin(request);
        if (admin == null) return error("请先登录");

        List<String> coinList = null;
        if (coins != null && !coins.trim().isEmpty()) {
            coinList = Arrays.asList(coins.split(","));
        }

        Long savedId = productService.adminSave(id, name, amount, validityDays, leverage,
                totalQuantity, perUserLimit, maxProfitPerCard,
                grantType, triggerType,
                hasMinRecharge, minRechargeAmount,
                hasMinOpenValue, minOpenValue,
                hasMinHoldMinutes, minHoldMinutes,
                activityStartTime, activityEndTime,
                coinList, status, admin.getId());

        return success(savedId);
    }
}
