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

const router = Router();

const defaultData = {
  heroTitle: { en: 'Business Partners', zh: '商業夥伴' },
  heroSubtitle: {
    en: 'Our strategic partners across the golf industry help us deliver exceptional experiences and value to Golf ID members.',
    zh: '我們的高爾夫行業戰略合作夥伴幫助我們為高球証會員提供卓越的體驗和價值。',
  },
  partners: [
    {
      name: { en: 'China Yinsheng Finance (Holding) Limited', zh: '中國銀盛金融（控股）有限公司' },
      description: {
        en: 'China Yinsheng Finance (Holding) Limited (CYS), established in April 2007 under the Money Lenders Ordinance in Hong Kong, is running a full portfolio of finance-related businesses including mortgages.',
        zh: '中國銀盛金融（控股）有限公司於2007年4月根據香港《放債人條例》成立，經營包括按揭在內的全方位金融相關業務。',
      },
    },
    {
      name: { en: '7Iron HK – Indoor Golf Academy', zh: '7Iron HK – 室內高爾夫學院' },
      description: {
        en: 'Indoor golf academy on Hong Kong Island offers golf simulator practice and coaching courses—a relaxed yet innovative and fun environment for all ages and skill levels.',
        zh: '位於港島的室內高爾夫學院，提供高爾夫模擬器練習及教練課程——輕鬆創新且有趣的環境，適合所有年齡及技術水平。',
      },
    },
    {
      name: { en: 'Phoenix Hill Golf Club – Guangdong Golf Course', zh: '鳳凰山高爾夫球會 – 廣東高爾夫球場' },
      description: {
        en: 'Located on Xiangxin West Road, Yan Tian, Fenggang Town, Dongguan City, Guangdong Province, this 27-hole golf course offers a premier golfing experience.',
        zh: '位於廣東省東莞市鳳崗鎮雁田香新西路，這座27洞高爾夫球場提供頂級的高爾夫體驗。',
      },
    },
    {
      name: { en: 'GOLFZON – Indoor Golf Facility', zh: 'GOLFZON – 室內高爾夫設施' },
      description: {
        en: 'GOLFZON Hong Kong offers prime locations in Causeway Bay, Lai Chi Kok, Admiralty, Tsim Sha Tsui East and Kowloon Bay area.',
        zh: 'GOLFZON香港在銅鑼灣、荔枝角、金鐘、尖沙咀東及九龍灣設有優質場地。',
      },
    },
    {
      name: { en: 'Queensway Golf – Golf Equipment Store', zh: '金鐘高爾夫 – 高爾夫球具店' },
      description: {
        en: 'Specializing in golf retail and wholesale, "Queensway Golf" (located in Sheung Wan) distributes golf clubs from leading global brands, with a focus on Japanese brands.',
        zh: '「金鐘高爾夫」（位於上環）專營高爾夫零售及批發，分銷全球領先品牌的球桿，尤其專注日本品牌。',
      },
    },
    {
      name: { en: 'GOLF CORE – Indoor Golf Driving Range', zh: 'GOLF CORE – 室內高爾夫練習場' },
      description: {
        en: 'Kwun Tong Indoor Golf Driving Range features an automatic ball return system.',
        zh: '觀塘室內高爾夫練習場配備自動回球系統。',
      },
    },
    {
      name: { en: 'Hong Kong PGA – Certified Golf Coach', zh: '香港PGA – 認證高爾夫教練' },
      description: {
        en: 'Enjoy one complimentary PGA coaching session with professional swing technique instruction.',
        zh: '享受一次免費PGA教練課程，包含專業揮桿技巧指導。',
      },
    },
    {
      name: { en: 'PURA GOLF – Custom Club Fitting', zh: 'PURA GOLF – 定製球桿服務' },
      description: {
        en: 'Professional club fitting service in Sheung Wan.',
        zh: '位於上環的專業球桿定製服務。',
      },
    },
    {
      name: { en: 'Hankow Golf – Golf Equipment Store', zh: 'Hankow Golf – 高爾夫球具店' },
      description: {
        en: 'Hankow Golf, located in Tsim Sha Tsui, is a specialty shop for golf equipment.',
        zh: 'Hankow Golf位於尖沙咀，是一家高爾夫球具專門店。',
      },
    },
    {
      name: { en: 'Timothy London – British Luxury Travel Brand', zh: 'Timothy London – 英國奢華旅行品牌' },
      description: {
        en: 'A premium British luggage and travel accessories brand, offering meticulously crafted carry-ons, checked suitcases, bags, and accessories—designed for discerning business travelers who demand elegance and functionality.',
        zh: '英國頂級行李箱及旅行配件品牌，提供精心製作的手提箱、託運箱、包袋及配件——專為追求優雅與實用的商務旅客而設。',
      },
    },
  ],
};

router.get('/', async (req: Request, res: Response): Promise<void> => {
  try {
    let doc = await Partners.findOne();

    if (!doc) {
      doc = new Partners(defaultData);
      await doc.save();
    }

    res.json(doc);
  } catch (error) {
    res.status(500).json({ error: 'Server error' });
  }
});

router.put(
  '/',
  authenticate,
  requireRole('admin'),
  async (req: AuthRequest, res: Response): Promise<void> => {
    try {
      let doc = await Partners.findOne();

      if (!doc) {
        doc = new Partners(req.body);
      } else {
        Object.assign(doc, req.body);
      }

      await doc.save();
      res.json(doc);
    } catch (error) {
      res.status(500).json({ error: 'Server error' });
    }
  }
);

export default router;
