/** Документы Foundry VTT, нужные для импорта (v11+). */

export type FoundryPackageKind = 'world' | 'module';

export type FoundryCompatibility = {
  minimum?: string;
  verified?: string;
  maximum?: string;
};

export type FoundryPackageManifest = {
  kind: FoundryPackageKind;
  /** id пакета (папка / manifest id). */
  id: string;
  /** Человекочитаемое название. */
  title: string;
  rootDir: string;
  compatibility?: FoundryCompatibility;
  coreVersion?: string;
  /** Пути компендиумов относительно rootDir (только для module). */
  packs: FoundryPackRef[];
};

export type FoundryPackRef = {
  name: string;
  label: string;
  path: string;
  type: string;
};

/** Плитка сцены (нужна для телепортов Monks Active Tiles). */
export type FoundrySceneTile = {
  flags?: {
    'monks-active-tiles'?: {
      active?: boolean;
      actions?: {
        action?: string;
        data?: {
          sceneid?: string | { id?: string; name?: string };
        };
      }[];
    };
  };
};

export type FoundrySceneDoc = {
  _id: string;
  name: string;
  img?: string | null;
  background?: { src?: string | null } | null;
  journal?: string | null;
  playlist?: string | null;
  playlistSound?: string | null;
  navigation?: boolean;
  navOrder?: number;
  sort?: number;
  description?: string | null;
  tiles?: FoundrySceneTile[];
};

export type FoundryActorDoc = {
  _id: string;
  name: string;
  img?: string | null;
  prototypeToken?: { texture?: { src?: string | null } | null } | null;
  system?: unknown;
  folder?: string | null;
  type?: string;
};

export type FoundryFolderDoc = {
  _id: string;
  name: string;
  type?: string;
  folder?: string | null;
  sort?: number;
  color?: string | null;
};

export type FoundryPlaylistSound = {
  _id?: string;
  name?: string;
  path?: string | null;
  sort?: number;
};

export type FoundryPlaylistDoc = {
  _id: string;
  name: string;
  sounds?: FoundryPlaylistSound[];
  sort?: number;
};

export type FoundryJournalPage = {
  _id?: string;
  name?: string;
  type?: string;
  text?: { content?: string; format?: number } | null;
  sort?: number;
};

export type FoundryJournalDoc = {
  _id: string;
  name: string;
  pages?: FoundryJournalPage[];
  content?: string;
};

/** Adventure содержит вложенные документы. */
export type FoundryAdventureDoc = {
  _id: string;
  name: string;
  scenes?: FoundrySceneDoc[];
  actors?: FoundryActorDoc[];
  playlists?: FoundryPlaylistDoc[];
  journal?: FoundryJournalDoc[];
  folders?: FoundryFolderDoc[];
  sort?: number;
};

export type FoundryLoadedDocuments = {
  scenes: FoundrySceneDoc[];
  actors: FoundryActorDoc[];
  playlists: FoundryPlaylistDoc[];
  journals: FoundryJournalDoc[];
  adventures: FoundryAdventureDoc[];
  folders: FoundryFolderDoc[];
};

export type FoundrySceneLinkHeuristic =
  | { kind: 'monks-teleports' }
  | { kind: 'journal-refs' }
  | { kind: 'navigation' }
  | { kind: 'sort-order' };

export type FoundrySceneEdgePlan = {
  /** Пары sourceSceneFoundryId → targetSceneFoundryId (могут быть ветвления). */
  edges: { sourceId: string; targetId: string }[];
  heuristic: FoundrySceneLinkHeuristic;
  /** Порядок сцен для списка / раскладки. */
  orderedSceneIds: string[];
  /** Foundry id сцены, которую стоит пометить START. */
  startSceneId: string | null;
};
