import type { CollectionConfig } from 'payload'
import { isYouTubeManager, hasYouTubeAccess } from '../lib/youtubeAccess'

export const YtMonthlyGoals: CollectionConfig = {
  slug: 'yt-monthly-goals',
  labels: {
    singular: { de: 'Monatsziel', en: 'Monthly Goal' },
    plural: { de: 'Monatsziele', en: 'Monthly Goals' },
  },
  admin: {
    group: 'YouTube',
    useAsTitle: 'displayTitle',
    defaultColumns: ['displayTitle', 'channel', 'month', 'updatedAt'],
  },
  access: {
    read: hasYouTubeAccess,
    create: isYouTubeManager,
    update: isYouTubeManager,
    delete: isYouTubeManager,
  },
  fields: [
    {
      type: 'row',
      fields: [
        {
          name: 'channel',
          type: 'relationship',
          relationTo: 'youtube-channels',
          required: true,
          label: { de: 'Kanal', en: 'Channel' },
          admin: { width: '50%' },
        },
        {
          name: 'month',
          type: 'date',
          required: true,
          label: { de: 'Monat', en: 'Month' },
          admin: {
            width: '50%',
            date: {
              pickerAppearance: 'monthOnly',
              displayFormat: 'MMMM yyyy',
            },
          },
        },
      ],
    },
    {
      name: 'displayTitle',
      type: 'text',
      admin: { hidden: true },
      hooks: {
        beforeChange: [
          async ({ siblingData, req }) => {
            if (siblingData.channel && siblingData.month) {
              try {
                const channelId = typeof siblingData.channel === 'object'
                  ? siblingData.channel.id
                  : siblingData.channel
                const channel = await req.payload.findByID({
                  collection: 'youtube-channels',
                  id: channelId,
                })
                const monthStr = new Date(siblingData.month).toLocaleDateString('de-DE', {
                  month: 'long',
                  year: 'numeric',
                })
                return `${channel?.name || 'Kanal'} - ${monthStr}`
              } catch {
                return 'Neues Monatsziel'
              }
            }
            return 'Neues Monatsziel'
          },
        ],
      },
    },

    // === CONTENT GOALS ===
    {
      name: 'contentGoals',
      type: 'group',
      label: { de: 'Content-Ziele', en: 'Content Goals' },
      fields: [
        {
          type: 'row',
          fields: [
            {
              name: 'longformsTarget',
              type: 'number',
              label: { de: 'Longforms (Ziel)', en: 'Longforms (Target)' },
              defaultValue: 12,
              admin: { width: '25%' },
            },
            {
              name: 'longformsCurrent',
              type: 'number',
              label: { de: 'Aktuell', en: 'Current' },
              defaultValue: 0,
              admin: { width: '25%' },
            },
            {
              name: 'shortsTarget',
              type: 'number',
              label: { de: 'Shorts (Ziel)', en: 'Shorts (Target)' },
              defaultValue: 28,
              admin: { width: '25%' },
            },
            {
              name: 'shortsCurrent',
              type: 'number',
              label: { de: 'Aktuell', en: 'Current' },
              defaultValue: 0,
              admin: { width: '25%' },
            },
          ],
        },
      ],
    },

    // === AUDIENCE GOALS ===
    {
      name: 'audienceGoals',
      type: 'group',
      label: { de: 'Audience-Ziele', en: 'Audience Goals' },
      fields: [
        {
          type: 'row',
          fields: [
            {
              name: 'subscribersTarget',
              type: 'number',
              label: { de: 'Neue Abos (Ziel)', en: 'New Subs (Target)' },
              admin: { width: '25%' },
            },
            {
              name: 'subscribersCurrent',
              type: 'number',
              label: { de: 'Aktuell', en: 'Current' },
              admin: { width: '25%' },
            },
            {
              name: 'viewsTarget',
              type: 'number',
              label: { de: 'Views (Ziel)', en: 'Views (Target)' },
              admin: { width: '25%' },
            },
            {
              name: 'viewsCurrent',
              type: 'number',
              label: { de: 'Aktuell', en: 'Current' },
              admin: { width: '25%' },
            },
          ],
        },
      ],
    },

    // === ENGAGEMENT GOALS ===
    {
      name: 'engagementGoals',
      type: 'group',
      label: { de: 'Engagement-Ziele', en: 'Engagement Goals' },
      fields: [
        {
          type: 'row',
          fields: [
            {
              name: 'avgCtrTarget',
              type: 'text',
              label: { de: 'Ø CTR (Ziel)', en: 'Avg CTR (Target)' },
              admin: {
                width: '25%',
                placeholder: 'z.B. ">4%"',
              },
            },
            {
              name: 'avgCtrCurrent',
              type: 'text',
              label: { de: 'Aktuell', en: 'Current' },
              admin: { width: '25%' },
            },
            {
              name: 'avgRetentionTarget',
              type: 'text',
              label: { de: 'Ø Retention (Ziel)', en: 'Avg Retention (Target)' },
              admin: {
                width: '25%',
                placeholder: 'z.B. ">50%"',
              },
            },
            {
              name: 'avgRetentionCurrent',
              type: 'text',
              label: { de: 'Aktuell', en: 'Current' },
              admin: { width: '25%' },
            },
          ],
        },
      ],
    },

    // === BUSINESS GOALS ===
    {
      name: 'businessGoals',
      type: 'group',
      label: { de: 'Business-Ziele', en: 'Business Goals' },
      fields: [
        {
          type: 'row',
          fields: [
            {
              name: 'newsletterSignupsTarget',
              type: 'number',
              label: { de: 'Newsletter-Anmeldungen (Ziel)', en: 'Newsletter Signups (Target)' },
              admin: { width: '25%' },
            },
            {
              name: 'newsletterSignupsCurrent',
              type: 'number',
              label: { de: 'Aktuell', en: 'Current' },
              admin: { width: '25%' },
            },
            {
              name: 'affiliateRevenueTarget',
              type: 'number',
              label: { de: 'Affiliate-Umsatz (Ziel)', en: 'Affiliate Revenue (Target)' },
              admin: { width: '25%' },
            },
            {
              name: 'affiliateRevenueCurrent',
              type: 'number',
              label: { de: 'Aktuell', en: 'Current' },
              admin: { width: '25%' },
            },
          ],
        },
      ],
    },

    // === CUSTOM GOALS ===
    {
      name: 'customGoals',
      type: 'array',
      label: { de: 'Weitere Ziele', en: 'Custom Goals' },
      fields: [
        {
          type: 'row',
          fields: [
            {
              name: 'metric',
              type: 'text',
              required: true,
              label: { de: 'Metrik', en: 'Metric' },
              admin: {
                width: '40%',
                placeholder: 'z.B. "SPARK Videos"',
              },
            },
            {
              name: 'target',
              type: 'text',
              required: true,
              label: { de: 'Ziel', en: 'Target' },
              admin: {
                width: '20%',
                placeholder: 'Ziel',
              },
            },
            {
              name: 'current',
              type: 'text',
              label: { de: 'Aktuell', en: 'Current' },
              admin: {
                width: '20%',
                placeholder: 'Aktuell',
              },
            },
            {
              name: 'status',
              type: 'select',
              label: 'Status',
              options: [
                { label: 'On Track', value: 'on_track' },
                { label: 'At Risk', value: 'at_risk' },
                { label: 'Achieved', value: 'achieved' },
                { label: 'Missed', value: 'missed' },
              ],
              admin: { width: '20%' },
            },
          ],
        },
      ],
    },

    {
      name: 'notes',
      type: 'textarea',
      label: { de: 'Notizen / Learnings', en: 'Notes / Learnings' },
      localized: true,
    },
  ],
  timestamps: true,
}
