import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { EmailLog, EmailDeliveryStatus } from '../../domain/entities/email-log.entity';
import { EmailVerificationType } from '../../domain/entities/email-verification.entity';
import { IEmailLogRepository } from '../../domain/repositories/email-log.repository.interface';

@Injectable()
export class EmailLogRepository implements IEmailLogRepository {
  constructor(
    @InjectRepository(EmailLog)
    private readonly repo: Repository<EmailLog>,
  ) {}

  async create(data: {
    email: string;
    type: EmailVerificationType;
    status: EmailDeliveryStatus;
    provider?: string;
    providerId?: string;
    errorMsg?: string;
    userId?: string;
  }): Promise<EmailLog> {
    const entity = this.repo.create({
      email: data.email,
      type: data.type,
      status: data.status,
      provider: data.provider ?? null,
      providerId: data.providerId ?? null,
      errorMsg: data.errorMsg ?? null,
      userId: data.userId ?? null,
    });
    return this.repo.save(entity);
  }
}
