-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS repos (
    id              TEXT PRIMARY KEY,
    name            TEXT NOT NULL,
    remote_url      TEXT NOT NULL,
    default_branch  TEXT NOT NULL,
    workspace_root  TEXT NOT NULL,
    credential_ref  TEXT,
    created_at      TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at      TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS workspace_leases (
    id          TEXT PRIMARY KEY,
    repo_id     TEXT NOT NULL REFERENCES repos(id),
    slot        TEXT NOT NULL,
    path        TEXT NOT NULL,
    state       TEXT NOT NULL,
    expires_at  TIMESTAMPTZ,
    created_at  TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at  TIMESTAMPTZ NOT NULL DEFAULT now(),
    UNIQUE (repo_id, slot)
);

CREATE TABLE IF NOT EXISTS operations (
    id               TEXT PRIMARY KEY,
    repo_id          TEXT NOT NULL REFERENCES repos(id),
    type             TEXT NOT NULL,
    state            TEXT NOT NULL,
    idempotency_key  TEXT,
    created_by       TEXT NOT NULL,
    created_at       TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at       TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE UNIQUE INDEX IF NOT EXISTS ux_operations_idempotency
    ON operations (idempotency_key)
    WHERE idempotency_key IS NOT NULL;

CREATE TABLE IF NOT EXISTS operation_events (
    id            TEXT PRIMARY KEY,
    operation_id  TEXT REFERENCES operations(id),
    type          TEXT NOT NULL,
    subject       TEXT NOT NULL,
    payload       JSONB NOT NULL DEFAULT '{}'::jsonb,
    created_at    TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS revision_cursors (
    repo_id      TEXT NOT NULL REFERENCES repos(id),
    branch       TEXT NOT NULL,
    revision     TEXT NOT NULL,
    observed_at  TIMESTAMPTZ NOT NULL DEFAULT now(),
    PRIMARY KEY (repo_id, branch)
);
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS revision_cursors;
DROP TABLE IF EXISTS operation_events;
DROP TABLE IF EXISTS operations;
DROP TABLE IF EXISTS workspace_leases;
DROP TABLE IF EXISTS repos;
-- +goose StatementEnd

