/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */
package org.elasticsearch.xpack.notification.email;

import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsException;
import org.elasticsearch.test.ESTestCase;

import java.util.Collections;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isOneOf;
import static org.hamcrest.Matchers.notNullValue;

public class AccountsTests extends ESTestCase {
    public void testSingleAccount() throws Exception {
        Settings.Builder builder = Settings.builder()
                .put("default_account", "account1");
        addAccountSettings("account1", builder);
        EmailService service = new EmailService(builder.build(), null,
                new ClusterSettings(Settings.EMPTY, Collections.singleton(EmailService.EMAIL_ACCOUNT_SETTING)));
        Account account = service.getAccount("account1");
        assertThat(account, notNullValue());
        assertThat(account.name(), equalTo("account1"));
        account = service.getAccount(null); // falling back on the default
        assertThat(account, notNullValue());
        assertThat(account.name(), equalTo("account1"));
    }

    public void testSingleAccountNoExplicitDefault() throws Exception {
        Settings.Builder builder = Settings.builder();
        addAccountSettings("account1", builder);
        EmailService service = new EmailService(builder.build(), null,
                new ClusterSettings(Settings.EMPTY, Collections.singleton(EmailService.EMAIL_ACCOUNT_SETTING)));
        Account account = service.getAccount("account1");
        assertThat(account, notNullValue());
        assertThat(account.name(), equalTo("account1"));
        account = service.getAccount(null); // falling back on the default
        assertThat(account, notNullValue());
        assertThat(account.name(), equalTo("account1"));
    }

    public void testMultipleAccounts() throws Exception {
        Settings.Builder builder = Settings.builder()
                .put("xpack.notification.email.default_account", "account1");
        addAccountSettings("account1", builder);
        addAccountSettings("account2", builder);

        EmailService service = new EmailService(builder.build(), null,
                new ClusterSettings(Settings.EMPTY, Collections.singleton(EmailService.EMAIL_ACCOUNT_SETTING)));
        Account account = service.getAccount("account1");
        assertThat(account, notNullValue());
        assertThat(account.name(), equalTo("account1"));
        account = service.getAccount("account2");
        assertThat(account, notNullValue());
        assertThat(account.name(), equalTo("account2"));
        account = service.getAccount(null); // falling back on the default
        assertThat(account, notNullValue());
        assertThat(account.name(), equalTo("account1"));
    }

    public void testMultipleAccountsNoExplicitDefault() throws Exception {
        Settings.Builder builder = Settings.builder()
                .put("default_account", "account1");
        addAccountSettings("account1", builder);
        addAccountSettings("account2", builder);

        EmailService service = new EmailService(builder.build(), null,
                new ClusterSettings(Settings.EMPTY, Collections.singleton(EmailService.EMAIL_ACCOUNT_SETTING)));
        Account account = service.getAccount("account1");
        assertThat(account, notNullValue());
        assertThat(account.name(), equalTo("account1"));
        account = service.getAccount("account2");
        assertThat(account, notNullValue());
        assertThat(account.name(), equalTo("account2"));
        account = service.getAccount(null);
        assertThat(account, notNullValue());
        assertThat(account.name(), isOneOf("account1", "account2"));
    }

    public void testMultipleAccountsUnknownDefault() throws Exception {
        Settings.Builder builder = Settings.builder().put("xpack.notification.email.default_account", "unknown");
        addAccountSettings("account1", builder);
        addAccountSettings("account2", builder);
        ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.singleton(EmailService.EMAIL_ACCOUNT_SETTING));
        SettingsException e = expectThrows(SettingsException.class, () -> new EmailService(builder.build(), null, clusterSettings));
        assertThat(e.getMessage(), is("could not find default account [unknown]"));
    }

    public void testNoAccount() throws Exception {
        Settings.Builder builder = Settings.builder();
        EmailService service = new EmailService(builder.build(), null,
                new ClusterSettings(Settings.EMPTY, Collections.singleton(EmailService.EMAIL_ACCOUNT_SETTING)));
        expectThrows(IllegalArgumentException.class, () -> service.getAccount(null));
    }

    public void testNoAccountWithDefaultAccount() throws Exception {
        Settings settings = Settings.builder().put("xpack.notification.email.default_account", "unknown").build();
        ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.singleton(EmailService.EMAIL_ACCOUNT_SETTING));
        SettingsException e = expectThrows(SettingsException.class, () -> new EmailService(settings, null, clusterSettings));
        assertThat(e.getMessage(), is("could not find default account [unknown]"));
    }

    private void addAccountSettings(String name, Settings.Builder builder) {
        builder.put("xpack.notification.email.account." + name + ".smtp.host", "_host");
    }
}
