defmodule WhoNeedHelp.Repo.Migrations.CreateSupportAndContentRemoval do
  use Ecto.Migration

  def change do
    create table(:support_requests, primary_key: false) do
      add :id, :binary_id, primary_key: true
      add :reference, :string, null: false
      add :kind, :string, null: false
      add :status, :string, null: false, default: "open"
      add :contact_email, :string, null: false
      add :subject, :string, null: false
      add :details, :text, null: false
      add :contact_verified_at, :utc_datetime
      add :resolution_note, :text
      add :reviewed_at, :utc_datetime
      add :response_sent_at, :utc_datetime
      add :requester_id, references(:users, type: :binary_id, on_delete: :nilify_all)
      add :reviewed_by_id, references(:users, type: :binary_id, on_delete: :nilify_all)
      timestamps(type: :utc_datetime)
    end

    create unique_index(:support_requests, [:reference])
    create index(:support_requests, [:status, :inserted_at, :id])
    create index(:support_requests, [:kind, :status, :inserted_at, :id])
    create index(:support_requests, [:requester_id, :inserted_at, :id])

    create table(:content_removal_notices, primary_key: false) do
      add :id, :binary_id, primary_key: true
      add :reference, :string, null: false
      add :regime, :string, null: false, default: "general"
      add :category, :string, null: false
      add :status, :string, null: false, default: "open"
      add :submitter_name, :string
      add :contact_email, :string
      add :relationship, :string
      add :content_locations, :text, null: false
      add :explanation, :text, null: false
      add :legal_basis, :text
      add :contact_verified_at, :utc_datetime
      add :good_faith, :boolean, null: false, default: false
      add :accurate_complete, :boolean, null: false, default: false
      add :electronic_signature, :string
      add :resolution_note, :text
      add :reviewed_at, :utc_datetime
      add :response_due_at, :utc_datetime
      add :acknowledgement_sent_at, :utc_datetime
      add :decision_sent_at, :utc_datetime
      add :requester_id, references(:users, type: :binary_id, on_delete: :nilify_all)
      add :reviewed_by_id, references(:users, type: :binary_id, on_delete: :nilify_all)
      timestamps(type: :utc_datetime)
    end

    create unique_index(:content_removal_notices, [:reference])
    create index(:content_removal_notices, [:status, :inserted_at, :id])
    create index(:content_removal_notices, [:regime, :status, :inserted_at, :id])
    create index(:content_removal_notices, [:requester_id, :inserted_at, :id])
  end
end
