package com.moka.gdtauto.task;

import com.moka.gdtauto.service.DailyReportService;
import com.tencent.ads.model.v3.TimeLine;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 动态创意日报表定时同步任务
 * 支持双口径（REQUEST_TIME / REPORTING_TIME）
 *
 * @author moka
 * @since 2026-03-03
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class DailyReportSyncTask {

    private final DailyReportService dailyReportService;

    private static final DateTimeFormatter FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    // ─── 同步当日数据 ────────────────────────────────────────────────────────────

    /**
     * 同步当日 REQUEST_TIME 口径日报表
     * cron: 每小时的 2,7,12,17,22,27,32,37,42,47,52,57 分执行
     */
    @Scheduled(cron = "0 2,7,12,17,22,27,32,37,42,47,52,57 * * * *")
    @SchedulerLock(name = "syncTodayReqDailyReport", lockAtMostFor = "4m", lockAtLeastFor = "1m")
    public void syncTodayRequestTime() {
        String today = LocalDate.now().format(FMT);
        log.info("[定时任务] 同步当日 REQUEST_TIME 日报表 date={}", today);
        try {
            int total = dailyReportService.syncAllAccounts(today, today, TimeLine.REQUEST_TIME.getValue());
            log.info("[定时任务] 当日 REQUEST_TIME 日报表同步完成 total={}", total);
        } catch (Exception e) {
            log.error("[定时任务] 当日 REQUEST_TIME 日报表同步异常", e);
        }
    }

    /**
     * 同步当日 REPORTING_TIME 口径日报表
     * cron: 每小时的 2,7,12,17,22,27,32,37,42,47,52,57 分执行（与 REQUEST_TIME 错开 30s 避免并发）
     */
    @Scheduled(cron = "30 2,7,12,17,22,27,32,37,42,47,52,57 * * * *")
    @SchedulerLock(name = "syncTodayRepDailyReport", lockAtMostFor = "4m", lockAtLeastFor = "1m")
    public void syncTodayReportingTime() {
        String today = LocalDate.now().format(FMT);
        log.info("[定时任务] 同步当日 REPORTING_TIME 日报表 date={}", today);
        try {
            int total = dailyReportService.syncAllAccounts(today, today, TimeLine.REPORTING_TIME.getValue());
            log.info("[定时任务] 当日 REPORTING_TIME 日报表同步完成 total={}", total);
        } catch (Exception e) {
            log.error("[定时任务] 当日 REPORTING_TIME 日报表同步异常", e);
        }
    }

    // ─── 同步昨日数据 ────────────────────────────────────────────────────────────

    /**
     * 同步昨日 REQUEST_TIME 口径日报表
     * cron: 每天 0,5,8,11,13,15 点的第 3 分钟执行
     */
    @Scheduled(cron = "0 3 0,5,8,11,13,15 * * *")
    @SchedulerLock(name = "syncYesterdayReqDailyReport", lockAtMostFor = "9m", lockAtLeastFor = "2m")
    public void syncYesterdayRequestTime() {
        String yesterday = LocalDate.now().minusDays(1).format(FMT);
        log.info("[定时任务] 同步昨日 REQUEST_TIME 日报表 date={}", yesterday);
        try {
            int total = dailyReportService.syncAllAccounts(yesterday, yesterday, TimeLine.REQUEST_TIME.getValue());
            log.info("[定时任务] 昨日 REQUEST_TIME 日报表同步完成 total={}", total);
        } catch (Exception e) {
            log.error("[定时任务] 昨日 REQUEST_TIME 日报表同步异常", e);
        }
    }

    /**
     * 同步昨日 REPORTING_TIME 口径日报表
     * cron: 每天 0,5,8,11,13,15 点的第 3 分 30 秒执行
     */
    @Scheduled(cron = "30 3 0,5,8,11,13,15 * * *")
    @SchedulerLock(name = "syncYesterdayRepDailyReport", lockAtMostFor = "9m", lockAtLeastFor = "2m")
    public void syncYesterdayReportingTime() {
        String yesterday = LocalDate.now().minusDays(1).format(FMT);
        log.info("[定时任务] 同步昨日 REPORTING_TIME 日报表 date={}", yesterday);
        try {
            int total = dailyReportService.syncAllAccounts(yesterday, yesterday, TimeLine.REPORTING_TIME.getValue());
            log.info("[定时任务] 昨日 REPORTING_TIME 日报表同步完成 total={}", total);
        } catch (Exception e) {
            log.error("[定时任务] 昨日 REPORTING_TIME 日报表同步异常", e);
        }
    }
}
