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

const router = Router();

const defaultData = {
  heroTitle: { en: 'Golf Development Fund', zh: '高爾夫發展基金' },
  heroSubtitle: { en: '', zh: '' },
  operationTitle: { en: 'Operation Mode', zh: '運作模式' },
  operationDescription: {
    en: 'Every Golf ID transaction contributes to the growth of golf in Hong Kong through a simple, transparent process.',
    zh: '每筆高球証交易都透過簡單透明的流程，為香港高爾夫的發展作出貢獻。',
  },
  operationSteps: [
    {
      title: { en: 'Payment', zh: '付款' },
      description: {
        en: 'For example, $10,000 spending × 0.2% = $20 contribution to GDF',
        zh: '例如，$10,000 消費 × 0.2% = $20 貢獻至 GDF',
      },
    },
    {
      title: { en: 'Golf ID Spending', zh: 'Golf ID 消費' },
      description: {
        en: 'A partial income from Golf ID spending is contributed to GDF',
        zh: 'Golf ID 消費的部分收入將貢獻至 GDF',
      },
    },
    {
      title: { en: 'Investment in Golf Platform Development', zh: '投資高爾夫平台發展' },
      description: {
        en: 'Funds are invested into developing the golf platform',
        zh: '資金用於發展高爾夫平台',
      },
    },
  ],
  ctaBanner: {
    title: { en: 'Be Part of Golf\'s Future', zh: '成為高爾夫未來的一份子' },
    description: {
      en: 'Your Golf ID membership directly supports the growth and development of golf across Hong Kong.',
      zh: '您的高球証會籍直接支持香港高爾夫運動的成長與發展。',
    },
    buttonText: { en: 'Get Golf ID', zh: '獲取高球証' },
    buttonUrl: '/golf-id',
  },
  investmentTitle: {
    en: 'Investment in Golf Platform Development',
    zh: '投資高爾夫平台發展',
  },
  investmentDescription: {
    en: 'Funds are strategically allocated across key areas to build a stronger golf ecosystem in Hong Kong.',
    zh: '資金被策略性地分配到關鍵領域，以建立更強大的香港高爾夫生態系統。',
  },
  investmentAreas: [
    {
      title: { en: 'Training Centers', zh: '訓練中心' },
      description: {
        en: 'Developing world-class training facilities to nurture golfing talent',
        zh: '建設世界級訓練設施，培養高爾夫人才',
      },
    },
    {
      title: { en: 'Golf Tournament Sponsorship', zh: '高爾夫錦標賽贊助' },
      description: {
        en: 'Supporting tournaments to promote the sport and competitive play',
        zh: '支持錦標賽以推廣運動及競技比賽',
      },
    },
    {
      title: { en: 'Golfers Development', zh: '高爾夫球手發展' },
      description: {
        en: 'Investing in player development programs for aspiring golfers',
        zh: '投資球手發展計劃，培育有潛力的高爾夫球手',
      },
    },
  ],
};

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

router.put(
  '/',
  authenticate,
  requireRole('admin'),
  async (req: AuthRequest, res: Response): Promise<void> => {
    try {
      let page = await GolfDevelopmentFund.findOne();
      if (!page) {
        page = new GolfDevelopmentFund(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;
