export type PredictionCardKind = "LEGACY" | "CUSTOM";
export type PredictionValueType = "NUMBER" | "TEXT" | "SELECT" | "MULTI_TEXT" | "DATE";

export type PredictionCardField = {
  id: string;
  cardId: string;
  label: string;
  sortOrder: number;
  points: number;
  isPrimary: boolean;
  isRequired: boolean;
  minNumber: number | null;
  maxNumber: number | null;
  stepNumber: number | null;
};

export type PredictionCardOption = {
  id: string;
  cardId: string;
  label: string;
  value: string;
  sortOrder: number;
};

export type PredictionCard = {
  id: string;
  code: string | null;
  title: string;
  description: string | null;
  type: PredictionCardKind;
  valueType: PredictionValueType;
  styleId: number;
  unit: string | null;
  sortOrder: number;
  basePoints: number;
  isActive: boolean;
  isDeletable: boolean;
  fields: PredictionCardField[];
  options: PredictionCardOption[];
};

export type PredictionEntryValue = {
  id: string;
  entryId: string;
  fieldId: string;
  valueText: string | null;
  valueNumber: number | null;
  valueDate: string | null;
  field: PredictionCardField;
};

export type PredictionEntryHistoryItem = {
  id: string;
  entryId: string;
  userId: string;
  snapshot: Array<{
    fieldId: string;
    valueText: string | null;
    valueNumber: number | null;
    valueDate: string | null;
  }>;
  message: string | null;
  createdAt: string;
};

export type PredictionFieldScore = {
  id: string;
  entryId: string;
  fieldId: string;
  suggestedPoints: number;
  awardedPoints: number | null;
  isValidated: boolean;
  note: string | null;
  field: PredictionCardField;
};

export type PredictionEntry = {
  id: string;
  userId: string;
  cardId: string;
  selectedBabyIndex: number | null;
  totalPoints: number;
  isScored: boolean;
  createdAt: string;
  updatedAt: string;
  card: PredictionCard;
  values: PredictionEntryValue[];
  history: PredictionEntryHistoryItem[];
  scores: PredictionFieldScore[];
};

export type PredictionActivity = {
  id: string;
  type: string;
  message: string;
  userId: string | null;
  cardId: string | null;
  entryId: string | null;
  createdAt: string;
  user: {
    id: string;
    username: string;
    displayName: string | null;
    profileImageUrl: string | null;
    profileBgColor: string | null;
  } | null;
  card: {
    id: string;
    title: string;
  } | null;
  entry: {
    id: string;
    values: Array<{
      fieldId: string;
      valueText: string | null;
      valueNumber: number | null;
      valueDate: string | null;
      field: {
        id: string;
        label: string;
      };
    }>;
  } | null;
};

export type PredictionBoardValue = {
  fieldId: string;
  fieldLabel: string;
  valueText: string | null;
  valueNumber: number | null;
  valueDate: string | null;
};

export type PredictionBoardOutcome = {
  fieldId: string;
  fieldLabel: string;
  selectedBabyIndex: number | null;
  valueText: string | null;
  valueNumber: number | null;
  valueDate: string | null;
};

export type PredictionBoardEntry = {
  entryId: string;
  userId: string;
  selectedBabyIndex: number | null;
  username: string;
  displayName: string | null;
  profileImageUrl: string | null;
  profileBgColor: string | null;
  isScored: boolean;
  totalPoints: number;
  values: PredictionBoardValue[];
  updatedAt: string;
};

export type PredictionBoardCard = {
  id: string;
  title: string;
  type: PredictionValueType;
  unit: string | null;
  styleId: number;
  totalPredictions: number;
  averageNumber: number | null;
  topTexts: Array<{ value: string; count: number }>;
  outcomes: PredictionBoardOutcome[];
  entries: PredictionBoardEntry[];
};

export type PredictionGameState = {
  id: string;
  status: "OPEN" | "CLOSED";
  finalized: boolean;
  closedAt: string | null;
  reopenedAt: string | null;
};

export type PredictionBoardResponse = {
  game: PredictionGameState | null;
  cards: PredictionBoardCard[];
};

export type PredictionScoreboardItem = {
  userId: string;
  username: string;
  displayName: string | null;
  profileImageUrl: string | null;
  profileBgColor: string | null;
  totalPoints: number;
};

export type PredictionFinalizeResponse = {
  finalized: boolean;
  winners: PredictionScoreboardItem[];
  scoreboard: PredictionScoreboardItem[];
};

export type PredictionOutcomeValue = {
  fieldId: string;
  valueText?: string;
  valueNumber?: number;
  valueDate?: string;
};

export type PredictionSetOutcomesPayload = {
  selectedBabyIndex?: number;
  values: PredictionOutcomeValue[];
};

export type PredictionValidateScoreItem = {
  fieldId: string;
  awardedPoints?: number;
  note?: string;
};

export type PredictionValidateScoresPayload = {
  scores: PredictionValidateScoreItem[];
};

export type PredictionScoringEntry = {
  id: string;
  userId: string;
  user: {
    id: string;
    username: string;
    displayName: string | null;
  };
  scores: Array<{
    fieldId: string;
    suggestedPoints: number;
    awardedPoints: number | null;
    isValidated: boolean;
    note: string | null;
    field: {
      id: string;
      label: string;
      points: number;
    };
  }>;
};

export type UpsertPredictionValuePayload = {
  fieldId: string;
  valueText?: string;
  valueNumber?: number;
  valueDate?: string;
};

export type UpsertPredictionPayload = {
  selectedBabyIndex?: number;
  values: UpsertPredictionValuePayload[];
};

export type PredictionDraftValue = {
  fieldId: string;
  valueText: string;
  valueNumber: number | null;
  valueDate: string;
};
