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

import { de } from '@payloadcms/translations/languages/de'
import { en } from '@payloadcms/translations/languages/en'
import { hu } from '@payloadcms/translations/languages/hu'

import { Users } from './collections/Users'
import { Media } from './collections/Media'
import {Persoenlichkeiten} from "@/collections/Persoenlichkeiten";
import {Geschichte} from "@/globals/Geschichte";
import {KulturSeiten} from "@/collections/KulturSeiten";
import {Politik} from "@/globals/Politik";
import {Kultur} from "@/globals/Kultur";
import {Sprache} from "@/globals/Sprache";
import {Neuigkeiten} from "@/collections/Neuigkeiten";
import {Artikel} from "@/collections/Artikel";
import {Archiv} from "@/collections/Archiv";

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

export default buildConfig({
  serverURL: 'http://localhost:3000',
  cors: [
    'http://localhost:3000',
    'http://localhost:5173',
    'https://donauschwaben.online',
  ],
  csrf: [
    'http://localhost:3000',
    'http://localhost:5173',
    'https://donauschwaben.online',
  ],
  localization: {
    locales: [
      { label: 'Deutsch', code: 'de' },
      { label: 'English', code: 'en' },
      { label: 'Magyar', code: 'hu' },
    ],
    defaultLocale: 'de',
    fallback: true,
  },
  i18n: {
    supportedLanguages: { de, en, hu },
  },
  admin: {
    user: Users.slug,
    importMap: {
      baseDir: path.resolve(dirname),
    },
  },
  collections: [
      Users,
      Media,
      Persoenlichkeiten,
      KulturSeiten,
      Neuigkeiten,
      Artikel,
      Archiv
  ],
  globals: [
    Geschichte,
    Kultur,
    Sprache,
    Politik,
  ],
  editor: lexicalEditor(),
  secret: process.env.PAYLOAD_SECRET || '',
  typescript: {
    outputFile: path.resolve(dirname, 'payload-types.ts'),
  },
  db: postgresAdapter({
    pool: {
      connectionString: process.env.DATABASE_URL || '',
    },
  }),
  sharp,
  plugins: [],
})
