import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';

@Injectable()
export class ContactsService {
  constructor(private prisma: PrismaService) {}

  async getFirst() {
    const contact = await this.prisma.contact.findFirst({ orderBy: { id: 'asc' } });
    if (!contact) throw new NotFoundException('Contact not found');
    return contact;
  }

  async upsert(data: { address?: string; phone?: string; email?: string; googleMapEmbed?: string }) {
    const existing = await this.prisma.contact.findFirst({ orderBy: { id: 'asc' } });
    if (existing) {
      return this.prisma.contact.update({ where: { id: existing.id }, data });
    }
    return this.prisma.contact.create({ data });
  }

  async adminFindAll() {
    return this.prisma.contact.findMany({ orderBy: { id: 'asc' } });
  }
}
