// https://nuxt.com/docs/api/configuration/nuxt-config
import { IUser } from "@models/user";
import { render } from "vue";
import { rc } from "./lib/server/constants";
import { defineNuxtConfig } from "nuxt/config";
import { fileURLToPath } from "url";

const cac = {
	isr: true,
	headersOnly: true,
	cache: {
		headersOnly: true,
		varies: ["Content-Length", "Cookie"],
		maxAge: 60,
	},
	swr: false,
};

export default defineNuxtConfig({
	experimental: {
		watcher: "chokidar-granular",
		treeshakeClientOnly: true,
	},

	devtools: { enabled: false },

	modules: [
		"vue-recaptcha/nuxt",
		"@ant-design-vue/nuxt",
		"@sidebase/nuxt-auth",
		"@pinia/nuxt",
		"@vueuse/nuxt",
		"@nuxt/test-utils/module",
		// "@nuxtjs/i18n",
	],

	// i18n: {
	// 	vueI18n: `./i18n.config.ts`,
	// },
	antd: {
		extractStyle: true,
	},

	css: ["public/fonts.css", "public/css/all.css"],

	auth: {
		baseURL: "/api/auth",
		provider: {
			type: "local",
			pages: {
				login: "/auth/login",
			},
			token: {
				signInResponseTokenPointer: "/token/access",
				type: "Bearer",
				headerName: "Authorization",
				maxAgeInSeconds: 15 * 60,
				cookieName: "rockfic_cookie",
			},

			// @ts-ignore
			sessionDataType: {} as IUser,
			endpoints: {
				signUp: { path: "/register", method: "post" },
				signOut: { path: "/logout", method: "post" },
				getSession: {
					path: "/session",
					method: "get",
				},
			},
			refresh: {
				isEnabled: true,
				endpoint: { path: "/refresh", method: "post" },
				token: {
					signInResponseRefreshTokenPointer: "/token/refresh",
					cookieName: "rockfic.refresh",
					maxAgeInSeconds: 1800,
					sameSiteAttribute: "lax",
					secureCookieAttribute: false,
					cookieDomain: process.env.NODE_ENV == "staging" ? "next.rockfic.com" : process.env.NODE_ENV == "production" ? "www.rockfic.com" : "localhost:3000",
					httpOnlyCookieAttribute: false,
				},
			},
		},
		globalAppMiddleware: false,

		//@ts-ignore
		session: {
			enableRefreshPeriodically: 60 * 60 * 2 * 1000,
			enableRefreshOnWindowFocus: false,
		},
	},
	vite: {
		esbuild: {
			logLimit: 0,
			logLevel: "debug",
			target: "es2020",
		},
		logLevel: "info",
	},
	nitro: {
		esbuild: {
			options: {
				minify: true,
				// loader: "default",
				logLevel: "verbose",
				// sourceMap: "inline",
				target: "es2020",
			},
		},
		preset: process.env.NODE_ENV == "production" ? "bun" : "node-server",
	},
	routeRules: {
		"/": cac,
		"/api/**": { cors: true },
		"/band/**": cac,
		"/authors": cac,
		"/story/**": {
			swr: true,
			isr: true,
			cache: {
				maxAge: 1800,
				headersOnly: true,
				varies: ["Cookie", "Set-Cookie"],
			},
		},
	},

	alias: {
		"@models": fileURLToPath(new URL("./models", import.meta.url)),
		"@client": fileURLToPath(new URL("./lib/client", import.meta.url)),
		"@server": fileURLToPath(new URL("./lib/server", import.meta.url)),
		"@functions": fileURLToPath(new URL("./lib/functions.ts", import.meta.url)),
		"@dbconfig": fileURLToPath(new URL("./lib/dbconfig.ts", import.meta.url)),
		"@test": fileURLToPath(new URL("./lib/testing/", import.meta.url)),
	},
	sourcemap: {
		server: true,
		client: true,
	},
	runtimeConfig: rc,
	typescript: {
		tsConfig: {
			exclude: ["./.nuxt/types/auth.d.ts"],
		},
	},
	imports: {
		autoImport: true,
		dirsScanOptions: {
			types: true,
		},
	},

	compatibilityDate: "2024-11-07",
});