-- NEO-146: per-player contract instance rows (prototype E7.M4 Slice 4).
CREATE TABLE IF NOT EXISTS contract_instance (
    contract_instance_id TEXT NOT NULL PRIMARY KEY,
    player_id TEXT NOT NULL REFERENCES player_position(player_id) ON DELETE CASCADE,
    template_id TEXT NOT NULL,
    status TEXT NOT NULL CHECK (status IN ('active', 'completed', 'expired')),
    seed_bucket TEXT NOT NULL,
    issued_at TIMESTAMPTZ NOT NULL,
    completed_at TIMESTAMPTZ,
    updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE UNIQUE INDEX IF NOT EXISTS idx_contract_instance_one_active_per_player
    ON contract_instance (player_id)
    WHERE status = 'active';

COMMENT ON TABLE contract_instance IS 'Persisted contract instances per player (NEO-146); at most one active row per player.';
