import type { RouteRecordRaw } from "vue-router";
import { createRouter, createWebHistory } from "@ionic/vue-router";
import authRoutes from "./auth";
import { createRouterGuard } from "./guard";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "/layout/riwa",
  },
  {
    path: "/:pathMatch(.*)*",
    redirect: "/layout/riwa",
  },
  ...authRoutes,
  {
    path: "/layout",
    component: () => import("@/components/layout/default.vue"),
    children: [
      {
        path: "riwa",
        component: () => import("@/views/riwa/index.vue"),
      },
      {
        path: "market",
        component: () => import("@/views/market/index.vue"),
      },
      {
        path: "trade",
        component: () => import("@/views/trade/index.vue"),
      },
      {
        path: "chat",
        component: () => import("@/views/chat/index.vue"),
      },
      {
        path: "notify",
        component: () => import("@/views/notify/index.vue"),
      },
      {
        path: "user",
        component: () => import("@/views/user/index.vue"),
        meta: { requiresAuth: true },
      },
    ],
  },
  {
    path: "/notify/:id",
    props: true,
    component: () => import("@/views/notify/detail.vue"),
  },
  {
    path: "/onchain-address",
    component: () => import("@/views/onchain-address/index.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/deposit/fiat",
    component: () => import("@/views/deposit/fiat.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/withdraw/index",
    component: () => import("@/views/withdraw/index.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/revenue/total",
    component: () => import("@/views/revenue/total/index.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/revenue/monthly",
    component: () => import("@/views/revenue/monthly/index.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/revenue/pending",
    component: () => import("@/views/revenue/pending/index.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/revenue/records",
    component: () => import("@/views/revenue/records/index.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/wallet/bill",
    component: () => import("@/views/wallet/bill.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/wallet/transfer",
    component: () => import("@/views/wallet/transfer.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/wallet/funding",
    component: () => import("@/views/wallet/funding.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/wallet/trading",
    component: () => import("@/views/wallet/trading.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/asset_record/:code",
    props: true,
    component: () => import("@/views/wallet/asset-record.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/user/settings",
    component: () => import("@/views/user-settings/index.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/user/settings/username",
    component: () => import("@/views/user-settings/username.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/user/settings/nickname",
    component: () => import("@/views/user-settings/nickname.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/user/settings/email",
    component: () => import("@/views/user-settings/email.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/system-settings",
    component: () => import("@/views/system-settings/index.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/system-settings/language",
    component: () => import("@/views/system-settings/language.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/system-settings/theme",
    component: () => import("@/views/system-settings/theme.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/issue/issuing-apply",
    props: ({ query, params }) => ({ query, params }),
    component: () => import("@/views/issue/issuing-apply/index.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/trade-settings/bank-management",
    component: () => import("@/views/trade-settings/bank-management/index.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/trade-settings/my-subscribe",
    component: () => import("@/views/trade-settings/my-subscribe/index.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/trade-settings/my-subscribe/:id",
    component: () => import("@/views/trade-settings/my-subscribe/detail.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/trade-settings/my-issues",
    component: () => import("@/views/trade-settings/my-issues/index.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/trade-settings/my-issues/:id",
    component: () => import("@/views/trade-settings/my-issues/detail.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/trade-settings/bank-management/add",
    component: () => import("@/views/trade-settings/bank-management/add.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/trade-rwa/:id",
    props: true,
    component: () => import("@/views/trade-rwa/outlet.vue"),
    meta: { requiresAuth: true },
    children: [
      {
        path: "",
        component: () => import("@/views/trade-rwa/index.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "edit",
        name: "trade-rwa-edit",
        component: () => import("@/views/trade-rwa/edit.vue"),
        meta: { requiresAuth: true },
      },
    ],
  },
  {
    path: "/trade-tokenized/:id",
    props: true,
    component: () => import("@/views/trade-tokenized/outlet.vue"),
    meta: { requiresAuth: true },
    children: [
      {
        path: "",
        component: () => import("@/views/trade-tokenized/index.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "edit",
        name: "trade-tokenized-edit",
        component: () => import("@/views/trade-tokenized/edit.vue"),
        meta: { requiresAuth: true },
      },
    ],
  },
  {
    path: "/global-menu",
    component: () => import("@/views/global-menu/index.vue"),
  },
  {
    path: "/pwa_download",
    component: () => import("@/views/pwa/download.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});

createRouterGuard(router);

export { router };
