-- +goose Up

CREATE TYPE bootstrap_materialization_status AS ENUM ('not_started', 'pending', 'running', 'succeeded', 'failed');
CREATE TYPE background_job_status AS ENUM ('pending', 'running', 'succeeded', 'failed');

ALTER TABLE installations
  ADD COLUMN IF NOT EXISTS materialization_status bootstrap_materialization_status NOT NULL DEFAULT 'not_started',
  ADD COLUMN IF NOT EXISTS materialization_error TEXT,
  ADD COLUMN IF NOT EXISTS materialization_requested_at TIMESTAMPTZ,
  ADD COLUMN IF NOT EXISTS materialization_started_at TIMESTAMPTZ,
  ADD COLUMN IF NOT EXISTS materialization_finished_at TIMESTAMPTZ;

UPDATE installations
SET
  materialization_status = CASE
    WHEN is_bootstrapped THEN 'succeeded'::bootstrap_materialization_status
    ELSE 'not_started'::bootstrap_materialization_status
  END,
  materialization_error = NULL,
  materialization_requested_at = CASE
    WHEN is_bootstrapped THEN COALESCE(bootstrapped_at, created_at, NOW())
    ELSE NULL
  END,
  materialization_started_at = CASE
    WHEN is_bootstrapped THEN COALESCE(bootstrapped_at, created_at, NOW())
    ELSE NULL
  END,
  materialization_finished_at = CASE
    WHEN is_bootstrapped THEN COALESCE(bootstrapped_at, created_at, NOW())
    ELSE NULL
  END
WHERE materialization_status = 'not_started'::bootstrap_materialization_status;

CREATE TABLE IF NOT EXISTS background_jobs (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  kind TEXT NOT NULL,
  status background_job_status NOT NULL DEFAULT 'pending',
  payload JSONB NOT NULL DEFAULT '{}'::jsonb,
  attempts INTEGER NOT NULL DEFAULT 0,
  max_attempts INTEGER NOT NULL DEFAULT 1,
  available_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  started_at TIMESTAMPTZ,
  finished_at TIMESTAMPTZ,
  last_error TEXT,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_background_jobs_claim ON background_jobs (status, available_at, created_at);

-- +goose Down

DROP INDEX IF EXISTS idx_background_jobs_claim;
DROP TABLE IF EXISTS background_jobs;

ALTER TABLE installations
  DROP COLUMN IF EXISTS materialization_finished_at,
  DROP COLUMN IF EXISTS materialization_started_at,
  DROP COLUMN IF EXISTS materialization_requested_at,
  DROP COLUMN IF EXISTS materialization_error,
  DROP COLUMN IF EXISTS materialization_status;

DROP TYPE IF EXISTS background_job_status;
DROP TYPE IF EXISTS bootstrap_materialization_status;
