create schema if not exists kbx;

create table if not exists kbx.user_suggestions (
    id uuid primary key,
    tenant_id uuid not null,
    user_id uuid not null,
    category varchar(32) not null check (category in ('inconvenience','bug','improvement')),
    message varchar(2000) not null,
    screen_id varchar(64) not null,
    screen_version varchar(32) not null,
    route varchar(512) not null,
    app_version varchar(64) not null,
    user_role varchar(128) not null,
    context jsonb null,
    status varchar(32) not null default 'NEW',
    created_at timestamptz not null default now()
);
create index if not exists ix_user_suggestions_screen_created
    on kbx.user_suggestions(screen_id, created_at desc);
create index if not exists ix_user_suggestions_status_created
    on kbx.user_suggestions(status, created_at desc);

-- AI telemetry intentionally excludes the user's full question and business payload.
create table if not exists kbx.ai_interactions (
    id uuid primary key,
    tenant_id uuid not null,
    user_id uuid not null,
    screen_id varchar(64) not null,
    screen_version varchar(32) not null,
    capability varchar(32) not null,
    result_kind varchar(32) not null,
    provider varchar(64) null,
    duration_ms integer null,
    created_at timestamptz not null default now()
);
create index if not exists ix_ai_interactions_screen_created
    on kbx.ai_interactions(screen_id, created_at desc);
