// storage-adapter-import-placeholder
import { postgresAdapter } from "@payloadcms/db-postgres";
import { payloadCloudPlugin } from "@payloadcms/payload-cloud";
import { formBuilderPlugin } from "@payloadcms/plugin-form-builder";
import { s3Storage } from "@payloadcms/storage-s3";
import path from "path";
import { buildConfig } from "payload";
import sharp from "sharp";
import { fileURLToPath } from "url";

import { BlogCategories } from "@/collections/BlogCategories";
import { Blogs } from "@/collections/Blogs";
import { BlogTags } from "@/collections/BlogTags";
import { Media } from "@/collections/Media";
import { Pages } from "@/collections/Pages";
import { Teams } from "@/collections/Teams";
import { Users } from "@/collections/Users";
import {
  BoldFeature,
  FixedToolbarFeature,
  HeadingFeature,
  InlineToolbarFeature,
  ItalicFeature,
  lexicalEditor,
  LinkFeature,
} from "@payloadcms/richtext-lexical";
import { GoogleReviews } from "@/globals/GoogleReviews";
import { Contacts } from "./globals/Contacts";

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

export default buildConfig({
  admin: {
    user: Users.slug,
    importMap: {
      // baseDir: path.resolve(dirname),
      baseDir: "@",
    },
    meta: {
      titleSuffix: "- Admin - Cochise Oncology",
      description: "Cochise Oncology",
      icons: [{ url: "/assets/images/demo-slick/logo-dark.webp" }],
    },
    components: {
      graphics: {
        Logo: {
          path: "/components/Logo/AdminLogo",
        },
      },
    },
    theme: "dark",
  },
  collections: [Users, Media, Blogs, Pages, Teams, BlogCategories, BlogTags],
  globals: [GoogleReviews, Contacts],
  secret: process.env.PAYLOAD_SECRET || "",
  typescript: {
    outputFile: path.resolve(dirname, "payload-types.ts"),
  },
  db: postgresAdapter({
    pool: {
      connectionString: process.env.DATABASE_URI || "",
    },
    // prodMigrations: migrations,
  }),
  editor: lexicalEditor({
    features: () => {
      return [
        BoldFeature(),
        ItalicFeature(),
        LinkFeature({ enabledCollections: ["blogs"] }),
        HeadingFeature({ enabledHeadingSizes: ["h1", "h2", "h3"] }),
        FixedToolbarFeature(),
        InlineToolbarFeature(),
      ];
    },
  }),
  sharp,
  plugins: [
    payloadCloudPlugin(),
    // storage-adapter-placeholder
    s3Storage({
      collections: {
        media: {
          prefix: "media",
        },
      },
      bucket: process.env.S3_BUCKET || "",
      config: {
        forcePathStyle: true, // Important for using Supabase
        credentials: {
          accessKeyId: process.env.S3_ACCESS_KEY_ID || "",
          secretAccessKey: process.env.S3_SECRET_ACCESS_KEY || "",
        },
        region: process.env.S3_REGION,
        endpoint: process.env.S3_ENDPOINT,
      },
    }),
    // form builder
    formBuilderPlugin({
      fields: {
        text: true,
        textarea: true,
        select: true,
        email: true,
        state: true,
        country: true,
        checkbox: true,
        number: true,
        message: false,
        payment: false,
      },
      formOverrides: {
        fields: undefined,
        admin: {
          group: "General",
        },
      },
      formSubmissionOverrides: {
        admin: {
          group: "General",
        },
      },
    }),
  ],
});