alter table seeds
add column if not exists semantic_run_id uuid references runs(id) on delete set null,
add column if not exists cluster_id text,
add column if not exists cluster_title text,
add column if not exists priority text,
add column if not exists updated_at timestamptz not null default now();

create unique index if not exists seeds_project_semantic_phrase_cluster_idx
on seeds(project_id, semantic_run_id, lower(phrase), coalesce(cluster_id, ''));

alter table wordstat_jobs
add column if not exists semantic_run_id uuid references runs(id) on delete set null,
add column if not exists provider text not null default 'disabled',
add column if not exists mode text not null default 'disabled',
add column if not exists region text not null default 'ru',
add column if not exists requested_phrases jsonb not null default '[]'::jsonb,
add column if not exists result_count integer not null default 0,
add column if not exists error_message text,
add column if not exists started_at timestamptz,
add column if not exists updated_at timestamptz not null default now();

create index if not exists wordstat_jobs_project_semantic_created_idx
on wordstat_jobs(project_id, semantic_run_id, created_at desc);

alter table wordstat_results
add column if not exists seed_id uuid references seeds(id) on delete set null,
add column if not exists source_phrase text,
add column if not exists normalized_phrase text,
add column if not exists region text,
add column if not exists position integer;

create index if not exists wordstat_results_job_seed_idx
on wordstat_results(job_id, seed_id);

create index if not exists wordstat_results_phrase_idx
on wordstat_results(lower(phrase));
