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

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

  async getByKey(cityId: number, templateKey: string) {
    const [template, city] = await Promise.all([
      this.prisma.messageTemplate.findUnique({
        where: { cityId_templateKey: { cityId, templateKey } },
      }),
      this.prisma.city.findUnique({ where: { cityId }, select: { name: true } }),
    ]);
    if (!template) throw new NotFoundException(`Template '${templateKey}' not configured`);
    return {
      templateKey: template.templateKey,
      body: template.body,
      cityName: city?.name ?? '',
      updatedAt: template.updatedAt,
    };
  }
}
