import { prisma } from '../../../config/database';
import type { UpdateMapSettingsInput } from './settings.schemas';

export const mapSettingsService = {
  async get() {
    let settings = await prisma.mapSettings.findFirst();
    if (!settings) {
      settings = await prisma.mapSettings.create({
        data: {
          latitude: 45.4215,
          longitude: -75.6972,
          zoom: 12,
        },
      });
    }
    return settings;
  },

  async update(data: UpdateMapSettingsInput) {
    const existing = await prisma.mapSettings.findFirst();
    if (existing) {
      return prisma.mapSettings.update({
        where: { id: existing.id },
        data,
      });
    }
    return prisma.mapSettings.create({
      data: {
        latitude: data.latitude ?? 45.4215,
        longitude: data.longitude ?? -75.6972,
        zoom: data.zoom ?? 12,
        ...data,
      },
    });
  },
};
