// src/collections/Favorites.ts

import type { CollectionConfig } from 'payload'
import { authenticatedOnly, tenantScopedPublicRead } from '../lib/tenantAccess'

/**
 * Category Options for Favorites
 */
const categoryOptions = [
  { label: 'Fashion', value: 'fashion' },
  { label: 'Beauty', value: 'beauty' },
  { label: 'Travel', value: 'travel' },
  { label: 'Tech', value: 'tech' },
  { label: 'Home', value: 'home' },
]

/**
 * Badge Options for Favorites
 */
const badgeOptions = [
  { label: 'Investment Piece', value: 'investment-piece' },
  { label: 'Daily Driver', value: 'daily-driver' },
  { label: 'GRFI Approved', value: 'grfi-approved' },
  { label: 'Neu', value: 'new' },
  { label: 'Bestseller', value: 'bestseller' },
]

/**
 * Price Range Options for Favorites
 */
const priceRangeOptions = [
  { label: 'Budget (< €50)', value: 'budget' },
  { label: 'Mid (€50-150)', value: 'mid' },
  { label: 'Premium (€150-500)', value: 'premium' },
  { label: 'Luxury (> €500)', value: 'luxury' },
]

/**
 * Affiliate Network Options
 */
const affiliateNetworkOptions = [
  { label: 'Amazon', value: 'amazon' },
  { label: 'Awin', value: 'awin' },
  { label: 'LTK (RewardStyle)', value: 'ltk' },
  { label: 'Direct', value: 'direct' },
  { label: 'Other', value: 'other' },
]

/**
 * Favorites Collection
 *
 * Affiliate-Produkte und Favoriten für BlogWoman.de
 * Tenant-scoped für Multi-Tenant-Betrieb.
 */
export const Favorites: CollectionConfig = {
  slug: 'favorites',
  admin: {
    group: 'Content',
    useAsTitle: 'title',
    defaultColumns: ['title', 'category', 'featured', 'isActive'],
    description: 'Affiliate-Produkte und Favoriten',
  },
  access: {
    read: tenantScopedPublicRead,
    create: authenticatedOnly,
    update: authenticatedOnly,
    delete: authenticatedOnly,
  },
  fields: [
    // Main Content
    {
      name: 'title',
      type: 'text',
      required: true,
      maxLength: 200,
      label: 'Titel',
    },
    {
      name: 'slug',
      type: 'text',
      required: true,
      unique: true,
      label: 'Slug',
      admin: {
        position: 'sidebar',
        description: 'URL-freundlicher Name',
      },
    },
    {
      name: 'description',
      type: 'textarea',
      maxLength: 300,
      label: 'Beschreibung',
      admin: {
        description: 'Kurze Produktbeschreibung (max. 300 Zeichen)',
      },
    },

    // Categorization
    {
      name: 'category',
      type: 'select',
      required: true,
      options: categoryOptions,
      label: 'Kategorie',
    },
    {
      name: 'subcategory',
      type: 'text',
      maxLength: 100,
      label: 'Unterkategorie',
      admin: {
        description: 'z.B. "Taschen", "Hautpflege", "Laptops"',
      },
    },

    // Pricing
    {
      name: 'price',
      type: 'number',
      min: 0,
      label: 'Preis (€)',
      admin: {
        description: 'Aktueller Preis in Euro',
      },
    },
    {
      name: 'priceRange',
      type: 'select',
      options: priceRangeOptions,
      label: 'Preiskategorie',
    },

    // Affiliate
    {
      name: 'affiliateUrl',
      type: 'text',
      required: true,
      label: 'Affiliate-URL',
      admin: {
        description: 'Vollständiger Affiliate-Link mit Tracking',
      },
      validate: (value: string | undefined | null) => {
        if (!value) return 'Affiliate-URL ist erforderlich'
        try {
          new URL(value)
          return true
        } catch {
          return 'Bitte eine gültige URL eingeben'
        }
      },
    },
    {
      name: 'affiliateNetwork',
      type: 'select',
      options: affiliateNetworkOptions,
      label: 'Affiliate-Netzwerk',
    },

    // Media
    {
      name: 'image',
      type: 'upload',
      relationTo: 'media',
      required: true,
      label: 'Produktbild',
      admin: {
        description: 'Hauptbild des Produkts',
      },
    },

    // Display Options
    {
      name: 'badge',
      type: 'select',
      options: badgeOptions,
      label: 'Badge',
      admin: {
        description: 'Optionales Label/Abzeichen',
      },
    },
    {
      name: 'featured',
      type: 'checkbox',
      defaultValue: false,
      label: 'Featured',
      admin: {
        position: 'sidebar',
        description: 'In der Featured-Sektion anzeigen',
      },
    },
    {
      name: 'isActive',
      type: 'checkbox',
      required: true,
      defaultValue: true,
      label: 'Aktiv',
      admin: {
        position: 'sidebar',
        description: 'Inaktive Favoriten werden nicht angezeigt',
      },
    },
    {
      name: 'order',
      type: 'number',
      defaultValue: 0,
      label: 'Sortierung',
      admin: {
        position: 'sidebar',
        description: 'Niedrigere Zahlen werden zuerst angezeigt',
      },
    },
  ],
}
