import path from "node:path";
import { fileURLToPath } from "node:url";
import { buildConfig } from "payload";
import { postgresAdapter } from "@payloadcms/db-postgres";
import { lexicalEditor } from "@payloadcms/richtext-lexical";
import sharp from "sharp";

import { Users } from "@/collections/Users";
import { Media } from "@/collections/Media";
import { Categories } from "@/collections/Categories";
import { Projects } from "@/collections/Projects";
import { Posts } from "@/collections/Posts";
import { Gear } from "@/collections/Gear";
import { ContactSubmissions } from "@/collections/ContactSubmissions";

import { Home } from "@/globals/Home";
import { About } from "@/globals/About";
import { Contact } from "@/globals/Contact";
import { Resume } from "@/globals/Resume";
import { Navigation } from "@/globals/Navigation";
import { SEO } from "@/globals/SEO";

const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);

export default buildConfig({
  serverURL: process.env.PAYLOAD_PUBLIC_SERVER_URL,
  secret: process.env.PAYLOAD_SECRET || "",

  admin: {
    user: "users",
    meta: {
      titleSuffix: "— Jason Woltje",
    },
  },

  editor: lexicalEditor({}),

  db: postgresAdapter({
    pool: {
      connectionString: process.env.DATABASE_URI,
    },
  }),

  collections: [
    Users,
    Media,
    Categories,
    Projects,
    Posts,
    Gear,
    ContactSubmissions,
  ],

  globals: [Home, About, Contact, Resume, Navigation, SEO],

  sharp,

  typescript: {
    outputFile: path.resolve(dirname, "payload-types.ts"),
  },

  graphQL: {
    schemaOutputFile: path.resolve(dirname, "generated-schema.graphql"),
  },
});
