import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import { firstValueFrom } from 'rxjs';

@Injectable()
export class NotifyService {
    constructor(private readonly httpService: HttpService) {}

    async sendPostRequest(emailDestinaires: string, contenu: string, object: string) {
        try {
            const url = process.env.EMAIL_URL;
            const payload = {
                    recipients: [emailDestinaires],
                    body: contenu,
                    subject: object
                };
            
            const headers = {
                'Content-Type': 'application/json'
                };
                
            const response = await firstValueFrom(
            this.httpService.post(String(url), JSON.stringify(payload), { headers }),
            );
        
            return await response.data;
        } catch (error) {
            console.log(error);
            
            return error.message;
        }
    }
}
