import type { CollectionConfig } from 'payload'

export const Media: CollectionConfig = {
  slug: 'media',
  admin: {
    useAsTitle: 'alt',
    group: 'Medien',
    description: 'Bilder und Dokumente mit automatischer Optimierung',
  },
  access: {
    read: () => true,
    create: ({ req }) => !!req.user,
    update: ({ req }) => !!req.user,
    delete: ({ req }) => !!req.user,
  },
  upload: {
    staticDir: 'media',
    mimeTypes: ['image/*', 'application/pdf', 'video/*'],
    // Bildoptimierung mit Sharp
    imageSizes: [
      // Thumbnail für Admin-Übersichten und kleine Vorschauen
      {
        name: 'thumbnail',
        width: 150,
        height: 150,
        fit: 'cover',
        position: 'center',
        formatOptions: {
          format: 'webp',
          options: {
            quality: 80,
          },
        },
      },
      // Kleine Bilder für Cards, Avatare, Icons
      {
        name: 'small',
        width: 400,
        height: undefined,
        fit: 'inside',
        withoutEnlargement: true,
        formatOptions: {
          format: 'webp',
          options: {
            quality: 80,
          },
        },
      },
      // Mittlere Bilder für Blog-Vorschauen, Testimonials
      {
        name: 'medium',
        width: 800,
        height: undefined,
        fit: 'inside',
        withoutEnlargement: true,
        formatOptions: {
          format: 'webp',
          options: {
            quality: 82,
          },
        },
      },
      // Große Bilder für Hero-Sections, Vollbild-Ansichten
      {
        name: 'large',
        width: 1200,
        height: undefined,
        fit: 'inside',
        withoutEnlargement: true,
        formatOptions: {
          format: 'webp',
          options: {
            quality: 85,
          },
        },
      },
      // Extra große Bilder für hochauflösende Displays
      {
        name: 'xlarge',
        width: 1920,
        height: undefined,
        fit: 'inside',
        withoutEnlargement: true,
        formatOptions: {
          format: 'webp',
          options: {
            quality: 85,
          },
        },
      },
      // 2K für Retina/HiDPI Displays
      {
        name: '2k',
        width: 2560,
        height: undefined,
        fit: 'inside',
        withoutEnlargement: true,
        formatOptions: {
          format: 'webp',
          options: {
            quality: 85,
          },
        },
      },
      // Quadratisches Format für Social Media / OG Images
      {
        name: 'og',
        width: 1200,
        height: 630,
        fit: 'cover',
        position: 'center',
        formatOptions: {
          format: 'webp',
          options: {
            quality: 85,
          },
        },
      },
      // AVIF-Varianten für moderne Browser (beste Kompression)
      {
        name: 'medium_avif',
        width: 800,
        height: undefined,
        fit: 'inside',
        withoutEnlargement: true,
        formatOptions: {
          format: 'avif',
          options: {
            quality: 70,
          },
        },
      },
      {
        name: 'large_avif',
        width: 1200,
        height: undefined,
        fit: 'inside',
        withoutEnlargement: true,
        formatOptions: {
          format: 'avif',
          options: {
            quality: 70,
          },
        },
      },
      {
        name: 'xlarge_avif',
        width: 1920,
        height: undefined,
        fit: 'inside',
        withoutEnlargement: true,
        formatOptions: {
          format: 'avif',
          options: {
            quality: 70,
          },
        },
      },
    ],
    // Admin-Thumbnail für die Übersicht
    adminThumbnail: 'thumbnail',
    // Fokuspunkt für Cropping
    focalPoint: true,
  },
  fields: [
    {
      name: 'alt',
      type: 'text',
      required: true,
      label: 'Alt-Text',
      admin: {
        description: 'Beschreibung für Screenreader und SEO (Pflichtfeld)',
      },
    },
    {
      name: 'caption',
      type: 'text',
      label: 'Bildunterschrift',
      admin: {
        description: 'Optionale Bildunterschrift für Darstellung unter dem Bild',
      },
    },
    {
      name: 'credit',
      type: 'text',
      label: 'Bildnachweis/Copyright',
      admin: {
        description: 'Fotograf, Agentur oder Quelle',
      },
    },
    {
      name: 'tags',
      type: 'text',
      hasMany: true,
      label: 'Tags',
      admin: {
        description: 'Schlagwörter für die Suche und Filterung',
      },
    },
  ],
}
