import { createRouter, createWebHistory } from "vue-router";
import { authState } from "./auth";
import DashboardPage from "./views/DashboardPage.vue";
import LoginPage from "./views/LoginPage.vue";
import RegisterPage from "./views/RegisterPage.vue";
import ChangeInitialPasswordPage from "./views/ChangeInitialPasswordPage.vue";
import PasswordResetPage from "./views/PasswordResetPage.vue";
import AcceptInvitationPage from "./views/AcceptInvitationPage.vue";
import AdminRegistrationsPage from "./views/AdminRegistrationsPage.vue";
import AdminRegistrationDetailPage from "./views/AdminRegistrationDetailPage.vue";
import OrganizationSetupPage from "./views/OrganizationSetupPage.vue";
import UsersPage from "./views/UsersPage.vue";
import CustomersPage from "./views/CustomersPage.vue";
import SuppliersPage from "./views/SuppliersPage.vue";
import ItemsPage from "./views/ItemsPage.vue";
import ActivitiesPage from "./views/ActivitiesPage.vue";
import CashDiscountTermsPage from "./views/CashDiscountTermsPage.vue";
import NumberRangesPage from "./views/NumberRangesPage.vue";
import QuotesPage from "./views/QuotesPage.vue";
import OutgoingInvoicesPage from "./views/OutgoingInvoicesPage.vue";
import IncomingInvoicesPage from "./views/IncomingInvoicesPage.vue";
import PriceImportsPage from "./views/PriceImportsPage.vue";
import ApiConnectorsPage from "./views/ApiConnectorsPage.vue";
import PriceRulesPage from "./views/PriceRulesPage.vue";
import CommunicationsPage from "./views/CommunicationsPage.vue";
import DocumentsPage from "./views/DocumentsPage.vue";

export const publicRoutes = [
  { path: "/login", label: "Login" },
  { path: "/register", label: "Registrierung" },
  { path: "/password-reset", label: "Passwort zurücksetzen" },
  { path: "/accept-invitation", label: "Einladung annehmen" }
];

export const privateRoutes = [
  { path: "/", label: "Dashboard", group: "Vorgänge" },
  { path: "/outgoing-invoices", label: "Ausgangsrechnungen", group: "Vorgänge" },
  { path: "/quotes", label: "Angebote", group: "Vorgänge" },
  { path: "/incoming-invoices", label: "Eingangsrechnungen", group: "Vorgänge" },
  { path: "/master-data/customers", label: "Kunden", group: "Stammdaten" },
  { path: "/master-data/suppliers", label: "Lieferanten", group: "Stammdaten" },
  { path: "/master-data/items", label: "Artikel", group: "Stammdaten" },
  { path: "/activities", label: "Aktivitäten", group: "Stammdaten" },
  { path: "/imports/price-lists", label: "Preislisten", group: "Arbeitsdaten" },
  { path: "/communications", label: "Kommunikation", group: "Arbeitsdaten" },
  { path: "/documents", label: "Dokumente", group: "Arbeitsdaten" },
  { path: "/settings/price-rules", label: "Preisregeln", group: "Einstellungen" },
  { path: "/settings/api-connectors", label: "Preis-APIs", group: "Einstellungen" },
  { path: "/setup/organization", label: "Firmendaten", group: "Einstellungen" },
  { path: "/settings/users", label: "Benutzerrechte", group: "Einstellungen" },
  { path: "/settings/number-ranges", label: "Nummernkreise", group: "Einstellungen" },
  { path: "/settings/cash-discount-terms", label: "Skonto", group: "Einstellungen" },
  { path: "/admin/organization-registrations", label: "Freischaltung", group: "Einstellungen" }
];

export const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: "/", component: DashboardPage, meta: { requiresAuth: true } },
    { path: "/login", component: LoginPage, meta: { publicOnly: true } },
    { path: "/register", component: RegisterPage, meta: { publicOnly: true } },
    { path: "/password-reset", component: PasswordResetPage, meta: { publicOnly: true } },
    { path: "/accept-invitation", component: AcceptInvitationPage, meta: { publicOnly: true } },
    {
      path: "/change-initial-password",
      component: ChangeInitialPasswordPage,
      meta: { requiresAuth: true, passwordChange: true }
    },
    {
      path: "/admin/organization-registrations",
      component: AdminRegistrationsPage,
      meta: { requiresAuth: true }
    },
    {
      path: "/admin/organization-registrations/:id",
      component: AdminRegistrationDetailPage,
      meta: { requiresAuth: true }
    },
    { path: "/setup/organization", component: OrganizationSetupPage, meta: { requiresAuth: true } },
    { path: "/settings/users", component: UsersPage, meta: { requiresAuth: true } },
    { path: "/settings/number-ranges", component: NumberRangesPage, meta: { requiresAuth: true } },
    { path: "/master-data/customers", component: CustomersPage, meta: { requiresAuth: true } },
    { path: "/master-data/suppliers", component: SuppliersPage, meta: { requiresAuth: true } },
    { path: "/master-data/items", component: ItemsPage, meta: { requiresAuth: true } },
    { path: "/settings/cash-discount-terms", component: CashDiscountTermsPage, meta: { requiresAuth: true } },
    { path: "/quotes", component: QuotesPage, meta: { requiresAuth: true } },
    { path: "/outgoing-invoices", component: OutgoingInvoicesPage, meta: { requiresAuth: true } },
    { path: "/incoming-invoices", component: IncomingInvoicesPage, meta: { requiresAuth: true } },
    { path: "/imports/price-lists", component: PriceImportsPage, meta: { requiresAuth: true } },
    { path: "/settings/api-connectors", component: ApiConnectorsPage, meta: { requiresAuth: true } },
    { path: "/settings/price-rules", component: PriceRulesPage, meta: { requiresAuth: true } },
    { path: "/communications", component: CommunicationsPage, meta: { requiresAuth: true } },
    { path: "/documents", component: DocumentsPage, meta: { requiresAuth: true } },
    { path: "/activities", component: ActivitiesPage, meta: { requiresAuth: true } },
    { path: "/:pathMatch(.*)*", redirect: "/login" }
  ]
});

router.beforeEach((to) => {
  const session = authState.session;
  if (!session && to.meta.requiresAuth) return "/login";
  if (session?.mustChangePassword && !to.meta.passwordChange) return "/change-initial-password";
  if (session && to.meta.publicOnly) return "/";
  return true;
});
