defmodule Berrypod.Mailer.Adapters do
  @moduledoc """
  Registry of supported email adapters and their config shapes.

  Each adapter entry defines the fields needed for configuration.
  The UI renders dynamically from this registry. Adding a new adapter
  is just adding an entry here — no other code changes needed.
  """

  alias Berrypod.Mailer.{Adapter, Field}

  # Ordered by capability: transactional + marketing first,
  # then transactional only, then self-hosted.
  @adapters [
    %Adapter{
      key: "smtp",
      name: "SMTP",
      module: Swoosh.Adapters.SMTP,
      description: "Connect to any email server via SMTP. Works with most providers and hosts.",
      tags: ["All email", "Any provider"],
      fields: [
        %Field{key: "relay", label: "Server host", type: :string, required: true},
        %Field{key: "port", label: "Port", type: :integer, default: 587},
        %Field{key: "username", label: "Username", type: :string},
        %Field{key: "password", label: "Password", type: :secret}
      ]
    },
    %Adapter{
      key: "sendgrid",
      name: "SendGrid",
      module: Swoosh.Adapters.Sendgrid,
      description: "Generous free tier, widely used.",
      tags: ["All email", "US"],
      url: "https://sendgrid.com",
      fields: [
        %Field{key: "api_key", label: "API key", type: :secret, required: true}
      ]
    },
    %Adapter{
      key: "brevo",
      name: "Brevo",
      module: Swoosh.Adapters.Brevo,
      description: "All-in-one platform, GDPR-friendly.",
      tags: ["All email", "France", "GDPR"],
      url: "https://www.brevo.com",
      fields: [
        %Field{key: "api_key", label: "API key", type: :secret, required: true}
      ]
    },
    %Adapter{
      key: "mailjet",
      name: "Mailjet",
      module: Swoosh.Adapters.Mailjet,
      description: "EU data processing, good free tier.",
      tags: ["All email", "France", "GDPR"],
      url: "https://www.mailjet.com",
      fields: [
        %Field{key: "api_key", label: "API key", type: :secret, required: true},
        %Field{key: "secret", label: "Secret key", type: :secret, required: true}
      ]
    },
    %Adapter{
      key: "mailersend",
      name: "MailerSend",
      module: Swoosh.Adapters.MailerSend,
      description: "Generous free tier, good analytics dashboard.",
      tags: ["All email", "EU option"],
      url: "https://www.mailersend.com",
      fields: [
        %Field{key: "api_key", label: "API key", type: :secret, required: true}
      ]
    },
    %Adapter{
      key: "resend",
      name: "Resend",
      module: Swoosh.Adapters.Resend,
      description: "Developer-friendly API, simple setup.",
      tags: ["Transactional", "US"],
      url: "https://resend.com",
      fields: [
        %Field{key: "api_key", label: "API key", type: :secret, required: true}
      ]
    },
    %Adapter{
      key: "mailgun",
      name: "Mailgun",
      module: Swoosh.Adapters.Mailgun,
      description: "EU region option available.",
      tags: ["Transactional", "EU option", "Sweden"],
      url: "https://www.mailgun.com",
      fields: [
        %Field{key: "api_key", label: "API key", type: :secret, required: true},
        %Field{key: "domain", label: "Domain", type: :string, required: true}
      ]
    },
    %Adapter{
      key: "postmark",
      name: "Postmark",
      module: Swoosh.Adapters.Postmark,
      description: "Excellent deliverability tracking.",
      tags: ["Transactional", "US"],
      url: "https://postmarkapp.com",
      fields: [
        %Field{key: "api_key", label: "API key", type: :secret, required: true}
      ]
    },
    %Adapter{
      key: "mailpace",
      name: "MailPace",
      module: Swoosh.Adapters.MailPace,
      description: "Privacy-focused, simple API.",
      tags: ["Transactional", "UK"],
      url: "https://mailpace.com",
      fields: [
        %Field{key: "api_key", label: "API key", type: :secret, required: true}
      ]
    },
    %Adapter{
      key: "postal",
      name: "Postal",
      module: Swoosh.Adapters.Postal,
      description: "Full control over your email infrastructure.",
      tags: ["All email", "Self-hosted", "Open source"],
      url: "https://docs.postalserver.io",
      fields: [
        %Field{key: "api_key", label: "API key", type: :secret, required: true},
        %Field{key: "base_url", label: "Server URL", type: :string, required: true}
      ]
    }
  ]

  @doc "Returns all supported adapters."
  def all, do: @adapters

  @doc "Returns an adapter by its string key, or nil."
  def get(key) when is_binary(key) do
    Enum.find(@adapters, &(&1.key == key))
  end

  @doc "Returns the namespaced settings key for an adapter field."
  def settings_key(adapter_key, field_key) do
    "email_#{adapter_key}_#{field_key}"
  end

  @doc "Returns the settings keys for an adapter's fields."
  def field_keys(%{key: adapter_key, fields: fields}) do
    Enum.map(fields, &settings_key(adapter_key, &1.key))
  end

  @doc "Returns all possible settings keys across all adapters."
  def all_field_keys do
    Enum.flat_map(@adapters, &field_keys/1)
  end
end
