// src/collections/SocialAccounts.ts

import type { CollectionConfig } from 'payload'
import { isCommunityManager, hasCommunityAccess } from '../lib/communityAccess'

/**
 * SocialAccounts Collection
 *
 * Verknüpft Social Media Accounts mit YouTube-Kanälen und speichert API-Credentials.
 * Teil des Community Management Systems.
 */
export const SocialAccounts: CollectionConfig = {
  slug: 'social-accounts',
  labels: {
    singular: 'Social Account',
    plural: 'Social Accounts',
  },
  admin: {
    group: 'Community',
    useAsTitle: 'displayName',
    defaultColumns: ['displayName', 'platform', 'linkedChannel', 'isActive'],
  },
  access: {
    read: hasCommunityAccess,
    create: isCommunityManager,
    update: isCommunityManager,
    delete: isCommunityManager,
  },
  fields: [
    {
      type: 'row',
      fields: [
        {
          name: 'platform',
          type: 'relationship',
          relationTo: 'social-platforms',
          required: true,
          label: 'Plattform',
          admin: { width: '50%' },
        },
        {
          name: 'linkedChannel',
          type: 'relationship',
          relationTo: 'youtube-channels',
          label: 'Verknüpfter YouTube-Kanal',
          admin: {
            width: '50%',
            description: 'Für Zuordnung zu Brand/Kanal',
          },
        },
      ],
    },
    {
      type: 'row',
      fields: [
        {
          name: 'displayName',
          type: 'text',
          required: true,
          label: 'Anzeigename',
          admin: {
            width: '50%',
            placeholder: 'BlogWoman YouTube',
          },
        },
        {
          name: 'accountHandle',
          type: 'text',
          label: 'Handle / Username',
          admin: {
            width: '50%',
            placeholder: '@blogwoman',
          },
        },
      ],
    },
    {
      type: 'row',
      fields: [
        {
          name: 'externalId',
          type: 'text',
          label: 'Platform Account ID',
          admin: {
            width: '50%',
            description: 'YouTube Channel ID, LinkedIn URN, etc.',
          },
        },
        {
          name: 'accountUrl',
          type: 'text',
          label: 'Account URL',
          admin: { width: '50%' },
        },
      ],
    },
    {
      name: 'isActive',
      type: 'checkbox',
      label: 'Aktiv',
      defaultValue: true,
      admin: {
        position: 'sidebar',
      },
    },

    // OAuth Credentials (verschlüsselt speichern!)
    {
      name: 'credentials',
      type: 'group',
      label: 'API Credentials',
      admin: {
        description: 'Sensible Daten – nur für Super-Admins sichtbar',
        condition: (data, siblingData, { user }) =>
          Boolean((user as any)?.isSuperAdmin || (user as any)?.is_super_admin),
      },
      fields: [
        {
          name: 'accessToken',
          type: 'text',
          label: 'Access Token',
          admin: {
            description: 'OAuth Access Token',
          },
        },
        {
          name: 'refreshToken',
          type: 'text',
          label: 'Refresh Token',
        },
        {
          name: 'tokenExpiresAt',
          type: 'date',
          label: 'Token Ablauf',
        },
        {
          name: 'apiKey',
          type: 'text',
          label: 'API Key',
          admin: {
            description: 'Für API-Key basierte Auth',
          },
        },
      ],
    },

    // Stats (periodisch aktualisiert)
    {
      name: 'stats',
      type: 'group',
      label: 'Account Stats',
      admin: {
        position: 'sidebar',
      },
      fields: [
        {
          name: 'followers',
          type: 'number',
          label: 'Followers/Subscribers',
          admin: { readOnly: true },
        },
        {
          name: 'totalPosts',
          type: 'number',
          label: 'Total Posts/Videos',
          admin: { readOnly: true },
        },
        {
          name: 'lastSyncedAt',
          type: 'date',
          label: 'Letzter Sync',
          admin: { readOnly: true },
        },
      ],
    },

    // Sync Settings
    {
      name: 'syncSettings',
      type: 'group',
      label: 'Sync Settings',
      fields: [
        {
          name: 'autoSyncEnabled',
          type: 'checkbox',
          label: 'Auto-Sync aktiviert',
          defaultValue: true,
        },
        {
          name: 'syncIntervalMinutes',
          type: 'number',
          label: 'Sync-Intervall (Minuten)',
          defaultValue: 15,
          min: 5,
          max: 1440,
        },
        {
          name: 'syncComments',
          type: 'checkbox',
          label: 'Kommentare synchronisieren',
          defaultValue: true,
        },
        {
          name: 'syncDMs',
          type: 'checkbox',
          label: 'DMs synchronisieren',
          defaultValue: false,
          admin: {
            description: 'Nicht alle Plattformen unterstützen DM-API',
          },
        },
      ],
    },
  ],
  timestamps: true,
}
