import {
  createWebHistory,
  createMemoryHistory,
  createRouter,
} from "vue-router";

// Layouts
import DefaultLayout from "@/layouts/default.vue";
import MenuLayout from "@/layouts/menu.vue";

// Pages
import HomePage from "@/pages/index.vue";
import Menu from "@/pages/menu/index.vue";
import Legal from "@/pages/legal/index.vue";
import HomePageEN from "@/pages/en/index.vue";
import MenuEN from "@/pages/en/menu/index.vue";
import LegalEN from "@/pages/en/legal/index.vue";

// Route definitions
export const routes = [
  // TR (default)
  {
    path: "/",
    component: DefaultLayout,
    children: [
      { path: "", name: "home", component: HomePage },
      { path: "legal", name: "legal", component: Legal },
    ],
  },
  {
    path: "/menu",
    component: MenuLayout,
    children: [{ path: "", name: "menu", component: Menu }],
  },

  // EN
  {
    path: "/en",
    component: DefaultLayout,
    children: [
      { path: "", name: "home-en", component: HomePageEN },
      { path: "legal", name: "legal-en", component: LegalEN },
    ],
  },
  {
    path: "/en/menu",
    component: MenuLayout,
    children: [{ path: "", name: "menu-en", component: MenuEN }],
  },

  // Admin (client-only, lazy — not included in SSG routes)
  {
    path: "/admin",
    name: "admin",
    component: () => import("@/pages/admin/index.vue"),
  },

  // 404
  {
    path: "/:pathMatch(.*)*",
    name: "not-found",
    component: () => import("@/pages/404.vue"),
  },
];

// Router instance
export const router = createRouter({
  history: import.meta.env.SSR
    ? createMemoryHistory(import.meta.env.BASE_URL)
    : createWebHistory(import.meta.env.BASE_URL),
  routes,

  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) return savedPosition;
    if (to.hash) return { el: to.hash };
    if (to.path !== from.path) return { top: 0 };
    return false;
  },
});
