defmodule Berrypod.Repo.Migrations.CreateNewsletter do
  use Ecto.Migration

  def change do
    create table(:newsletter_subscribers, primary_key: false) do
      add :id, :binary_id, primary_key: true
      add :email, :string, null: false
      add :status, :string, null: false, default: "pending"
      add :confirmation_token, :string
      add :confirmed_at, :utc_datetime
      add :unsubscribed_at, :utc_datetime
      add :consent_text, :string
      add :source, :string, default: "website"
      add :ip_hash, :string

      timestamps(type: :utc_datetime)
    end

    create unique_index(:newsletter_subscribers, [:email])
    create index(:newsletter_subscribers, [:status])
    create index(:newsletter_subscribers, [:confirmation_token])

    create table(:newsletter_campaigns, primary_key: false) do
      add :id, :binary_id, primary_key: true
      add :subject, :string, null: false
      add :body, :text, null: false
      add :status, :string, null: false, default: "draft"
      add :scheduled_at, :utc_datetime
      add :sent_at, :utc_datetime
      add :sent_count, :integer, default: 0
      add :failed_count, :integer, default: 0

      timestamps(type: :utc_datetime)
    end

    create index(:newsletter_campaigns, [:status])
    create index(:newsletter_campaigns, [:scheduled_at])
  end
end
