// storage-adapter-import-placeholder
import { mongooseAdapter } from '@payloadcms/db-mongodb'
import { payloadCloudPlugin } from '@payloadcms/payload-cloud'
import { lexicalEditor } from '@payloadcms/richtext-lexical'
import path from 'path'
import { buildConfig } from 'payload'
import { fileURLToPath } from 'url'
import sharp from 'sharp'

import { Users } from './collections/Users'
import { Media } from './collections/Media'
import { s3Storage } from '@payloadcms/storage-s3'
import { PrivacyPolicy } from './app/(payload)/globals/privacy-policy'
import { Home } from './app/(payload)/globals/home'
import { News } from './collections/News'
import { Reviews } from './collections/Reviews'
import { Cars } from './collections/Cars'
import { ContactForm } from './globals/ContactForm/config'
import { RentalTerms } from './app/(payload)/globals/rental-terms'
import { Blog } from './app/(payload)/globals/blog'
import { Catalog } from './app/(payload)/globals/catalog'
import { ReviewsPage } from './app/(payload)/globals/reviews'
import { AboutUs } from './app/(payload)/globals/aboutUs'
import { NotFound } from './globals/not-found'
import { Footer } from './globals/Footer/config'

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

export default buildConfig({
  admin: {
    user: Users.slug,
    importMap: {
      baseDir: path.resolve(dirname),
    },
  },
  collections: [Users, Media, News, Reviews, Cars],
  editor: lexicalEditor(),
  secret: process.env.PAYLOAD_SECRET || '',
  typescript: {
    outputFile: path.resolve(dirname, 'payload-types.ts'),
  },
  localization: {
    locales: ['en', 'ru', 'es'],
    defaultLocale: 'en',
  },
  db: mongooseAdapter({
    url: process.env.DATABASE_URI || '',
  }),
  globals: [
    PrivacyPolicy,
    Home,
    RentalTerms,
    ContactForm,
    Blog,
    ReviewsPage,
    AboutUs,
    NotFound,
    Catalog,
    Footer,
  ],
  sharp,
  plugins: [
    payloadCloudPlugin(),
    s3Storage({
      collections: {
        media: true,
      },
      bucket: process.env.S3_BUCKET || '',
      config: {
        credentials: {
          accessKeyId: process.env.S3_ACCESS_KEY_ID || '',
          secretAccessKey: process.env.S3_SECRET_ACCESS_KEY || '',
        },
        region: process.env.S3_REGION,
        requestHandler: {
          connectionTimeout: 10000,
          requestTimeout: 10000,
          httpAgent: {
            keepAlive: true,
            maxSockets: 1000,
          },
          httpsAgent: {
            keepAlive: true,
            maxSockets: 1000,
          },
        },
      },
    }),
  ],
})
