
import { postgresAdapter } from '@payloadcms/db-postgres';

import sharp from 'sharp' // sharp-import
import path from 'path'
import { buildConfig } from 'payload'
import { fileURLToPath } from 'url'
import { lexicalEditor, InlineToolbarFeature, BlocksFeature } from '@payloadcms/richtext-lexical';

import Users from '@/payload/collections/Users';
import Pages from '@/payload/collections/Pages';
import Posts from '@/payload/collections/Posts';
import Studies from './collections/Studies';
import Tags from '@/payload/collections/Tags';
import Menu from '@/payload/globals/Menu'
import { Media } from '@/payload/collections/Media';
import { plugins } from '@/payload/plugins/plugins';
import { ThreeColumnBlock } from '@/Components/blocks/richtext/ThreeColumnBlock/config'

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

export default buildConfig({
  routes: {
    // Spremenite privzeto pot '/api' v karkoli želite
    api: '/www-api',
    admin: '/www-admin'
  },

  admin: {
    importMap: {
      baseDir: path.resolve(dirname),
    },
  },

  // If you'd like to use Rich Text, pass your editor here
  editor: lexicalEditor({
    features: ({ defaultFeatures }) => [
      ...defaultFeatures,
      InlineToolbarFeature(),
      BlocksFeature({
        blocks: [
          ThreeColumnBlock,
          // ... your other blocks
        ],
      }),
    ],
  }),

  // Define and configure your collections in this array
  collections: [Pages, Media, Posts, Studies, Tags, Users],

  globals: [Menu],

  plugins: [...plugins],

  localization: {
    locales: ['en', 'sl', 'de'], // Add your locales
    defaultLocale: 'en',
    fallback: true,
  },

  // Your Payload secret - should be a complex and secure string, unguessable
  secret: process.env.PAYLOAD_SECRET || '',
  // Whichever Database Adapter you're using should go here
  // Mongoose is shown as an example, but you can also use Postgres
  db: postgresAdapter({
    // Postgres-specific arguments go here.
    // `pool` is required.
    pool: {
      host: process.env.DATABASE_HOST,
      port: 25060,
      database: process.env.DATABASE_NAME,
      user: process.env.DATABASE_USER,
      password: process.env.DATABASE_PASSWORD,

      // The SSL settings are now guaranteed to be applied
      ssl: {
        rejectUnauthorized: false, // This is the critical setting
        ca: process.env.DATABASE_CERT,
      },
    }
  }),
  // If you want to resize images, crop, set focal point, etc.
  // make sure to install it and pass it to the config.
  // This is optional - if you don't need to do these things,
  // you don't need it!
  sharp,

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