create table if not exists assets ( id text primary key, owner_id text not null, kind text not null, name text not null, url text not null, storage_path text, source text not null, tags text[] not null default '{}', metadata jsonb not null default '{}'::jsonb, created_at timestamptz not null default now(), updated_at timestamptz not null default now() ); create table if not exists generation_jobs ( id text primary key, owner_id text not null, capability text not null, provider text not null, req_key text not null, status text not null, prompt text, input_asset_ids text[] not null default '{}', input_urls text[] not null default '{}', output_asset_ids text[] not null default '{}', provider_task_id text, request_payload jsonb not null default '{}'::jsonb, response_payload jsonb, error jsonb, retry_of text, created_at timestamptz not null default now(), updated_at timestamptz not null default now() ); create table if not exists usage_events ( id text primary key, owner_id text not null, job_id text not null references generation_jobs(id) on delete cascade, capability text not null, quantity integer not null default 1, estimated_unit text not null default 'image', created_at timestamptz not null default now() ); create table if not exists projects ( id text primary key, owner_id text not null, name text not null, brief text not null default '', type text not null default 'custom', asset_ids text[] not null default '{}', created_at timestamptz not null default now(), updated_at timestamptz not null default now() ); create index if not exists assets_owner_created_idx on assets(owner_id, created_at desc); create index if not exists generation_jobs_owner_created_idx on generation_jobs(owner_id, created_at desc); create index if not exists generation_jobs_status_idx on generation_jobs(status); create index if not exists usage_events_owner_created_idx on usage_events(owner_id, created_at desc);