create table if not exists demo_runs (
    id varchar(64) primary key,
    status varchar(32) not null,
    current_step varchar(64),
    progress int not null default 0,
    task_id varchar(64),
    summary jsonb not null default '{}'::jsonb,
    cancel_requested boolean not null default false,
    started_at timestamptz not null,
    completed_at timestamptz,
    error_message text
);

create table if not exists demo_run_events (
    id varchar(64) primary key,
    run_id varchar(64) not null references demo_runs(id),
    step_key varchar(64) not null,
    step_name varchar(128) not null,
    status varchar(32) not null,
    progress int not null,
    title varchar(256) not null,
    message text,
    entity_type varchar(64),
    entity_id varchar(64),
    metrics jsonb not null default '{}'::jsonb,
    visual_payload jsonb not null default '{}'::jsonb,
    created_at timestamptz not null
);

create index if not exists idx_demo_runs_status on demo_runs(status);
create index if not exists idx_demo_run_events_run on demo_run_events(run_id, created_at);
