// ABOUTME: TypeScript type definitions for TAB Affiliates API
// ABOUTME: Types inferred from Zod schemas and API specification

// ============================================================================
// Common Types
// ============================================================================

export interface ApiHeader {
  title: string;
  generated_time: string; // ISO 8601 timestamp
  url: string;
}

export interface ApiErrorResponse {
  error: {
    code: string;
    message: string;
  };
}

// ============================================================================
// Meetings API Types
// ============================================================================

export interface MeetingsListParams {
  enc: string;
  ids?: string[] | null;
  date_from: string;
  date_to: string;
  type?: string; // T | H | G
  country?: string;
  limit?: number;
  offset?: number;
  futures?: boolean;
  meeting_numbers?: number[] | null;
}

export interface RaceSummary {
  id: string;
  race_number: number;
  name: string;
  start_time: string; // ISO 8601 timestamp
  tote_start_time: string; // HH:MM:SS format
  track_condition: string;
  distance: number;
  weather: string;
  country: string;
  state: string;
  status: string; // e.g., "Final", "Open", "Closed"
}

export interface Meeting {
  meeting: string; // UUID
  name: string;
  date: string; // ISO 8601 date
  track_condition: string;
  category: string; // T | H | G
  category_name: string;
  country: string;
  state: string;
  races: RaceSummary[];
}

export interface MeetingsListResponse {
  header: ApiHeader;
  params: MeetingsListParams;
  data: {
    meetings: Meeting[];
  };
}

export interface MeetingByIdParams {
  enc: string;
  id: string;
}

export interface MeetingByIdResponse {
  header: ApiHeader;
  params: MeetingByIdParams;
  data: {
    meetings: Meeting[]; // Should contain exactly 1 meeting
  };
}

// ============================================================================
// Client Configuration
// ============================================================================

export interface TabApiClientConfig {
  baseUrl: string;
  timeout?: number;
  maxRetries?: number;
  retryDelay?: number;
  rateLimitPerMinute?: number;
}

// ============================================================================
// Request Options
// ============================================================================

export interface GetMeetingsOptions {
  category?: 'T' | 'H' | 'G';
  country?: string;
  dateFrom?: string; // YYYY-MM-DD or 'today' | 'week' | 'month' | 'all'
  dateTo?: string; // YYYY-MM-DD or 'today' | 'week' | 'month' | 'all'
  futures?: boolean;
  limit?: number;
  offset?: number;
}

// ============================================================================
// Race/Event Detail Types (GET /events/:id)
// ============================================================================

export interface RunnerOdds {
  fixed_win?: number;
  fixed_place?: number;
  pool_win?: number;
  pool_place?: number;
}

export interface OddsFluc {
  timestamp: string;
  fluc: number;
}

export interface FlucsWithTimestamp {
  last_six?: OddsFluc[];
  open?: OddsFluc;
  high?: OddsFluc;
  low?: OddsFluc;
  nine_am?: OddsFluc;
}

export interface RunnerWeight {
  allocated?: string;
  total?: string;
}

export interface PerformanceStats {
  number_of_starts: number;
  number_of_wins: number;
  number_of_placings: number;
  number_of_seconds: number;
  number_of_thirds: number;
  winning_distance?: number;
}

export interface RunnerDetail {
  entrant_id?: string;
  market_name?: string;
  primary_market?: boolean;
  name: string;
  is_scratched: boolean;
  gear?: string;
  odds?: RunnerOdds | null;
  scratch_time: number;
  barrier: number;
  runner_number: number;
  prize_money?: string;
  age?: number;
  sex?: string;
  colour?: string;
  silk_colours?: string;
  jockey?: string;
  apprentice_indicator?: string;
  allowance_weight?: string;
  owners?: string;
  country?: string;
  trainer_name?: string;
  trainer_location?: string;
  last_twenty_starts?: string;
  sire?: string;
  dam?: string;
  dam_sire?: string;
  breeding?: string;
  weight?: RunnerWeight | null;
  favourite?: boolean;
  mover?: boolean;
  horse_id?: number;                          // TAB's horse_id
  harness_nz_horse_id?: number | string;      // Harness Racing NZ horse ID (can be number or string from API)
  past_performances?: {
    jockey?: PerformanceStats;
  } | null;
  jockey_past_performances?: {
    last_50_starts?: PerformanceStats;
    trainer?: PerformanceStats;
  } | null;
  fresh_90?: PerformanceStats | null;
  flucs?: number[];
  flucs_with_timestamp?: FlucsWithTimestamp | null;
  scr_time?: string | null;
  deduction?: {
    win: number;
    place: number;
  } | null;
}

export interface RaceResult {
  position: number;
  name: string;
  barrier: number;
  runner_number: number;
  margin_length: number;
  entrant_id: string;
}

export interface DividendPosition {
  runner_number: number;
  position: number;
}

export interface Dividend {
  id: string;
  tote: string;
  product_name: string;
  status: string;
  dividend: number;
  pool_size?: number;
  jackpot_size?: number;
  positions: DividendPosition[];
  description?: string;
}

export interface TotePool {
  product_type: string;
  total: number;
  status: string;
  guarantee?: number;
  brought_forward?: number;
  terminating?: boolean;
  jackpot?: number;
  leg?: number;
  is_commingled?: boolean;
  is_concession?: boolean;
  is_final?: boolean;
  win_investment?: string;
}

export interface RaceDetail {
  event_id: string;
  meeting_name: string;
  meeting_id: string;
  status: string;
  description: string;
  advertised_start: number;
  actual_start?: number;
  advertised_start_string?: string;
  actual_start_string?: string;
  race_number: number;
  type: string;
  country: string;
  state?: string;
  distance: number;
  weather?: string;
  track_condition?: string;
  track_direction?: string;
  entrant_count?: number;
  field_size?: number;
  positions_paid?: number;
  rail_position?: string;
  prize_monies?: Record<string, number>;
  comment?: string;
}

export interface RaceByIdParams {
  enc: string;
  id: string;
  present_overlay?: boolean;
  with_big_bets?: boolean;
  with_live_bets?: boolean;
  with_money_tracker?: boolean;
  with_biggest_bet?: boolean;
  will_pays?: boolean;
  with_tote_trends_data?: boolean;
}

export interface RaceByIdResponse {
  header: ApiHeader;
  params?: RaceByIdParams;
  data: {
    race: RaceDetail;
    results?: RaceResult[];
    dividends?: Dividend[];
    runners: RunnerDetail[];
    tote_pools?: TotePool[];
  };
}
