package io.ljunggren.sendgrid.api.service;

import com.sendgrid.SendGrid;

/**
 * Base class shared by SendGrid service classes. It owns the
 * underlying {@link SendGrid} client and validates the API key.
 */
public class BaseService {

	protected SendGrid sendGrid;

	/**
	 * Create a new service backed by a real SendGrid client.
	 *
	 * @param apiKey the SendGrid API key. Must be non-null and non-blank.
	 * @throws IllegalArgumentException if {@code apiKey} is null or blank
	 */
	protected BaseService(String apiKey) {
		if (apiKey == null || apiKey.isBlank()) {
			throw new IllegalArgumentException("apiKey must not be null or blank");
		}
		this.sendGrid = new SendGrid(apiKey);
	}

	/**
	 * Package-private constructor used by tests to inject a mock
	 * {@link SendGrid} client without contacting the real service.
	 *
	 * @param sendGrid pre-built (potentially mocked) SendGrid client
	 */
	BaseService(SendGrid sendGrid) {
		if (sendGrid == null) {
			throw new IllegalArgumentException("sendGrid must not be null");
		}
		this.sendGrid = sendGrid;
	}

}
