import { buildConfig } from "payload";
import { postgresAdapter } from "@payloadcms/db-postgres";
import { lexicalEditor, BlocksFeature } from "@payloadcms/richtext-lexical";
import { nodemailerAdapter } from "@payloadcms/email-nodemailer";
import { s3Storage } from "@payloadcms/storage-s3";
import sharp from "sharp";
import path from "path";
import { fileURLToPath } from "url";
import { payloadBlocks } from "./blocks/allBlocks";

import { Users } from "./collections/Users";
import { Media } from "./collections/Media";
import { FormSubmissions } from "./collections/FormSubmissions";
import { Pages } from "./collections/Pages";

import { migrations } from "../../migrations/index";

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

export default buildConfig({
  admin: {
    user: Users.slug,
    importMap: {
      baseDir: path.resolve(dirname),
    },
    meta: {
      titleSuffix: " – MB Grid Solutions",
    },
  },
  collections: [Users, Media, FormSubmissions, Pages],
  editor: lexicalEditor({
    features: ({ defaultFeatures }) => [
      ...defaultFeatures,
      BlocksFeature({
        blocks: payloadBlocks,
      }),
    ],
  }),
  secret: process.env.PAYLOAD_SECRET || "fallback-secret-for-dev",
  typescript: {
    outputFile: path.resolve(dirname, "payload-types.ts"),
  },
  db: postgresAdapter({
    pool: {
      connectionString:
        process.env.DATABASE_URI ||
        process.env.POSTGRES_URI ||
        `postgresql://${process.env.POSTGRES_USER || "postgres"}:${process.env.POSTGRES_PASSWORD || "postgres"}@127.0.0.1:5432/${process.env.POSTGRES_DB || "payload"}`,
    },
    prodMigrations: migrations,
  }),
  email: nodemailerAdapter({
    defaultFromAddress:
      process.env.MAIL_FROM ||
      process.env.SMTP_FROM ||
      "info@mb-grid-solutions.com",
    defaultFromName: "MB Grid Solutions CMS",
    transportOptions: {
      host: process.env.MAIL_HOST || process.env.SMTP_HOST || "localhost",
      port: parseInt(
        process.env.MAIL_PORT || process.env.SMTP_PORT || "587",
      ),
      auth: {
        user:
          process.env.MAIL_USERNAME ||
          process.env.MAIL_USER ||
          process.env.SMTP_USER,
        pass: process.env.MAIL_PASSWORD || process.env.SMTP_PASS,
      },
      secure:
        process.env.MAIL_SECURE === "true" ||
        process.env.SMTP_SECURE === "true",
    },
  }),
  sharp,
  plugins: [
    ...(process.env.S3_ENDPOINT
      ? [
          s3Storage({
            collections: {
              media: {
                prefix: `${process.env.S3_PREFIX || "mb-grid-solutions"}/media`,
              },
            },
            bucket: process.env.S3_BUCKET || "",
            config: {
              credentials: {
                accessKeyId: process.env.S3_ACCESS_KEY || "",
                secretAccessKey: process.env.S3_SECRET_KEY || "",
              },
              region: process.env.S3_REGION || "fsn1",
              endpoint: process.env.S3_ENDPOINT,
              forcePathStyle: true,
            },
          }),
        ]
      : []),
  ],
});
