import { registerHooks } from "./system/hooks.mjs";
import { registerSettings } from "./system/settings.mjs";
import { GroupLink } from "./system/group-link.mjs";

// Import document classes.
import * as documents from "./documents/_module.mjs";

import { VermineUtils } from "./system/roll.mjs";
import { VermineCombat, VermineCombatant, VermineCombatTracker } from "./system/fight.mjs";

// Import DataModels
import * as models from "./models/_module.mjs"

// Import ApplicationV2 sheets
import * as sheets from "./applications/sheets/_module.mjs"

// Import helper/utility classes and constants.
import { preloadHandlebarsTemplates, registerHandlebarsHelpers } from "./system/handlebars-manager.mjs";
import { VERMINE } from "./system/config.mjs";

/* -------------------------------------------- */
/*  Init Hook                                   */
/* -------------------------------------------- */

Hooks.once('init', async function () {

  // System stylesheet is automatically loaded by Foundry from system.json
  // No need to manually inject it - this was causing MIME type issues
  // If you need to ensure fresh CSS, use cache-busting in the filename or system.json version

  // Register GroupLink hooks for automatic synchronization
  GroupLink.registerHooks();

  // Register ALL DataModels FIRST - this is crucial for Foundry V2
  // Use individual assignments like Celestopol for compatibility
  CONFIG.Actor.dataModels.character = models.VermineCharacterData;
  CONFIG.Actor.dataModels.npc = models.VermineNpcData;
  CONFIG.Actor.dataModels.group = models.VermineGroupData;
  CONFIG.Actor.dataModels.creature = models.VermineCreatureData;
  CONFIG.Actor.dataModels.community = models.VermineCommunityData;
  CONFIG.Actor.dataModels.vehicle = models.VermineVehicleData;

  // Libellés des types "community"/"vehicle" (TYPES.Actor.* du core ne les connaît pas)
  CONFIG.Actor.typeLabels ??= {};
  CONFIG.Actor.typeLabels.community = "VERMINE.type_community";
  CONFIG.Actor.typeLabels.vehicle = "VERMINE.type_vehicle";
  Hooks.once('i18nInit', () => {
    CONFIG.Actor.typeLabels.community = "VERMINE.type_community";
    CONFIG.Actor.typeLabels.vehicle = "VERMINE.type_vehicle";
  });

  CONFIG.Item.dataModels.item = models.VermineItemData;
  CONFIG.Item.dataModels.weapon = models.VermineWeaponData;
  CONFIG.Item.dataModels.defense = models.VermineDefenseData;
  CONFIG.Item.dataModels.ability = models.VermineAbilityData;
  CONFIG.Item.dataModels.creaturecapacity = models.VermineCreatureCapacityData;
  CONFIG.Item.dataModels.specialty = models.VermineSpecialtyData;
  CONFIG.Item.dataModels.background = models.VermineBackgroundData;
  CONFIG.Item.dataModels.trauma = models.VermineTraumaData;
  CONFIG.Item.dataModels.evolution = models.VermineEvolutionData;
  CONFIG.Item.dataModels.rumor = models.VermineRumorData;
  CONFIG.Item.dataModels.target = models.VermineTargetData;
  CONFIG.Item.dataModels.rite = models.VermineRiteData;

  // Define custom Document classes AFTER ALL DataModels are registered
  CONFIG.Actor.documentClass = documents.VermineActor;
  CONFIG.Item.documentClass = documents.VermineItem;

  // Add utility classes to the global game object so that they're more easily
  // accessible in global contexts.
  // Note: Do NOT expose Document classes here as it can cause issues with DataModel initialization
  game.vermine2047 = {
    VermineUtils,
    VermineCombat,
    GroupLink
  };

  CONFIG.ui.combat = VermineCombatTracker;
  CONFIG.Combatant.documentClass = VermineCombatant;
  CONFIG.Combat.documentClass = VermineCombat;


  // Register sheet application classes (ApplicationV2)
  // Unregister core sheets
  foundry.documents.collections.Actors.unregisterSheet("core", foundry.applications.sheets.ActorSheetV2, {
    types: ["character", "npc", "group", "creature", "community", "vehicle"]
  })
  foundry.documents.collections.Items.unregisterSheet("core", foundry.appv1?.sheets?.ItemSheet)

  // Actor sheets
  foundry.documents.collections.Actors.registerSheet("vermine2047", sheets.VermineCharacterSheetV2, {
    types: ["character"], makeDefault: true, label: "VERMINE.Sheet.character"
  })
  foundry.documents.collections.Actors.registerSheet("vermine2047", sheets.VermineNpcSheetV2, {
    types: ["npc"], makeDefault: true, label: "VERMINE.Sheet.npc"
  })
  foundry.documents.collections.Actors.registerSheet("vermine2047", sheets.VermineCreatureSheetV2, {
    types: ["creature"], makeDefault: true, label: "VERMINE.Sheet.creature"
  })
  foundry.documents.collections.Actors.registerSheet("vermine2047", sheets.VermineGroupSheetV2, {
    types: ["group"], makeDefault: true, label: "VERMINE.Sheet.group"
  })
  foundry.documents.collections.Actors.registerSheet("vermine2047", sheets.VermineCommunitySheetV2, {
    types: ["community"], makeDefault: true, label: "VERMINE.Sheet.community"
  })
  foundry.documents.collections.Actors.registerSheet("vermine2047", sheets.VermineVehicleSheetV2, {
    types: ["vehicle"], makeDefault: true, label: "VERMINE.Sheet.vehicle"
  })

  // Item sheets — un par type
  foundry.documents.collections.Items.registerSheet("vermine2047", sheets.VermineItemSheetV2, {
    types: ["item"], makeDefault: true
  })
  foundry.documents.collections.Items.registerSheet("vermine2047", sheets.VermineWeaponSheetV2, {
    types: ["weapon"], makeDefault: true
  })
  foundry.documents.collections.Items.registerSheet("vermine2047", sheets.VermineDefenseSheetV2, {
    types: ["defense"], makeDefault: true
  })
  foundry.documents.collections.Items.registerSheet("vermine2047", sheets.VermineAbilitySheetV2, {
    types: ["ability"], makeDefault: true
  })
  foundry.documents.collections.Items.registerSheet("vermine2047", sheets.VermineCreatureCapacitySheetV2, {
    types: ["creaturecapacity"], makeDefault: true
  })
  foundry.documents.collections.Items.registerSheet("vermine2047", sheets.VermineSpecialtySheetV2, {
    types: ["specialty"], makeDefault: true
  })
  foundry.documents.collections.Items.registerSheet("vermine2047", sheets.VermineBackgroundSheetV2, {
    types: ["background"], makeDefault: true
  })
  foundry.documents.collections.Items.registerSheet("vermine2047", sheets.VermineTraumaSheetV2, {
    types: ["trauma"], makeDefault: true
  })
  foundry.documents.collections.Items.registerSheet("vermine2047", sheets.VermineEvolutionSheetV2, {
    types: ["evolution"], makeDefault: true
  })
  foundry.documents.collections.Items.registerSheet("vermine2047", sheets.VermineRumorSheetV2, {
    types: ["rumor"], makeDefault: true
  })
  foundry.documents.collections.Items.registerSheet("vermine2047", sheets.VermineTargetSheetV2, {
    types: ["target"], makeDefault: true
  })
  foundry.documents.collections.Items.registerSheet("vermine2047", sheets.VermineRiteSheetV2, {
    types: ["rite"], makeDefault: true
  })

  registerHandlebarsHelpers(); // Register Handlebars helpers
  registerHooks(); // register Hooks
  registerSettings(); // register Vermine Settings

  // Add custom constants for configuration.
  CONFIG.VERMINE = VERMINE;

  // Les DataModels sont déjà enregistrés dans CONFIG.Actor.dataModels et
  // CONFIG.Item.dataModels. On expose leurs définitions pour compatibilité.

  /**
   * Set an initiative formula for the system
   * @type {String}
   */
  CONFIG.Combat.initiative = {
    formula: "(@abilities.reflexes.value + @skills.alertness.value)d10cs>=@combatStatus.difficulty",
    decimals: 2
  };

  //afficher le mode de jeu
  let mode = game.settings.get('vermine2047', 'game-mode');
  if (!mode) { mode = '1'; await game.settings.set('vermine2047', 'game-mode', '1') }
  let el = document.createElement('SPAN');
  switch (mode) {
    case '1':
      el.innerText = 'mode survie';
      break;
    case '2':
      el.innerText = 'mode cauchemard';
      break;
    case '3':
      el.innerText = 'mode apocalypse';
      break;
  }
  el.classList.add('game-mode');
  el.id = 'game-mode-' + mode;
  document.querySelector('#ui-left').prepend(el);


  // Preload templates (ApplicationV2 + legacy)
  await preloadHandlebarsTemplates();
  await foundry.applications.handlebars.loadTemplates([
    "systems/vermine2047/templates/actor/appv2/character-header.hbs",
    "systems/vermine2047/templates/actor/appv2/character-main.hbs",
    "systems/vermine2047/templates/actor/appv2/character-abilities.hbs",
    "systems/vermine2047/templates/actor/appv2/character-totem.hbs",
    "systems/vermine2047/templates/actor/appv2/character-equipment.hbs",
    "systems/vermine2047/templates/actor/appv2/character-stories.hbs",
    "systems/vermine2047/templates/actor/appv2/character-combat.hbs",
    "systems/vermine2047/templates/actor/appv2/npc-main.hbs",
    "systems/vermine2047/templates/actor/appv2/npc-traits.hbs",
    "systems/vermine2047/templates/actor/appv2/npc-equipment.hbs",
    "systems/vermine2047/templates/actor/appv2/npc-combat.hbs",
    "systems/vermine2047/templates/actor/appv2/npc-notes.hbs",
    "systems/vermine2047/templates/actor/appv2/group-main.hbs",
    "systems/vermine2047/templates/actor/appv2/group-info.hbs",
    "systems/vermine2047/templates/actor/appv2/group-gear.hbs",
    "systems/vermine2047/templates/actor/appv2/group-road.hbs",
    "systems/vermine2047/templates/actor/appv2/group-reserve.hbs",
    "systems/vermine2047/templates/actor/appv2/creature-main.hbs",
    "systems/vermine2047/templates/actor/appv2/creature-info.hbs",
    "systems/vermine2047/templates/actor/appv2/creature-stats.hbs",
    "systems/vermine2047/templates/actor/appv2/creature-combat.hbs",
    "systems/vermine2047/templates/actor/appv2/creature-effects.hbs",
    "systems/vermine2047/templates/actor/appv2/community-main.hbs",
    "systems/vermine2047/templates/actor/appv2/community-identity.hbs",
    "systems/vermine2047/templates/actor/appv2/community-domains.hbs",
    "systems/vermine2047/templates/actor/appv2/community-resources.hbs",
    "systems/vermine2047/templates/actor/appv2/vehicle-main.hbs",
    "systems/vermine2047/templates/actor/appv2/vehicle-specs.hbs"
  ]);

});


