import type { GlobalConfig } from 'payload'

import { adminOnly } from '../access/adminOnly'
import { auditGlobalChange } from '../collections/Audit/hooks/auditHooks'
import { revalidateHome } from './hooks/revalidateHome'

export const Home: GlobalConfig = {
  slug: 'home',
  access: {
    read: () => true,
    update: adminOnly,
  },
  fields: [
    // Hero Section
    {
      name: 'heroHeadline',
      type: 'text',
      required: true,
      defaultValue: '創造企業更多發展的可能性\n是我們的使命',
      admin: {
        description: '首頁 Hero 主標題（支援換行）',
      },
    },
    {
      name: 'heroSubheadline',
      type: 'text',
      required: true,
      defaultValue: "It's our destiny to create possibilities for your business.",
      admin: {
        description: '首頁 Hero 副標題（英文）',
      },
    },
    {
      name: 'heroDesktopVideo',
      type: 'upload',
      relationTo: 'media',
      required: false,
      admin: {
        description: '桌面版 Hero 背景影片',
      },
    },
    {
      name: 'heroMobileVideo',
      type: 'upload',
      relationTo: 'media',
      required: false,
      admin: {
        description: '手機版 Hero 背景影片（建議較小檔案以節省流量）',
      },
    },
    {
      name: 'heroFallbackImage',
      type: 'upload',
      relationTo: 'media',
      required: false,
      admin: {
        description: '影片載入失敗時的備用圖片',
      },
    },
    {
      name: 'heroLogo',
      type: 'upload',
      relationTo: 'media',
      required: false,
      admin: {
        description: 'Hero 區域顯示的 Logo（可選）',
      },
    },

    // Service Features Section
    {
      name: 'serviceFeatures',
      type: 'array',
      fields: [
        {
          name: 'icon',
          type: 'text',
          required: true,
          admin: {
            description: '圖示（支援 SVG 或 Emoji，例如：🎯 或 <svg>...</svg>）',
          },
        },
        {
          name: 'title',
          type: 'text',
          required: true,
        },
        {
          name: 'description',
          type: 'textarea',
          required: true,
        },
        {
          name: 'link',
          type: 'group',
          fields: [
            {
              name: 'url',
              type: 'text',
              admin: {
                description: '連結 URL（可選）',
              },
            },
          ],
        },
      ],
      maxRows: 4,
      admin: {
        initCollapsed: true,
      },
    },

    // Portfolio Preview Section
    {
      name: 'portfolioSection',
      type: 'group',
      fields: [
        {
          name: 'headline',
          type: 'text',
          defaultValue: '精選案例',
          required: true,
        },
        {
          name: 'subheadline',
          type: 'text',
          defaultValue: '探索我們為客戶打造的優質網站',
          required: false,
        },
        {
          name: 'itemsToShow',
          type: 'number',
          defaultValue: 3,
          min: 1,
          max: 6,
          admin: {
            description: '顯示多少個作品項目',
          },
        },
      ],
    },

    // CTA Section
    {
      name: 'ctaSection',
      type: 'group',
      fields: [
        {
          name: 'headline',
          type: 'text',
          defaultValue: '準備好開始新的旅程了嗎',
          required: true,
        },
        {
          name: 'description',
          type: 'textarea',
          defaultValue: '讓我們一起打造您的數位成功故事',
          required: false,
        },
        {
          name: 'buttonText',
          type: 'text',
          defaultValue: '聯絡我們',
          required: true,
        },
        {
          name: 'buttonLink',
          type: 'text',
          defaultValue: '/contact-us',
          required: true,
        },
      ],
    },
  ],
  hooks: {
    afterChange: [revalidateHome, auditGlobalChange('home')],
  },
}
