import { Request, Response } from 'express';
import SiteSettings from '../schema/site-settings.schema';

const DEFAULTS = {
  key: 'global',
  name: 'mj.Lux Beauty Studio',
  tagline: 'Where Luxury Becomes You',
  established: 'EST. 15.10.25',
  location: 'Spintex Road, Accra, Ghana',
  phone: '020 723 4110',
  phoneRaw: '233207234110',
  email: 'hello@mjluxbeauty.com',
  instagram: '@mjlux_beautystudio',
  facebook: 'mjluxbeautystudio',
  tiktok: '@mjlux_beautystudio',
  twitter: 'mjluxbeautystudio',
  whatsappUrl: 'https://wa.me/233207234110',
  footerAbout:
    'A luxury beauty destination offering premium hair, nails, lashes and spa services in Accra.',
};

async function getOrCreateSettings() {
  let settings = await SiteSettings.findOne({ key: 'global' });
  if (!settings) {
    settings = await SiteSettings.create(DEFAULTS);
  }
  return settings;
}

export class SiteSettingsController {
  static async getPublic(req: Request, res: Response) {
    try {
      const response = await getOrCreateSettings();
      return res.status(200).json({ success: true, response });
    } catch {
      return res.status(500).json({ success: false, message: 'System error' });
    }
  }

  static async update(req: Request, res: Response) {
    try {
      const response = await SiteSettings.findOneAndUpdate(
        { key: 'global' },
        { ...req.body, key: 'global' },
        { new: true, upsert: true, setDefaultsOnInsert: true }
      );
      return res.status(200).json({ success: true, response });
    } catch {
      return res.status(500).json({ success: false, message: 'System error' });
    }
  }
}
