package com.adx.tracking.conversionsync;

import com.adx.tracking.baidu.model.ConversionQuery;
import com.adx.tracking.storage.TiDBColdStore;
import com.adx.tracking.storage.model.MediaCallbackRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Instant;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 对应 Go internal/conversionsync/runner.go 的 Runner。
 * 定期调用 ConversionSyncService.syncKuaishouConversions()。
 * 在 Spring 中通过 @Scheduled 触发（见 AppScheduler）。
 */
public class ConversionSyncRunner {

    private static final Logger log = LoggerFactory.getLogger(ConversionSyncRunner.class);
    private static final DateTimeFormatter DATE_FMT = DateTimeFormatter.ofPattern("yyyyMMdd");

    private final ConversionSyncService syncer;
    private final int dateOffsetDays;
    private final int pageSize;
    private final List<Integer> acts;

    public ConversionSyncRunner(ConversionSyncService syncer,
                                 int dateOffsetDays, int pageSize, List<Integer> acts) {
        this.syncer         = syncer;
        this.dateOffsetDays = dateOffsetDays;
        this.pageSize       = pageSize > 0 ? pageSize : 1;
        this.acts           = acts;
    }

    /**
     * 对应 Go Runner.RunOnce()。
     */
    public ConversionSyncService.SyncResult runOnce() throws Exception {
        String date = LocalDate.now().plusDays(dateOffsetDays).format(DATE_FMT);
        ConversionQuery query = new ConversionQuery();
        query.setDate(date);
        query.setPageSize(pageSize);
        query.setActs(acts);
        return syncer.syncKuaishouConversions(query);
    }
}

// ─── RetryService ─────────────────────────────────────────────────────────────

// 单独的 RetryService 类，和 Go internal/conversionsync/retry.go 对应：
// 从 TiDB 查出未成功的 Kuaishou 回调，逐条重试。
