import { Injectable } from '@nestjs/common';
import axios from 'axios';

@Injectable()
export class EmailAppointmentCancelledService {
  async appointment_cancelled(email: string, name: string, date: Date, reason: string) {

    const url = process.env.MSG_EMAIL_SEND_EMAIL_API;

    const headers = {
      accept: 'application/json',
      'content-type': 'application/json',
      authkey: process.env.MSG_EMAIL_SEND_EMAIL_AUTHKEY
    }

    const data = {
      "recipients": [
        {
          "to": [
            {
              "email": email,
              "name": name
            }
          ],
          "variables": { "var": name, "var1": date, "var2": reason }
        }
      ],
      "from": {
        "email": process.env.MSG_EMAIL_SEND_EMAIL_FROM_EMAIL
      },
      "domain": process.env.MSG_EMAIL_SEND_EMAIL_DOMAIN,
      "template_id": process.env.MSG_APPT_CANCEL_TEMPLATE_ID
    }
    try {
      const response = await axios.post(url, data, { headers });

      return response.data;
    } catch (error) {
      console.log(error);

      return error;

    }
  }
}
