defmodule Berrypod.MailerTest do
  use Berrypod.DataCase, async: false

  alias Berrypod.Mailer
  alias Berrypod.Settings

  setup do
    # Store original config to restore after each test
    original = Application.get_env(:berrypod, Mailer)
    on_exit(fn -> Application.put_env(:berrypod, Mailer, original) end)
    :ok
  end

  describe "email_configured?/0" do
    test "returns false with Local adapter" do
      Application.put_env(:berrypod, Mailer, adapter: Swoosh.Adapters.Local)
      refute Mailer.email_configured?()
    end

    test "returns true with a real adapter" do
      Application.put_env(:berrypod, Mailer, adapter: Swoosh.Adapters.Postmark, api_key: "test")
      assert Mailer.email_configured?()
    end
  end

  describe "load_config/0" do
    test "loads adapter config from settings" do
      Settings.put_setting("email_adapter", "postmark")
      Settings.put_secret("email_postmark_api_key", "pm_test_key_123")

      Mailer.load_config()

      config = Application.get_env(:berrypod, Mailer)
      assert config[:adapter] == Swoosh.Adapters.Postmark
      assert config[:api_key] == "pm_test_key_123"
    end

    test "loads SMTP config with multiple fields" do
      Settings.put_setting("email_adapter", "smtp")
      Settings.put_setting("email_smtp_relay", "smtp.example.com")
      Settings.put_setting("email_smtp_port", 465, "integer")
      Settings.put_setting("email_smtp_username", "user@example.com")
      Settings.put_secret("email_smtp_password", "secret123")

      Mailer.load_config()

      config = Application.get_env(:berrypod, Mailer)
      assert config[:adapter] == Swoosh.Adapters.SMTP
      assert config[:relay] == "smtp.example.com"
      assert config[:port] == 465
      assert config[:username] == "user@example.com"
      assert config[:password] == "secret123"
    end

    test "is a no-op when no email_adapter is set" do
      original = Application.get_env(:berrypod, Mailer)
      Mailer.load_config()
      assert Application.get_env(:berrypod, Mailer) == original
    end
  end

  describe "email_verified?/0" do
    test "returns false when adapter is Local" do
      Application.put_env(:berrypod, Mailer, adapter: Swoosh.Adapters.Local)
      Mailer.mark_email_verified()
      refute Mailer.email_verified?()
    end

    test "returns false when configured but not verified" do
      Application.put_env(:berrypod, Mailer, adapter: Swoosh.Adapters.Postmark, api_key: "test")
      Mailer.clear_email_verified()
      refute Mailer.email_verified?()
    end

    test "returns true when configured and verified" do
      Application.put_env(:berrypod, Mailer, adapter: Swoosh.Adapters.Postmark, api_key: "test")
      Mailer.mark_email_verified()
      assert Mailer.email_verified?()
    end

    test "clear_email_verified resets the flag" do
      Application.put_env(:berrypod, Mailer, adapter: Swoosh.Adapters.Postmark, api_key: "test")
      Mailer.mark_email_verified()
      assert Mailer.email_verified?()

      Mailer.clear_email_verified()
      refute Mailer.email_verified?()
    end
  end

  describe "current_config/0" do
    test "returns {nil, %{}} when no adapter configured" do
      Application.put_env(:berrypod, Mailer, adapter: Swoosh.Adapters.Local)
      assert {nil, %{}} = Mailer.current_config()
    end

    test "returns adapter key and config when configured from settings" do
      Settings.put_setting("email_adapter", "postmark")
      Settings.put_secret("email_postmark_api_key", "pm_test_key_123")

      Mailer.load_config()

      {adapter_key, config} = Mailer.current_config()
      assert adapter_key == "postmark"
      assert config["api_key"] =~ "•••"
    end
  end
end
