import type { CreatorStatistics } from '@/services/statistics';

import type { stateAwardStatistics } from './awards';

export interface Ranked {
  label: string;
  value: number;
}

/** The overview chapters: what the database holds, when and where the creations were made. */
export interface NumbersData {
  totals: {
    creations: number;
    creators: number;
    branches: number;
    places: number;
    lifeEvents: number;
    institutions: number;
    programs: number;
  };
  creationsByBranch: Ranked[];
  decades: number[];
  /** Creations per branch and decade, rows in `creationsByBranch` order. */
  decadeMatrix: { branch: string; counts: number[] }[];
  datedShareFrom1980: number;
  peakDecade: number;
  topPlaces: Ranked[];
  budapestShare: number;
  creatorsByBranch: Ranked[];
  topProfessions: Ranked[];
  deceasedCreators: number;
  completeness: Ranked[];
  /** Creators by the number of their creations. The last bucket is "> 100". */
  workBuckets: Ranked[];
}

type FullSnapshot = typeof import('./_data/snapshot.json');
type Mentors = typeof import('./_data/mentors.json');

/*
 * The fields the story reads. The page sends only these to the browser, so the generated files
 * can stay complete; a field used in the story has to be added here first.
 */
export const OVERVIEW_KEYS = [
  'totals',
  'creationsByBranch',
  'creatorsByBranch',
  'decades',
  'decadeMatrix',
  'budapestShare',
  'workBuckets',
] as const satisfies readonly (keyof NumbersData)[];

export const SNAPSHOT_KEYS = [
  'awardsByDecade',
  'classmatePairs',
  'counties',
  'schools',
  'topAwards',
] as const satisfies readonly (keyof FullSnapshot)[];

export const DATABASE_KEYS = [
  'architecture',
  'playedRoles',
  'sizes',
  'stagedWorks',
  'titles',
] as const satisfies readonly (keyof FullSnapshot['database'])[];

// The frames themselves are drawn into an image (src/app/[locale]/felfedezes/_scripts/generate-size-collage.mjs).
export const SIZE_KEYS = [
  'count',
  'medianWidth',
  'medianHeight',
  'orientation',
  'largest',
] as const satisfies readonly (keyof FullSnapshot['database']['sizes'])[];

export const MENTOR_KEYS = [
  'teachers',
  'students',
  'topTeachers',
] as const satisfies readonly (keyof Mentors)[];

export type StoryOverview = Pick<NumbersData, (typeof OVERVIEW_KEYS)[number]>;

/** Static data contract of the story: the snapshot fields it reads. */
export type Snapshot = Pick<FullSnapshot, (typeof SNAPSHOT_KEYS)[number]> &
  ReturnType<typeof stateAwardStatistics> &
  Pick<
    CreatorStatistics,
    | 'birthDecades'
    | 'bornAbroadShare'
    | 'countries'
    | 'ageBins'
    | 'ageAtCreation'
    | 'oldCreationShare'
    | 'oldestWorkers'
  > & {
    database: Omit<Pick<FullSnapshot['database'], (typeof DATABASE_KEYS)[number]>, 'sizes'> & {
      sizes: Pick<FullSnapshot['database']['sizes'], (typeof SIZE_KEYS)[number]>;
    };
  };

export type StoryMentors = Pick<Mentors, (typeof MENTOR_KEYS)[number]>;

/** A title motif's overall ranking (the story shows the compact view only). */
export type TitleMotif = { matched: number; ranking: Ranked[] };
export type TitleMotifs = { total: number; seasons: TitleMotif; animals: TitleMotif };

export type ClassmatePair = (typeof import('./_data/classmates.json'))['pairs'][number];
