import type { Database } from '../../supabase/database.types'
import type { OriginResourceType } from '../core/types'

// Import Supabase types for new task_schedules table
export type TaskScheduleRow = Database['public']['Tables']['task_schedules']['Row']
export type TaskScheduleInsert = Database['public']['Tables']['task_schedules']['Insert']
export type TaskScheduleUpdate = Database['public']['Tables']['task_schedules']['Update']

/**
 * Target for schedule execution - identifies what resource to execute.
 * Unlike ExecutionTarget, payload is NOT included here because schedules
 * store payload in the scheduleConfig (varies per step/item).
 */
export interface ScheduleTarget {
  resourceType: 'agent' | 'workflow'
  resourceId: string
}

/**
 * Optional origin tracking for schedules.
 * Unlike OriginTracking (which is required), these fields are all optional
 * for schedules created directly via API (not triggered by another resource).
 */
export interface ScheduleOriginTracking {
  originExecutionId?: string
  originResourceType?: OriginResourceType
  originResourceId?: string
}

// Task schedule config discriminated union
// Named TaskScheduleConfig to avoid conflict with ExecutionInterface's ScheduleConfig
export type TaskScheduleConfig = RecurringScheduleConfig | RelativeScheduleConfig | AbsoluteScheduleConfig

export interface RecurringScheduleConfig {
  type: 'recurring'
  cron?: string
  interval?: 'daily' | 'weekly' | 'monthly'
  time?: string
  timezone: string
  payload: Record<string, unknown>
  endAt?: string | null
  overduePolicy?: 'skip' | 'execute' // Default: 'skip'
}

export interface RelativeScheduleConfig {
  type: 'relative'
  anchorAt: string
  anchorLabel?: string
  items: RelativeScheduleItem[]
  overduePolicy?: 'skip' | 'execute' // Default: 'skip'
}

export interface RelativeScheduleItem {
  offset: string // '-7d', '+3d', '-2h', '+1h'
  payload: Record<string, unknown>
  label?: string
}

export interface AbsoluteScheduleConfig {
  type: 'absolute'
  items: AbsoluteScheduleItem[]
  overduePolicy?: 'skip' | 'execute' // Default: 'skip'
}

export interface AbsoluteScheduleItem {
  runAt: string
  payload: Record<string, unknown>
  label?: string
}

// Domain type (camelCase)
export interface TaskSchedule extends ScheduleOriginTracking {
  id: string
  organizationId: string
  name: string
  description?: string

  target: ScheduleTarget
  scheduleConfig: TaskScheduleConfig

  nextRunAt?: Date
  currentStep: number
  status: 'active' | 'paused' | 'completed' | 'cancelled'

  lastRunAt?: Date
  lastExecutionId?: string

  maxRetries: number
  idempotencyKey?: string
  createdAt: Date
  updatedAt: Date
}

export interface CreateScheduleInput extends ScheduleOriginTracking {
  organizationId: string
  name: string
  description?: string

  target: ScheduleTarget
  scheduleConfig: TaskScheduleConfig

  maxRetries?: number
  idempotencyKey?: string
  metadata?: Record<string, unknown>
}

export interface BatchProcessResult {
  processed: number
  succeeded: number
  failed: number
}
