package io.ljunggren.sendgrid.api;

import java.util.List;
import java.util.Map;

import io.ljunggren.sendgrid.api.exception.SendgridException;
import io.ljunggren.sendgrid.api.model.Statistics;
import io.ljunggren.sendgrid.api.service.Aggregation;
import io.ljunggren.sendgrid.api.service.EmailService;
import io.ljunggren.sendgrid.api.service.UsageService;

/**
 * Public facade for the SendGrid REST API. Wraps the email
 * sending and usage statistics services so callers only need a
 * single API key and a single object.
 *
 * <p>Example:
 * <pre>{@code
 * SendgridApi api = new SendgridApi(apiKey);
 * api.sendEmail(from, to, subject, body);
 * }</pre>
 */
public class SendgridApi {

	private final EmailService emailService;
	private final UsageService usageService;

	/**
	 * Create a new {@code SendgridApi} using the given API key.
	 *
	 * @param apiKey the SendGrid API key
	 * @throws IllegalArgumentException if {@code apiKey} is null or blank
	 */
	public SendgridApi(String apiKey) {
		this(new EmailService(apiKey), new UsageService(apiKey));
	}

	/**
	 * Package-private constructor primarily used by tests to
	 * inject mock services.
	 *
	 * @param emailService email service
	 * @param usageService usage service
	 */
	SendgridApi(EmailService emailService, UsageService usageService) {
		if (emailService == null || usageService == null) {
			throw new IllegalArgumentException("emailService and usageService must not be null");
		}
		this.emailService = emailService;
		this.usageService = usageService;
	}

	/**
	 * Send a plain-text email.
	 *
	 * @param from    sender address
	 * @param to      recipient address
	 * @param subject email subject
	 * @param body    plain-text body
	 * @throws SendgridException if the email cannot be sent
	 */
	public void sendEmail(String from, String to, String subject, String body) throws SendgridException {
		emailService.sendEmail(from, to, subject, body);
	}

	/**
	 * Send a SendGrid template email.
	 *
	 * @param templateId   SendGrid template id
	 * @param from         sender address
	 * @param to           recipient address
	 * @param templateData substitution variables
	 * @throws SendgridException if the email cannot be sent
	 */
	public void sendTemplateEmail(String templateId, String from, String to, Map<String, Object> templateData) throws SendgridException {
		emailService.sendTemplateEmail(templateId, from, to, templateData);
	}

	/**
	 * @return statistics for today only
	 * @throws SendgridException on error
	 */
	public List<Statistics> getDailyUsage() throws SendgridException {
		return usageService.getDailyUsage();
	}

	/**
	 * @return daily statistics for the current calendar month so far
	 * @throws SendgridException on error
	 */
	public List<Statistics> getMonthlyStatistics() throws SendgridException {
		return usageService.getMonthlyStatistics();
	}

	/**
	 * @param days number of days of history (inclusive of today)
	 * @return daily statistics for the last {@code days} days
	 * @throws SendgridException on error
	 */
	public List<Statistics> getStatistics(int days) throws SendgridException {
		return usageService.getStatistics(days);
	}

	/**
	 * @param days        number of days of history (inclusive of today)
	 * @param aggregation aggregation granularity
	 * @return statistics for the last {@code days} days, aggregated as requested
	 * @throws SendgridException on error
	 */
	public List<Statistics> getStatistics(int days, Aggregation aggregation) throws SendgridException {
		return usageService.getStatistics(days, aggregation);
	}

}
