/*
 * The MIT License (MIT)
 *
 * Copyright © 2021 xrv <xrg@live.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.tzld.piaoquan.ad.engine.server.controller;

import com.tzld.piaoquan.ad.engine.commons.feign.manager.service.HolidayService;
import com.tzld.piaoquan.ad.engine.commons.feign.manager.vo.HolidayCalendarVO;
import com.tzld.piaoquan.ad.engine.service.predict.impl.PredictModelServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 节日查询测试控制器
 *
 * @author ad-engine
 * @since 2025-08-22
 */
@Slf4j
@RestController
@RequestMapping("/test/holiday")
@Api(tags = "节日查询测试接口")
public class HolidayTestController {
    
    @Autowired
    private HolidayService holidayService;

    @Autowired
    private PredictModelServiceImpl predictModelService;
    

    /**
     * 健康检查接口
     */
    @GetMapping("/health")
    @ApiOperation("节日服务健康检查")
    public Map<String, Object> healthCheck() {
        Map<String, Object> result = new HashMap<>();

        try {
            log.info("开始节日服务健康检查，实际请求路径: /manager/platform/holidayCalendar/isHoliday");

            boolean isHoliday = holidayService.isTodayHoliday();
            result.put("status", "healthy");
            result.put("service", "holiday-service");
            result.put("context_path_info", "manager服务使用context-path=/manager");
            result.put("feign_path", "/manager/platform/holidayCalendar/isHoliday");
            result.put("timestamp", System.currentTimeMillis());
            result.put("test_result", isHoliday ? "有节日数据" : "无节日数据");

        } catch (Exception e) {
            log.error("节日服务健康检查失败，请检查context-path配置", e);
            result.put("status", "unhealthy");
            result.put("service", "holiday-service");
            result.put("context_path_info", "manager服务使用context-path=/manager");
            result.put("feign_path", "/manager/platform/holidayCalendar/isHoliday");
            result.put("timestamp", System.currentTimeMillis());
            result.put("error", e.getMessage());
            result.put("troubleshooting", "检查Feign客户端path是否包含/manager前缀");
        }

        return result;
    }

    /**
     * 测试早晨节日时间判断函数
     */
    @GetMapping("/earlyMorningHoliday")
    @ApiOperation("测试6-8点节日时间判断")
    public Map<String, Object> testEarlyMorningHoliday() {
        Map<String, Object> result = new HashMap<>();

        try {
            // 调用新增的函数
            boolean isEarlyMorningHoliday = predictModelService.isEarlyMorningHoliday();

            // 获取当前时间信息用于调试
            int currentHour = com.tzld.piaoquan.ad.engine.commons.util.DateUtils.getCurrentHour();
            boolean isHoliday = holidayService.isTodayHoliday();
            String holidayName = "不是节日";
            if (isHoliday) {
                holidayName = holidayService.getTodayHoliday().getHolidayName();
            }
            result.put("success", true);
            result.put("isEarlyMorningHoliday", isEarlyMorningHoliday);
            result.put("currentHour", currentHour);
            result.put("isInTimeRange", currentHour >= 6 && currentHour < 8);
            result.put("isHoliday", isHoliday);
            result.put("holidayName", holidayName);
            result.put("timestamp", System.currentTimeMillis());

            // 详细说明
            if (isEarlyMorningHoliday) {
                result.put("message", String.format("当前时间%d点在6-8点范围内且今日是节日(%s)，返回true",
                    currentHour, holidayName != null ? holidayName : "未知节日"));
            } else if (currentHour >= 6 && currentHour < 8) {
                result.put("message", String.format("当前时间%d点在6-8点范围内但今日不是节日，返回false", currentHour));
            } else {
                result.put("message", String.format("当前时间%d点不在6-8点范围内，返回false", currentHour));
            }

            log.info("早晨节日时间判断测试结果: {}", isEarlyMorningHoliday);

        } catch (Exception e) {
            log.error("早晨节日时间判断测试失败", e);
            result.put("success", false);
            result.put("error", e.getMessage());
            result.put("isEarlyMorningHoliday", false);
        }

        return result;
    }
}
