import { Router, Response, Request } from 'express';
import { GroupServices } from '../models/index.js';
import { authenticate, requireRole, AuthRequest } from '../middleware/auth.js';

const router = Router();

const defaultData = {
  heroTitle: { en: 'Group Services', zh: '團體服務' },
  heroSubtitle: { en: 'Why host a Group / Team Golf Event', zh: '為什麼舉辦團體/團隊高爾夫活動' },
  advantagesTitle: { en: 'GLG Advantages', zh: 'GLG 優勢' },
  advantagesDescription: {
    en: "Partner with GLG to enhance your group golf events and ensure success. With our extensive golf experience and knowledge, we are your ideal partner for organizing corporate golf outings. We provide memorable experiences that strengthen business relationships, embody your company's values, and offer a seamless experience for all participants.",
    zh: '與 GLG 合作，提升您的團體高爾夫活動並確保成功。憑藉我們豐富的高爾夫經驗和知識，我們是您組織企業高爾夫活動的理想合作夥伴。',
  },
  advantages: [
    { title: { en: 'Professional Image', zh: '專業形象' }, description: { en: "Enhance your group's professional image with our top-notch event organization", zh: '通過我們一流的活動組織提升您團隊的專業形象' } },
    { title: { en: 'Streamlined Communication', zh: '精簡溝通' }, description: { en: 'We handle all interactions with various parties, removing the hassle from your end', zh: '我們處理與各方的所有互動，免除您的煩惱' } },
    { title: { en: 'Expert Event Management', zh: '專業活動管理' }, description: { en: 'Our experienced staff ensures the smooth execution of your event', zh: '我們經驗豐富的員工確保您的活動順利執行' } },
    { title: { en: 'Tailored Service', zh: '度身定制服務' }, description: { en: 'We customize the services to meet your specific requirements', zh: '我們根據您的特定需求定制服務' } },
    { title: { en: 'Industry Connections', zh: '行業聯繫' }, description: { en: 'Benefit from our strong connections across the golf industry', zh: '受益於我們在高爾夫行業的廣泛聯繫' } },
    { title: { en: 'Client-centric Approach', zh: '以客為本' }, description: { en: 'Our focus is on meeting and exceeding client expectations, delivering exceptional value', zh: '我們專注於滿足並超越客戶期望，提供卓越價值' } },
    { title: { en: 'Competitive Service Fee', zh: '具競爭力的服務費' }, description: { en: 'Providing professional services with reasonable cost', zh: '以合理的成本提供專業服務' } },
  ],
  servicesTitle: { en: 'Services Include', zh: '服務包括' },
  serviceItems: [
    { title: { en: 'Pre-event Consultancy', zh: '活動前諮詢' }, description: { en: 'Initial planning and strategy sessions to outline your event', zh: '初步規劃和策略會議以概述您的活動' } },
    { title: { en: 'Budget Setting', zh: '預算設定' }, description: { en: 'Financial planning to ensure cost-effective event management', zh: '財務規劃以確保具成本效益的活動管理' } },
    { title: { en: 'Golf Course Booking and Arrangement', zh: '高爾夫球場預訂及安排' }, description: { en: 'Securing and coordinating your ideal venue', zh: '預訂和協調您的理想場地' } },
    { title: { en: 'Event Promotion and Recruitment', zh: '活動推廣及招募' }, description: { en: 'Marketing your event to attract participants if needed', zh: '在需要時推廣您的活動以吸引參與者' } },
    { title: { en: 'Event Logistics Management', zh: '活動物流管理' }, description: { en: 'Handling all logistical details to ensure seamless execution (Local/International Travel)', zh: '處理所有物流細節以確保無縫執行（本地/國際旅行）' } },
    { title: { en: 'Tournament Scoring System', zh: '錦標賽計分系統' }, description: { en: 'Providing reliable and accurate scoring', zh: '提供可靠而準確的計分' } },
    { title: { en: 'Group Golf Clinic/Seminars', zh: '團體高爾夫診所/研討會' }, description: { en: 'Professional coaching sessions for groups', zh: '為團體提供專業教練課程' } },
  ],
  tournamentsTitle: { en: 'Tournaments Include', zh: '錦標賽包括' },
  tournamentItems: [
    {
      title: { en: 'Reception', zh: '接待' },
      details: [{ en: 'Dedicated staff to assist with check-in, golf bag (baggage handling), and equipment management', zh: '專責員工協助辦理登記、球袋（行李處理）及設備管理' }],
    },
    {
      title: { en: 'Fun Event Planning', zh: '趣味活動策劃' },
      details: [
        { en: 'Challenge design: Longest Drive award, Closest to Pin award', zh: '挑戰設計：最遠開球獎、最近旗桿獎' },
        { en: 'Interactive sessions: Team competitions, fun obstacle games to enhance engagement', zh: '互動環節：團隊比賽、趣味障礙賽以提高參與度' },
      ],
    },
    {
      title: { en: 'Professional Referee Support', zh: '專業裁判支援' },
      details: [
        { en: 'Real-time dispute resolution', zh: '實時爭議解決' },
        { en: 'Pre-game rules briefing', zh: '賽前規則簡報' },
      ],
    },
    {
      title: { en: 'On-Site Coordination', zh: '現場協調' },
      details: [{ en: 'Immediate handling of unexpected situations to ensure smooth operations', zh: '即時處理突發情況以確保順利運作' }],
    },
    {
      title: { en: 'Award Ceremony Design', zh: '頒獎典禮設計' },
      details: [{ en: 'Trophies & prizes: Custom-engraved trophies / co-branded gifts', zh: '獎盃和獎品：定制雕刻獎盃/聯名禮品' }],
    },
    {
      title: { en: 'Event Report', zh: '活動報告' },
      details: [{ en: 'Performance analysis: Participation rate, satisfaction survey, media exposure statistics', zh: '績效分析：參與率、滿意度調查、媒體曝光統計' }],
    },
    {
      title: { en: 'Custom Brand Merchandise', zh: '定制品牌商品' },
      details: [
        { en: 'Event souvenirs: Custom golf balls / caps / umbrellas', zh: '活動紀念品：定制高爾夫球/帽子/雨傘' },
        { en: 'Co-branded items: Team uniforms, personalized tees', zh: '聯名產品：團隊制服、個性化球座' },
      ],
    },
    {
      title: { en: 'Visual Identity', zh: '視覺識別' },
      details: [
        { en: 'Provide banners, logos, and roll-up banner', zh: '提供橫幅、標誌和易拉架' },
        { en: 'Professional photography & videography: Aerial shots / slow-motion replays / highlight reels', zh: '專業攝影及錄影：航拍/慢動作回放/精華片段' },
      ],
    },
  ],
};

// GET /api/group-services — public
router.get('/', async (req: Request, res: Response): Promise<void> => {
  try {
    let page = await GroupServices.findOne();
    if (!page) {
      page = new GroupServices(defaultData);
      await page.save();
    }
    res.json(page);
  } catch (error) {
    res.status(500).json({ error: 'Server error' });
  }
});

// PUT /api/group-services — admin only
router.put(
  '/',
  authenticate,
  requireRole('admin'),
  async (req: AuthRequest, res: Response): Promise<void> => {
    try {
      let page = await GroupServices.findOne();
      if (!page) {
        page = new GroupServices(req.body);
      } else {
        Object.assign(page, req.body);
      }
      await page.save();
      res.json(page);
    } catch (error) {
      res.status(500).json({ error: 'Server error' });
    }
  }
);

export default router;
