import "./assets/styles.scss";
import { createApp } from "vue";
import App from "./App.vue";
import { createCoreRouter } from "./router/index.js";
import { customerRoutes } from "./modules/customers/index.js";
import { hourRegistrationRoutes } from "./modules/hour-registration/index.js";
import { offerRoutes } from "./modules/offers/index.js";
import { invoiceRoutes } from "./modules/invoices/index.js";

import { definePreset } from "@primeuix/themes";
import Aura from "@primeuix/themes/aura";
import PrimeVue from "primevue/config";
import ConfirmationService from "primevue/confirmationservice";
import ToastService from "primevue/toastservice";
import Ripple from "primevue/ripple";
import StyleClass from "primevue/styleclass";

import { hydrateAppSettings } from "./composables/useAppSettings.js";
import { applyThemeFromLayoutConfig } from "./themeBootstrap.js";

/**
 * Bootstrap Vue, Vue Router, and PrimeVue for Maestro CORE.
 *
 * @param {(router: import('vue-router').Router) => void} [configureRouter]
 *        Optional callback to extend the router after CORE routes are registered.
 * @param {{ customers?: boolean }} [modules]
 *        Deprecated: customer SPA routes are always mounted; runtime enable/disable is enforced via
 *        {@link meta.module} guards and {@link enabled_module_slugs} from `/api/user`.
 */
export async function bootCore(configureRouter, _modules = {}) {
  void _modules;

  await hydrateAppSettings();

  const router = createCoreRouter((r) => {
    customerRoutes.forEach((route) =>
      r.addRoute("authenticated", route),
    );
    hourRegistrationRoutes.forEach((route) =>
      r.addRoute("authenticated", route),
    );
    offerRoutes.forEach((route) => r.addRoute("authenticated", route));
    invoiceRoutes.forEach((route) => r.addRoute("authenticated", route));
    if (typeof configureRouter === "function") {
      configureRouter(r);
    }
  });

  const app = createApp(App);

  app.use(router);

  const MyPreset = definePreset(Aura, {
    semantic: {
      primary: {
        50: "{sky.50}",
        100: "{sky.100}",
        200: "{sky.200}",
        300: "{sky.300}",
        400: "{sky.400}",
        500: "{sky.500}",
        600: "{sky.600}",
        700: "{sky.700}",
        800: "{sky.800}",
        900: "{sky.900}",
        950: "{sky.950}",
      },
    },
  });

  app.use(PrimeVue, {
    theme: {
      preset: MyPreset,
      options: {
        darkModeSelector: ".app-dark",
      },
    },
  });
  app.use(ToastService);
  app.use(ConfirmationService);
  app.directive("ripple", Ripple);
  app.directive("styleclass", StyleClass);

  applyThemeFromLayoutConfig();

  app.mount("#app");
}
