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

const router = Router();

const defaultData = {
  heroTitle: { en: 'ESHOP', zh: '網上商店' },
  heroSubtitle: {
    en: 'We curate a range of unique golf products for our members, offering a convenient way to purchase at exclusive prices. Browse the product list below for details and ordering information.',
    zh: '我們為會員搜羅各種獨特的高球產品，讓會員能以簡易的方式和優惠的價格購入心儀的貨品。詳情請瀏覽以下貨品清單並細閱選購方法。',
  },
  memberBanner: {
    title: { en: 'Exclusive Member Offers', zh: '尊享會員優惠' },
    description: {
      en: 'The following products are exclusively offered at special prices for Golf ID members. Register now for free to unlock member pricing.',
      zh: '以下產品獨家優惠專為「高球証會員」提供。立即免費登記成為會員，解鎖優惠價格。',
    },
    ctaText: { en: 'Register for Free', zh: '免費登記' },
    ctaLink: 'https://golfid.com.hk/register',
  },
  products: [
    {
      name: { en: 'GolfMA Laser Rangefinder', zh: '戈夫曼高爾夫激光測距儀' },
      brand: 'GolfMA',
      regularPrice: 1780,
      memberPrice: 1450,
      isActive: true,
    },
    {
      name: { en: 'Burlington Check-In Luggage', zh: 'Burlington 託運箱' },
      brand: 'Timothy London',
      regularPrice: 9445,
      memberPrice: 6611,
      isActive: true,
    },
    {
      name: { en: 'Burlington Cabin Luggage', zh: 'Burlington 登機箱' },
      brand: 'Timothy London',
      regularPrice: 8388,
      memberPrice: 5871,
      isActive: true,
    },
    {
      name: { en: 'Cavendish Check-In Luggage', zh: 'Cavendish 託運箱' },
      brand: 'Timothy London',
      regularPrice: 9487,
      memberPrice: 4743,
      isActive: true,
    },
    {
      name: { en: 'Cavendish Cabin Luggage', zh: 'Cavendish 登機箱' },
      brand: 'Timothy London',
      regularPrice: 8427,
      memberPrice: 4213,
      isActive: true,
    },
    {
      name: { en: 'Vice Golf Balls', zh: 'Vice 高爾夫球' },
      brand: 'Vice Golf',
      regularPrice: 470,
      memberPrice: 380,
      isActive: true,
    },
    {
      name: { en: 'Pro V1 Golf Balls', zh: 'Pro V1 高爾夫球' },
      brand: 'Titleist',
      regularPrice: 485,
      memberPrice: 400,
      isActive: true,
    },
    {
      name: { en: 'TP5 Golf Balls', zh: 'TP5 高爾夫球' },
      brand: 'TaylorMade',
      regularPrice: 485,
      memberPrice: 400,
      isActive: true,
    },
  ],
  hotline: '(852) 3582 3088',
};

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

    if (!doc) {
      doc = new Eshop(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 Eshop.findOne();

      if (!doc) {
        doc = new Eshop(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;
