import type { GlobalConfig } from 'payload'

/**
 * SEO Settings Global
 *
 * Globale SEO-Einstellungen pro Tenant für:
 * - Meta-Defaults
 * - Social Media Profile
 * - Schema.org Organisation
 * - robots.txt Regeln
 */
export const SEOSettings: GlobalConfig = {
  slug: 'seo-settings',
  label: 'SEO Einstellungen',
  admin: {
    group: 'Einstellungen',
    description: 'Globale SEO-Konfiguration und Schema.org Daten',
  },
  access: {
    // Alle angemeldeten Benutzer können lesen
    read: ({ req: { user } }) => Boolean(user),
    // Nur Super Admins können bearbeiten
    update: ({ req: { user } }) => Boolean(user?.isSuperAdmin),
  },
  fields: [
    // === META DEFAULTS ===
    {
      name: 'metaDefaults',
      type: 'group',
      label: 'Standard Meta-Tags',
      fields: [
        {
          name: 'titleSuffix',
          type: 'text',
          label: 'Titel-Suffix',
          defaultValue: '| Website',
          localized: true,
          admin: {
            description: 'Wird an jeden Seitentitel angehängt (z.B. "Startseite | Firmenname")',
          },
        },
        {
          name: 'defaultDescription',
          type: 'textarea',
          label: 'Standard Meta-Beschreibung',
          maxLength: 160,
          localized: true,
          admin: {
            description: 'Wird verwendet, wenn keine spezifische Beschreibung gesetzt ist',
          },
        },
        {
          name: 'defaultOgImage',
          type: 'upload',
          relationTo: 'media',
          label: 'Standard Social Media Bild',
          admin: {
            description: 'Fallback-Bild für Social Media Shares (empfohlen: 1200x630px)',
          },
        },
        {
          name: 'keywords',
          type: 'text',
          hasMany: true,
          label: 'Standard Keywords',
          admin: {
            description: 'Globale Keywords (optional, geringe SEO-Relevanz)',
          },
        },
      ],
    },

    // === ORGANIZATION SCHEMA ===
    {
      name: 'organization',
      type: 'group',
      label: 'Organisation (Schema.org)',
      admin: {
        description: 'Daten für das Organization Schema',
      },
      fields: [
        {
          name: 'name',
          type: 'text',
          label: 'Firmenname',
          required: true,
        },
        {
          name: 'legalName',
          type: 'text',
          label: 'Rechtlicher Name',
          admin: {
            description: 'Vollständiger rechtlicher Firmenname (falls abweichend)',
          },
        },
        {
          name: 'description',
          type: 'textarea',
          label: 'Unternehmensbeschreibung',
          maxLength: 300,
          localized: true,
        },
        {
          name: 'logo',
          type: 'upload',
          relationTo: 'media',
          label: 'Logo',
          admin: {
            description: 'Firmenlogo für Schema.org (min. 112x112px, empfohlen: 512x512px)',
          },
        },
        {
          name: 'foundingDate',
          type: 'date',
          label: 'Gründungsdatum',
          admin: {
            date: {
              pickerAppearance: 'dayOnly',
            },
          },
        },
      ],
    },

    // === CONTACT INFO ===
    {
      name: 'contact',
      type: 'group',
      label: 'Kontaktdaten',
      fields: [
        {
          name: 'email',
          type: 'email',
          label: 'E-Mail',
        },
        {
          name: 'phone',
          type: 'text',
          label: 'Telefon',
          admin: {
            description: 'Im Format +49 123 456789',
          },
        },
        {
          name: 'fax',
          type: 'text',
          label: 'Fax',
        },
      ],
    },

    // === ADDRESS ===
    {
      name: 'address',
      type: 'group',
      label: 'Adresse',
      fields: [
        {
          name: 'street',
          type: 'text',
          label: 'Straße & Hausnummer',
        },
        {
          name: 'postalCode',
          type: 'text',
          label: 'Postleitzahl',
        },
        {
          name: 'city',
          type: 'text',
          label: 'Stadt',
        },
        {
          name: 'region',
          type: 'text',
          label: 'Bundesland/Region',
        },
        {
          name: 'country',
          type: 'text',
          label: 'Land',
          defaultValue: 'Deutschland',
        },
        {
          name: 'countryCode',
          type: 'text',
          label: 'Ländercode',
          defaultValue: 'DE',
          admin: {
            description: 'ISO 3166-1 Alpha-2 Code',
          },
        },
      ],
    },

    // === GEO LOCATION ===
    {
      name: 'geo',
      type: 'group',
      label: 'Geo-Koordinaten',
      admin: {
        description: 'Für Local Business Schema',
      },
      fields: [
        {
          name: 'latitude',
          type: 'number',
          label: 'Breitengrad',
          admin: {
            step: 0.000001,
          },
        },
        {
          name: 'longitude',
          type: 'number',
          label: 'Längengrad',
          admin: {
            step: 0.000001,
          },
        },
      ],
    },

    // === SOCIAL PROFILES ===
    {
      name: 'socialProfiles',
      type: 'array',
      label: 'Social Media Profile',
      admin: {
        description: 'URLs zu Social Media Profilen (für sameAs Schema)',
      },
      fields: [
        {
          name: 'platform',
          type: 'select',
          label: 'Plattform',
          options: [
            { label: 'Facebook', value: 'facebook' },
            { label: 'Instagram', value: 'instagram' },
            { label: 'Twitter/X', value: 'twitter' },
            { label: 'LinkedIn', value: 'linkedin' },
            { label: 'YouTube', value: 'youtube' },
            { label: 'TikTok', value: 'tiktok' },
            { label: 'Pinterest', value: 'pinterest' },
            { label: 'XING', value: 'xing' },
            { label: 'Andere', value: 'other' },
          ],
        },
        {
          name: 'url',
          type: 'text',
          label: 'Profil-URL',
          required: true,
        },
      ],
    },

    // === LOCAL BUSINESS ===
    {
      name: 'localBusiness',
      type: 'group',
      label: 'Local Business',
      admin: {
        description: 'Zusätzliche Daten für lokale Unternehmen',
      },
      fields: [
        {
          name: 'enabled',
          type: 'checkbox',
          label: 'Local Business Schema aktivieren',
          defaultValue: false,
        },
        {
          name: 'type',
          type: 'select',
          label: 'Geschäftstyp',
          options: [
            { label: 'Lokales Unternehmen', value: 'LocalBusiness' },
            { label: 'Arztpraxis', value: 'Physician' },
            { label: 'Zahnarzt', value: 'Dentist' },
            { label: 'Anwaltskanzlei', value: 'Attorney' },
            { label: 'Restaurant', value: 'Restaurant' },
            { label: 'Hotel', value: 'Hotel' },
            { label: 'Einzelhandel', value: 'Store' },
            { label: 'Fitnessstudio', value: 'HealthClub' },
            { label: 'Friseursalon', value: 'HairSalon' },
            { label: 'Autowerkstatt', value: 'AutoRepair' },
            { label: 'Immobilienmakler', value: 'RealEstateAgent' },
            { label: 'Finanzdienstleister', value: 'FinancialService' },
            { label: 'IT-Dienstleister', value: 'ProfessionalService' },
            { label: 'Pflegedienst', value: 'MedicalBusiness' },
          ],
          admin: {
            condition: (data) => data?.localBusiness?.enabled,
          },
        },
        {
          name: 'priceRange',
          type: 'select',
          label: 'Preiskategorie',
          options: [
            { label: '€ (Günstig)', value: '€' },
            { label: '€€ (Mittel)', value: '€€' },
            { label: '€€€ (Gehoben)', value: '€€€' },
            { label: '€€€€ (Premium)', value: '€€€€' },
          ],
          admin: {
            condition: (data) => data?.localBusiness?.enabled,
          },
        },
        {
          name: 'openingHours',
          type: 'array',
          label: 'Öffnungszeiten',
          admin: {
            condition: (data) => data?.localBusiness?.enabled,
            description: 'Im Format "Mo-Fr 09:00-17:00"',
          },
          fields: [
            {
              name: 'specification',
              type: 'text',
              label: 'Öffnungszeit',
              admin: {
                placeholder: 'Mo-Fr 09:00-17:00',
              },
            },
          ],
        },
      ],
    },

    // === ROBOTS SETTINGS ===
    {
      name: 'robots',
      type: 'group',
      label: 'Robots & Indexierung',
      fields: [
        {
          name: 'allowIndexing',
          type: 'checkbox',
          label: 'Indexierung erlauben',
          defaultValue: true,
          admin: {
            description: 'Wenn deaktiviert, wird die gesamte Website von Suchmaschinen ausgeschlossen',
          },
        },
        {
          name: 'additionalDisallow',
          type: 'text',
          hasMany: true,
          label: 'Zusätzliche Pfade ausschließen',
          admin: {
            description: 'Pfade die nicht gecrawlt werden sollen (z.B. "/intern", "/preview")',
            condition: (data) => data?.robots?.allowIndexing,
          },
        },
      ],
    },

    // === VERIFICATION CODES ===
    {
      name: 'verification',
      type: 'group',
      label: 'Verifizierungscodes',
      admin: {
        description: 'Codes für Suchmaschinen-Verifizierung',
      },
      fields: [
        {
          name: 'google',
          type: 'text',
          label: 'Google Search Console',
          admin: {
            description: 'Meta-Tag Content (nur der Code, nicht das gesamte Tag)',
          },
        },
        {
          name: 'bing',
          type: 'text',
          label: 'Bing Webmaster Tools',
        },
        {
          name: 'yandex',
          type: 'text',
          label: 'Yandex Webmaster',
        },
      ],
    },
  ],
}
