// ABOUTME: Configuration for all scheduled jobs (cron expressions and settings)
// ABOUTME: Defines morning scrape, pre-race, post-race, and cleanup job schedules

import { ScheduleType, SchedulerConfig } from './types';

export const SCHEDULER_CONFIGS: Record<ScheduleType, SchedulerConfig> = {
  /**
   * Morning Scrape - Daily at 6 AM AEDT/AEST
   *
   * Fetches all meetings for today and tomorrow
   * - Australian thoroughbred and harness meetings
   * - NZ thoroughbred and harness meetings
   * - Prepares database for the day's racing
   */
  [ScheduleType.MORNING_SCRAPE]: {
    cronExpression: '0 6 * * *',
    timezone: 'Pacific/Auckland',
    enabled: true,
    description: 'Daily morning scrape of all meetings for today and tomorrow',
  },

  /**
   * Pre-Race Update T-60 (60 minutes before)
   *
   * Runs every 5 minutes to check for races starting in ~60 minutes
   * - Updates field information
   * - Captures opening odds
   * - Validates race details
   */
  [ScheduleType.PRE_RACE_T60]: {
    cronExpression: '*/5 * * * *', // Every 5 minutes
    timezone: 'Pacific/Auckland',
    enabled: true,
    description: 'Pre-race update 60 minutes before scheduled start',
  },

  /**
   * Pre-Race Update T-15 (15 minutes before)
   *
   * Runs every 5 minutes to check for races starting in ~15 minutes
   * - Final field check
   * - Captures late scratchings
   * - Updates current odds
   */
  [ScheduleType.PRE_RACE_T15]: {
    cronExpression: '*/5 * * * *', // Every 5 minutes
    timezone: 'Pacific/Auckland',
    enabled: true,
    description: 'Pre-race update 15 minutes before scheduled start',
  },

  /**
   * Post-Race Results T+5 (5 minutes after)
   *
   * Runs every 5 minutes to check for races that finished recently
   * - Fetches official results
   * - Records finishing positions
   * - Stores final dividends
   * - Handles provisional -> confirmed transition
   */
  [ScheduleType.POST_RACE_T5]: {
    cronExpression: '*/5 * * * *', // Every 5 minutes
    timezone: 'Pacific/Auckland',
    enabled: true,
    description: 'Post-race results checking (handles provisional->confirmed)',
  },

  /**
   * Odds Update - Every 30 seconds during racing hours
   *
   * Frequent odds updates for races in the next 2 hours
   * - Only runs during peak racing times (7 AM - 10 PM)
   * - Captures odds movements
   * - Stores odds history
   */
  [ScheduleType.ODDS_UPDATE]: {
    cronExpression: '*/30 7-22 * * *', // Every 30 seconds, 7 AM - 10 PM
    timezone: 'Pacific/Auckland',
    enabled: false, // Start disabled, enable when odds tracking is implemented
    description: 'Frequent odds updates during racing hours',
  },

  /**
   * JobRun Cleanup - Daily at 2 AM
   *
   * Cleans up old JobRun records to prevent database bloat
   * - Deletes successful jobs older than 2 weeks
   * - Keeps failed/partial jobs indefinitely for debugging
   */
  [ScheduleType.CLEANUP_JOB_RUNS]: {
    cronExpression: '0 2 * * *', // Daily at 2 AM
    timezone: 'Pacific/Auckland',
    enabled: true,
    description: 'Clean up old JobRun records (2-week retention for successful jobs)',
  },
};

/**
 * Time windows for pre-race and post-race jobs
 * Windows are wider to accommodate 5-minute check intervals
 */
export const TIME_WINDOWS = {
  PRE_RACE_T60: {
    minutesBefore: 60,
    windowMinutes: 10, // Check races starting in 55-70 minutes (covers 5-min intervals)
  },
  PRE_RACE_T15: {
    minutesBefore: 15,
    windowMinutes: 10, // Check races starting in 10-25 minutes
  },
  POST_RACE_INITIAL: {
    minutesAfter: 10,
    windowMinutes: 10, // Check races finished 5-20 minutes ago (provisional results)
  },
  POST_RACE_CONFIRMED: {
    minutesAfter: 15,
    windowMinutes: 15, // Check races finished 10-30 minutes ago (confirmed results)
  },
};

/**
 * Morning scrape configuration with retry strategy
 */
export const MORNING_SCRAPE_CONFIG = {
  categories: ['T', 'H'] as const, // Thoroughbred and Harness
  countries: ['AUS', 'NZ'] as const,
  daysToFetch: 2, // Today and tomorrow
  retrySchedule: [
    { hour: 6, minute: 15 },  // 6:15 AM - First retry
    { hour: 6, minute: 30 },  // 6:30 AM - Second retry
    { hour: 7, minute: 0 },   // 7:00 AM - Third retry
  ],
  maxRetries: 3,
};

/**
 * Retry configuration for race-specific jobs
 */
export const RETRY_CONFIG = {
  PRE_RACE: {
    maxRetries: 2,
    retryDelays: [60000, 120000], // 1 min, 2 min
  },
  POST_RACE: {
    maxRetries: 3,
    retryDelays: [300000, 300000, 600000], // 5 min, 5 min, 10 min (to catch provisional->confirmed)
  },
};
