import type Database from "better-sqlite3";

export function initializeSchema(db: Database.Database) {
  db.exec(`
    create table if not exists brand_templates (
      id text primary key,
      brand_name text not null,
      company_full_name text not null,
      company_short_names text not null,
      product_names text not null,
      target_industries text not null,
      target_audience text not null,
      verified_claims text not null,
      forbidden_claims text not null,
      tone_rules text not null,
      created_at text not null,
      updated_at text not null
    );

    create table if not exists article_jobs (
      id text primary key,
      brand_template_id text,
      source_title text not null,
      source_body text not null,
      image_inputs text not null,
      publish_platform text not null,
      user_instructions text not null,
      status text not null,
      export_paths text not null,
      created_at text not null,
      updated_at text not null,
      foreign key (brand_template_id) references brand_templates(id)
    );

    create table if not exists fact_cards (
      job_id text primary key,
      source text not null,
      fact_card text not null,
      confirmed_by_user integer not null,
      created_at text not null,
      updated_at text not null,
      foreign key (job_id) references article_jobs(id) on delete cascade
    );

    create table if not exists optimized_articles (
      job_id text not null,
      revision integer not null,
      article text not null,
      created_at text not null,
      primary key (job_id, revision),
      foreign key (job_id) references article_jobs(id) on delete cascade
    );

    create table if not exists qa_reports (
      job_id text not null,
      revision integer not null,
      report text not null,
      created_at text not null,
      primary key (job_id, revision),
      foreign key (job_id, revision)
        references optimized_articles(job_id, revision) on delete cascade
    );
  `);
}
