defmodule ApiWeb.ShareController do
  use ApiWeb, :controller
  require Logger

  def index(conn, params) do
    Envar.load(".env")
    Envar.require_env_file(".env")

    idsite = Envar.get("IDSITE")
    Logger.info("parametros mail: #{inspect(params)}")
    # Logger.debug("parametros mail: #{params}")
    data = params["data"]

    json = %{
      accessionnumber: data["accession"],
      patientid: data["patientid"],
      vencimiento: DateTime.add(DateTime.utc_now(), 2 * 24 * 60 * 60, :second)
    }

    studydata = %{
      name: data["patientName"],
      fromEmail: params["senderemail"],
      email: params["email"],
      lStudyDate: data["studyDate"],
      lSiteName: Envar.get("FROM"),
      json: json,
      idsite: idsite
    }

    email = Api.UserEmailMailer.send_email(studydata)
    Api.Mailer.deliver(email)

    json(conn, json)
  end
end
