defmodule Towerops.Repo.Migrations.CreateOrganizationInvitations do
  use Ecto.Migration

  def change do
    create table(:organization_invitations, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :organization_id, references(:organizations, type: :binary_id, on_delete: :delete_all),
        null: false

      add :email, :string, null: false
      add :role, :string, null: false
      add :token, :string, null: false
      add :invited_by_id, references(:users, type: :binary_id, on_delete: :nilify_all)
      add :accepted_at, :utc_datetime
      add :accepted_by_id, references(:users, type: :binary_id, on_delete: :nilify_all)
      add :expires_at, :utc_datetime, null: false

      timestamps(type: :utc_datetime)
    end

    create unique_index(:organization_invitations, [:token])
    create index(:organization_invitations, [:organization_id])
    create index(:organization_invitations, [:email])
  end
end
