package com.moka.gdtauto.task;

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

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;

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

    private final DailyAdgroupReportService dailyAdgroupReportService;

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

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

    /**
     * 同步当日 REQUEST_TIME 口径日报表
     * cron: 每小时
     */
    @Scheduled(cron = "0 10 * * * *")
    @SchedulerLock(name = "syncTodayReqDailyAdgroupReport", lockAtMostFor = "4m", lockAtLeastFor = "1m")
    public void syncTodayRequestTime() {
        String today = LocalDate.now().format(FMT);
        log.info("[定时任务] 广告同步当日 REQUEST_TIME 日报表 date={}", today);
        try {
            dailyAdgroupReportService.syncAllAccounts(null, today, today, TimeLine.REQUEST_TIME.getValue());
            log.info("[定时任务] 广告当日 REQUEST_TIME 日报表同步完成");
        } catch (Exception e) {
            log.error("[定时任务] 广告当日 REQUEST_TIME 日报表同步异常", e);
        }
    }

    /**
     * 同步当日 REPORTING_TIME 口径日报表
     * cron: 每小时的1分执行（与 REQUEST_TIME 错开 30s 避免并发）
     */
    @Scheduled(cron = "30 1 * * * *")
    @SchedulerLock(name = "syncTodayRepDailyAdgroupReport", lockAtMostFor = "4m", lockAtLeastFor = "1m")
    public void syncTodayReportingTime() {
        String today = LocalDate.now().format(FMT);
        log.info("[定时任务] 广告同步当日 REPORTING_TIME 日报表 date={}", today);
        try {
            dailyAdgroupReportService.syncAllAccounts(null, today, today, TimeLine.REPORTING_TIME.getValue());
            log.info("[定时任务] 广告当日 REPORTING_TIME 日报表同步完成");
        } catch (Exception e) {
            log.error("[定时任务] 广告当日 REPORTING_TIME 日报表同步异常", e);
        }
    }

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

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

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