defmodule Tarakan.Repo.Migrations.AddReproductionToFindingChecks do
  use Ecto.Migration

  def change do
    alter table(:canonical_finding_checks) do
      # Client-attested reproduction. The worker runs the PoC on its own machine
      # and reports the outcome; the artifact and runtime are stored verbatim so
      # a server-side runner can later re-execute the identical thing without a
      # schema change.
      add :repro_status, :string
      add :repro_runtime, :string
      add :repro_artifact, :text
      add :repro_transcript, :text
    end

    create constraint(:canonical_finding_checks, :canonical_finding_checks_repro_status_valid,
             check: "repro_status IS NULL OR repro_status IN ('pass','fail','inconclusive')"
           )

    # A reproduction is only meaningful with something to re-run.
    create constraint(:canonical_finding_checks, :canonical_finding_checks_repro_needs_artifact,
             check: "repro_status IS NULL OR repro_artifact IS NOT NULL"
           )

    create index(:canonical_finding_checks, [:canonical_finding_id, :repro_status])
  end
end
