import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { EmailTemplate } from './email-template.schema';

@Injectable()
export class EmailTemplatesService {
  constructor(
    @InjectModel(EmailTemplate.name)
    private readonly model: Model<EmailTemplate>,
  ) {}

  async getByKey(key: string): Promise<EmailTemplate> {
    const template = await this.model.findOne({
      key,
      isActive: true,
    });

    if (!template) {
      throw new NotFoundException(`Email template not found for key: ${key}`);
    }

    return template;
  }
}
