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

const router = Router();

const defaultData = {
  newsSection: {
    title: { en: 'Latest News', zh: '最新消息' },
    subtitle: { en: 'Stay updated with the latest from GLG', zh: '了解GLG的最新動態' },
  },
  servicesSection: {
    title: { en: 'Our Services', zh: '我們的服務' },
    subtitle: { en: 'Comprehensive golf services for every level', zh: '為各級別提供全面的高爾夫服務' },
    cards: [
      {
        title: { en: 'Golf Academy', zh: '高爾夫學院' },
        description: { en: 'World-class coaching from PGA professionals for all skill levels.', zh: '由PGA專業教練為各水平球員提供世界級指導。' },
        image: '',
        link: '/services/golf-academy',
      },
      {
        title: { en: 'Book Lessons', zh: '預約課程' },
        description: { en: 'Schedule your next lesson with our expert coaches at your convenience.', zh: '按您的方便安排下一堂專業教練課程。' },
        image: '',
        link: '/services/book-lessons',
      },
      {
        title: { en: 'Group Services', zh: '團體服務' },
        description: { en: 'Corporate events, tournaments, and group experiences tailored for you.', zh: '為您量身定制的企業活動、錦標賽和團體體驗。' },
        image: '',
        link: '/services/group-services',
      },
      {
        title: { en: 'Golf Development Fund', zh: '高爾夫發展基金' },
        description: { en: 'Supporting the next generation of golfers through our development programs.', zh: '通過我們的發展計劃支持下一代高爾夫球手。' },
        image: '',
        link: '/services/golf-development-fund',
      },
    ],
  },
  eventGallerySection: {
    title: { en: 'Event Gallery', zh: '活動相冊' },
    subtitle: { en: 'Relive our most memorable moments', zh: '重溫我們最難忘的時刻' },
  },
  golfIdSection: {
    title: { en: 'Golf ID', zh: '高球証' },
    description: {
      en: 'Join thousands of members enjoying exclusive golf benefits, rewards, and experiences.',
      zh: '加入數千名會員，享受獨家高爾夫福利、獎勵和體驗。',
    },
    image: '',
    ctaText: { en: 'Get Your Golf ID', zh: '取得您的高球証' },
    ctaLink: '/golf-id',
  },
  aboutSection: {
    title: { en: 'About GLG', zh: '關於GLG' },
    description: {
      en: 'Greens Leisure Group is dedicated to promoting golf in Hong Kong, offering world-class services and facilities.',
      zh: 'Greens Leisure Group致力於在香港推廣高爾夫運動，提供世界級的服務和設施。',
    },
    ctaText: { en: 'Learn More About Us', zh: '了解更多' },
  },
};

router.get('/', async (req: Request, res: Response): Promise<void> => {
  try {
    let data = await HomePage.findOne();
    if (!data) {
      data = await HomePage.create(defaultData);
    }
    res.json(data);
  } catch (error) {
    console.error('Error fetching homepage data:', error);
    res.status(500).json({ message: 'Error fetching homepage data' });
  }
});

router.put('/', authenticate, requireRole('admin'), async (req: AuthRequest, res: Response): Promise<void> => {
  try {
    let data = await HomePage.findOne();
    if (!data) {
      data = await HomePage.create(req.body);
    } else {
      Object.assign(data, req.body);
      await data.save();
    }
    res.json(data);
  } catch (error) {
    console.error('Error updating homepage data:', error);
    res.status(500).json({ message: 'Error updating homepage data' });
  }
});

export default router;
