import type { CollectionConfig } from 'payload'

export const SiteSettings: CollectionConfig = {
  slug: 'site-settings',
  labels: {
    singular: 'Site Settings',
    plural: 'Site Settings',
  },
  admin: {
    useAsTitle: 'siteName',
    group: 'Einstellungen',
    description: 'Allgemeine Website-Einstellungen pro Tenant',
  },
  access: {
    read: () => true,
    create: ({ req }) => Boolean(req.user?.isSuperAdmin),
    update: ({ req }) => Boolean(req.user),
    delete: ({ req }) => Boolean(req.user?.isSuperAdmin),
  },
  fields: [
    {
      name: 'siteName',
      type: 'text',
      label: 'Website-Name',
      required: true,
      localized: true,
    },
    {
      name: 'siteTagline',
      type: 'text',
      label: 'Tagline',
      localized: true,
    },
    {
      name: 'logo',
      type: 'upload',
      label: 'Logo',
      relationTo: 'media',
    },
    {
      name: 'favicon',
      type: 'upload',
      label: 'Favicon',
      relationTo: 'media',
    },
    {
      name: 'contact',
      type: 'group',
      label: 'Kontakt',
      fields: [
        {
          name: 'email',
          type: 'email',
          label: 'E-Mail',
        },
        {
          name: 'phone',
          type: 'text',
          label: 'Telefon',
        },
        {
          name: 'fax',
          type: 'text',
          label: 'Fax',
        },
      ],
    },
    // Adresse
    {
      name: 'address',
      type: 'group',
      label: 'Adresse',
      fields: [
        {
          name: 'street',
          type: 'text',
          label: 'Straße & Hausnummer',
          admin: {
            description: 'z.B. "Hans-Böckler-Str. 19"',
          },
        },
        {
          name: 'additionalLine',
          type: 'text',
          label: 'Adresszusatz',
          admin: {
            description: 'z.B. "Gebäude B, 3. Stock"',
          },
        },
        {
          type: 'row',
          fields: [
            {
              name: 'zip',
              type: 'text',
              label: 'PLZ',
              admin: {
                width: '30%',
              },
            },
            {
              name: 'city',
              type: 'text',
              label: 'Stadt',
              admin: {
                width: '70%',
              },
            },
          ],
        },
        {
          name: 'state',
          type: 'text',
          label: 'Bundesland/Region',
        },
        {
          name: 'country',
          type: 'text',
          defaultValue: 'Deutschland',
          label: 'Land',
        },
      ],
    },
    // Geo-Koordinaten
    {
      name: 'geo',
      type: 'group',
      label: 'Geo-Koordinaten',
      admin: {
        description: 'Für Kartenanzeige (Google Maps, OpenStreetMap)',
      },
      fields: [
        {
          type: 'row',
          fields: [
            {
              name: 'lat',
              type: 'number',
              label: 'Breitengrad (Latitude)',
              admin: {
                step: 0.000001,
                description: 'z.B. 51.521732',
                width: '50%',
              },
            },
            {
              name: 'lng',
              type: 'number',
              label: 'Längengrad (Longitude)',
              admin: {
                step: 0.000001,
                description: 'z.B. 6.928850',
                width: '50%',
              },
            },
          ],
        },
        {
          name: 'zoom',
          type: 'number',
          defaultValue: 15,
          min: 1,
          max: 20,
          label: 'Zoom-Level',
          admin: {
            description: '1 = Weltkarte, 20 = Straßenebene',
          },
        },
      ],
    },
    {
      name: 'footer',
      type: 'group',
      label: 'Footer',
      fields: [
        {
          name: 'copyrightText',
          type: 'text',
          label: 'Copyright-Text',
          localized: true,
        },
        {
          name: 'showSocialLinks',
          type: 'checkbox',
          label: 'Social Links anzeigen',
          defaultValue: true,
        },
      ],
    },
    {
      name: 'seo',
      type: 'group',
      label: 'SEO',
      fields: [
        {
          name: 'defaultMetaTitle',
          type: 'text',
          label: 'Standard Meta-Titel',
          localized: true,
        },
        {
          name: 'defaultMetaDescription',
          type: 'textarea',
          label: 'Standard Meta-Beschreibung',
          localized: true,
        },
        {
          name: 'defaultOgImage',
          type: 'upload',
          relationTo: 'media',
          label: 'Standard Social Media Bild',
        },
      ],
    },
  ],
}
