import type { CollectionConfig } from 'payload'

/**
 * Projects Collection
 *
 * Projekte für Spieleentwicklung und kreative Arbeiten (gunshin)
 * Portfolio von Spielen, Mods, Tools und anderen Projekten.
 */
export const Projects: CollectionConfig = {
  slug: 'projects',
  labels: {
    singular: 'Projekt',
    plural: 'Projekte',
  },
  admin: {
    group: 'Inhalte',
    useAsTitle: 'title',
    defaultColumns: ['title', 'type', 'status', 'platform', 'releaseDate'],
    description: 'Projekte, Spiele und kreative Arbeiten',
  },
  access: {
    read: () => true,
    create: ({ req: { user } }) => Boolean(user),
    update: ({ req: { user } }) => Boolean(user),
    delete: ({ req: { user } }) => Boolean(user?.isSuperAdmin),
  },
  fields: [
    // Grundinformationen
    {
      name: 'title',
      type: 'text',
      required: true,
      label: 'Projekttitel',
      localized: true,
    },
    {
      name: 'slug',
      type: 'text',
      required: true,
      unique: true,
      label: 'Slug',
    },
    {
      name: 'tagline',
      type: 'text',
      label: 'Tagline',
      localized: true,
      admin: {
        description: 'Kurze, prägnante Beschreibung (1 Zeile)',
      },
    },
    {
      name: 'description',
      type: 'richText',
      label: 'Beschreibung',
      localized: true,
    },
    {
      name: 'shortDescription',
      type: 'textarea',
      label: 'Kurzbeschreibung',
      localized: true,
      admin: {
        description: 'Für Übersichten und Social Media',
      },
    },
    // Projekt-Typ
    {
      name: 'type',
      type: 'select',
      required: true,
      label: 'Projekt-Typ',
      options: [
        { label: 'Spiel (Vollversion)', value: 'game' },
        { label: 'Demo', value: 'demo' },
        { label: 'Mod', value: 'mod' },
        { label: 'Tool / Utility', value: 'tool' },
        { label: 'Asset Pack', value: 'assets' },
        { label: 'Prototyp', value: 'prototype' },
        { label: 'Game Jam', value: 'gamejam' },
        { label: 'Tutorial / Kurs', value: 'tutorial' },
        { label: 'Open Source', value: 'opensource' },
        { label: 'Sonstiges', value: 'other' },
      ],
    },
    // Genre
    {
      name: 'genres',
      type: 'select',
      hasMany: true,
      label: 'Genres',
      options: [
        { label: 'Action', value: 'action' },
        { label: 'Adventure', value: 'adventure' },
        { label: 'RPG', value: 'rpg' },
        { label: 'Strategie', value: 'strategy' },
        { label: 'Simulation', value: 'simulation' },
        { label: 'Puzzle', value: 'puzzle' },
        { label: 'Horror', value: 'horror' },
        { label: 'Shooter', value: 'shooter' },
        { label: 'Platformer', value: 'platformer' },
        { label: 'Racing', value: 'racing' },
        { label: 'Sports', value: 'sports' },
        { label: 'Fighting', value: 'fighting' },
        { label: 'Music/Rhythm', value: 'music' },
        { label: 'Visual Novel', value: 'visualnovel' },
        { label: 'Survival', value: 'survival' },
        { label: 'Sandbox', value: 'sandbox' },
        { label: 'Tower Defense', value: 'towerdefense' },
        { label: 'Roguelike', value: 'roguelike' },
        { label: 'Indie', value: 'indie' },
      ],
    },
    // Plattformen
    {
      name: 'platforms',
      type: 'select',
      hasMany: true,
      label: 'Plattformen',
      options: [
        { label: 'Windows', value: 'windows' },
        { label: 'macOS', value: 'macos' },
        { label: 'Linux', value: 'linux' },
        { label: 'Web/Browser', value: 'web' },
        { label: 'iOS', value: 'ios' },
        { label: 'Android', value: 'android' },
        { label: 'PlayStation', value: 'playstation' },
        { label: 'Xbox', value: 'xbox' },
        { label: 'Nintendo Switch', value: 'switch' },
        { label: 'Steam Deck', value: 'steamdeck' },
        { label: 'VR', value: 'vr' },
      ],
    },
    // Medien
    {
      name: 'featuredImage',
      type: 'upload',
      relationTo: 'media',
      required: true,
      label: 'Hauptbild',
    },
    {
      name: 'logo',
      type: 'upload',
      relationTo: 'media',
      label: 'Projekt-Logo',
    },
    {
      name: 'screenshots',
      type: 'array',
      label: 'Screenshots',
      fields: [
        {
          name: 'image',
          type: 'upload',
          relationTo: 'media',
          required: true,
          label: 'Screenshot',
        },
        {
          name: 'caption',
          type: 'text',
          label: 'Bildunterschrift',
          localized: true,
        },
      ],
    },
    {
      name: 'videos',
      type: 'array',
      label: 'Videos',
      fields: [
        {
          name: 'type',
          type: 'select',
          required: true,
          label: 'Typ',
          options: [
            { label: 'Trailer', value: 'trailer' },
            { label: 'Gameplay', value: 'gameplay' },
            { label: 'Devlog', value: 'devlog' },
            { label: 'Tutorial', value: 'tutorial' },
            { label: 'Sonstiges', value: 'other' },
          ],
        },
        {
          name: 'title',
          type: 'text',
          label: 'Titel',
          localized: true,
        },
        {
          name: 'url',
          type: 'text',
          required: true,
          label: 'Video-URL',
          admin: {
            description: 'YouTube, Vimeo oder direkter Link',
          },
        },
        {
          name: 'thumbnail',
          type: 'upload',
          relationTo: 'media',
          label: 'Thumbnail',
        },
      ],
    },
    // Technische Details
    {
      name: 'techStack',
      type: 'group',
      label: 'Technologie',
      fields: [
        {
          name: 'engine',
          type: 'select',
          label: 'Engine',
          options: [
            { label: 'Unity', value: 'unity' },
            { label: 'Unreal Engine', value: 'unreal' },
            { label: 'Godot', value: 'godot' },
            { label: 'GameMaker', value: 'gamemaker' },
            { label: 'RPG Maker', value: 'rpgmaker' },
            { label: 'Construct', value: 'construct' },
            { label: 'Custom Engine', value: 'custom' },
            { label: 'Ren\'Py', value: 'renpy' },
            { label: 'Phaser', value: 'phaser' },
            { label: 'Sonstiges', value: 'other' },
          ],
        },
        {
          name: 'languages',
          type: 'select',
          hasMany: true,
          label: 'Programmiersprachen',
          options: [
            { label: 'C#', value: 'csharp' },
            { label: 'C++', value: 'cpp' },
            { label: 'GDScript', value: 'gdscript' },
            { label: 'JavaScript', value: 'javascript' },
            { label: 'TypeScript', value: 'typescript' },
            { label: 'Python', value: 'python' },
            { label: 'Lua', value: 'lua' },
            { label: 'Rust', value: 'rust' },
            { label: 'Blueprint', value: 'blueprint' },
          ],
        },
        {
          name: 'tools',
          type: 'text',
          label: 'Weitere Tools',
          admin: {
            description: 'z.B. Blender, Aseprite, FMOD',
          },
        },
      ],
    },
    // Systemanforderungen
    {
      name: 'requirements',
      type: 'group',
      label: 'Systemanforderungen',
      admin: {
        condition: (data) =>
          data?.type === 'game' || data?.type === 'demo' || data?.type === 'tool',
      },
      fields: [
        {
          name: 'minimum',
          type: 'group',
          label: 'Minimum',
          fields: [
            { name: 'os', type: 'text', label: 'Betriebssystem' },
            { name: 'cpu', type: 'text', label: 'Prozessor' },
            { name: 'ram', type: 'text', label: 'RAM' },
            { name: 'gpu', type: 'text', label: 'Grafikkarte' },
            { name: 'storage', type: 'text', label: 'Speicherplatz' },
          ],
        },
        {
          name: 'recommended',
          type: 'group',
          label: 'Empfohlen',
          fields: [
            { name: 'os', type: 'text', label: 'Betriebssystem' },
            { name: 'cpu', type: 'text', label: 'Prozessor' },
            { name: 'ram', type: 'text', label: 'RAM' },
            { name: 'gpu', type: 'text', label: 'Grafikkarte' },
            { name: 'storage', type: 'text', label: 'Speicherplatz' },
          ],
        },
      ],
    },
    // Veröffentlichung & Links
    {
      name: 'releaseDate',
      type: 'date',
      label: 'Veröffentlichungsdatum',
      admin: {
        date: {
          pickerAppearance: 'dayOnly',
          displayFormat: 'dd.MM.yyyy',
        },
      },
    },
    {
      name: 'links',
      type: 'group',
      label: 'Links',
      fields: [
        {
          name: 'website',
          type: 'text',
          label: 'Projekt-Website',
        },
        {
          name: 'steam',
          type: 'text',
          label: 'Steam',
        },
        {
          name: 'itchio',
          type: 'text',
          label: 'itch.io',
        },
        {
          name: 'epicGames',
          type: 'text',
          label: 'Epic Games Store',
        },
        {
          name: 'gog',
          type: 'text',
          label: 'GOG',
        },
        {
          name: 'playStore',
          type: 'text',
          label: 'Google Play Store',
        },
        {
          name: 'appStore',
          type: 'text',
          label: 'App Store',
        },
        {
          name: 'github',
          type: 'text',
          label: 'GitHub',
        },
        {
          name: 'discord',
          type: 'text',
          label: 'Discord',
        },
        {
          name: 'twitter',
          type: 'text',
          label: 'Twitter/X',
        },
      ],
    },
    // Downloads
    {
      name: 'downloads',
      type: 'array',
      label: 'Downloads',
      fields: [
        {
          name: 'title',
          type: 'text',
          required: true,
          label: 'Titel',
          admin: {
            description: 'z.B. "Windows Build", "Demo v0.5"',
          },
        },
        {
          name: 'platform',
          type: 'select',
          label: 'Plattform',
          options: [
            { label: 'Windows', value: 'windows' },
            { label: 'macOS', value: 'macos' },
            { label: 'Linux', value: 'linux' },
            { label: 'Universal', value: 'universal' },
          ],
        },
        {
          name: 'version',
          type: 'text',
          label: 'Version',
        },
        {
          name: 'file',
          type: 'upload',
          relationTo: 'media',
          label: 'Datei',
        },
        {
          name: 'externalUrl',
          type: 'text',
          label: 'Externer Download-Link',
          admin: {
            condition: (data, siblingData) => !siblingData?.file,
          },
        },
        {
          name: 'size',
          type: 'text',
          label: 'Dateigröße',
        },
      ],
    },
    // Features
    {
      name: 'features',
      type: 'array',
      label: 'Features',
      fields: [
        {
          name: 'title',
          type: 'text',
          required: true,
          label: 'Feature',
          localized: true,
        },
        {
          name: 'description',
          type: 'textarea',
          label: 'Beschreibung',
          localized: true,
        },
        {
          name: 'icon',
          type: 'upload',
          relationTo: 'media',
          label: 'Icon',
        },
      ],
    },
    // Team / Credits
    {
      name: 'team',
      type: 'array',
      label: 'Team / Credits',
      fields: [
        {
          name: 'name',
          type: 'text',
          required: true,
          label: 'Name',
        },
        {
          name: 'role',
          type: 'text',
          label: 'Rolle',
          localized: true,
        },
        {
          name: 'link',
          type: 'text',
          label: 'Link (Portfolio/Social)',
        },
        {
          name: 'avatar',
          type: 'upload',
          relationTo: 'media',
          label: 'Avatar',
        },
      ],
    },
    // Game Jam Info
    {
      name: 'gameJam',
      type: 'group',
      label: 'Game Jam Info',
      admin: {
        condition: (data) => data?.type === 'gamejam',
      },
      fields: [
        {
          name: 'jamName',
          type: 'text',
          label: 'Jam-Name',
        },
        {
          name: 'theme',
          type: 'text',
          label: 'Thema',
        },
        {
          name: 'duration',
          type: 'text',
          label: 'Dauer',
          admin: {
            description: 'z.B. "48 Stunden"',
          },
        },
        {
          name: 'ranking',
          type: 'text',
          label: 'Platzierung',
        },
        {
          name: 'jamLink',
          type: 'text',
          label: 'Link zum Jam',
        },
      ],
    },
    // Devlog Posts (Verknüpfung)
    {
      name: 'devlogs',
      type: 'relationship',
      relationTo: 'posts',
      hasMany: true,
      label: 'Devlog-Posts',
      admin: {
        description: 'Verknüpfte Blog-Posts über dieses Projekt',
      },
    },
    // Status
    {
      name: 'status',
      type: 'select',
      required: true,
      defaultValue: 'development',
      label: 'Status',
      options: [
        { label: 'In Entwicklung', value: 'development' },
        { label: 'Early Access', value: 'earlyaccess' },
        { label: 'Released', value: 'released' },
        { label: 'Pausiert', value: 'paused' },
        { label: 'Eingestellt', value: 'cancelled' },
        { label: 'Abgeschlossen', value: 'completed' },
      ],
      admin: {
        position: 'sidebar',
      },
    },
    // Sichtbarkeit
    {
      name: 'visibility',
      type: 'select',
      defaultValue: 'public',
      label: 'Sichtbarkeit',
      options: [
        { label: 'Öffentlich', value: 'public' },
        { label: 'Entwurf', value: 'draft' },
        { label: 'Unlisted', value: 'unlisted' },
        { label: 'Privat', value: 'private' },
      ],
      admin: {
        position: 'sidebar',
      },
    },
    // Featured
    {
      name: 'featured',
      type: 'checkbox',
      defaultValue: false,
      label: 'Featured Projekt',
      admin: {
        position: 'sidebar',
      },
    },
    // Sortierung
    {
      name: 'sortOrder',
      type: 'number',
      defaultValue: 0,
      label: 'Sortierung',
      admin: {
        position: 'sidebar',
        description: 'Höher = weiter oben',
      },
    },
    // SEO
    {
      name: 'seo',
      type: 'group',
      label: 'SEO',
      fields: [
        {
          name: 'metaTitle',
          type: 'text',
          label: 'Meta-Titel',
          localized: true,
        },
        {
          name: 'metaDescription',
          type: 'textarea',
          label: 'Meta-Beschreibung',
          localized: true,
        },
        {
          name: 'ogImage',
          type: 'upload',
          relationTo: 'media',
          label: 'Social Media Bild',
        },
      ],
    },
  ],
  timestamps: true,
  hooks: {
    beforeChange: [
      ({ data }) => {
        // Auto-generate slug
        if (data && !data.slug && data.title) {
          data.slug = data.title
            .toLowerCase()
            .replace(/[äöüß]/g, (char: string) => {
              const map: Record<string, string> = { ä: 'ae', ö: 'oe', ü: 'ue', ß: 'ss' }
              return map[char] || char
            })
            .replace(/[^a-z0-9]+/g, '-')
            .replace(/(^-|-$)/g, '')
        }
        return data
      },
    ],
  },
}
