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
    );

    create table if not exists rubric_versions (
      id text primary key,
      version text not null,
      name text not null,
      dimensions text not null,
      formula text not null,
      is_active integer not null,
      created_at text not null
    );

    create table if not exists scoring_runs (
      id text primary key,
      job_id text not null,
      revision integer not null,
      rubric_version_id text not null,
      dimension_scores text not null,
      composite_score real not null,
      rationale text not null,
      created_at text not null,
      foreign key (job_id, revision)
        references optimized_articles(job_id, revision) on delete cascade,
      foreign key (rubric_version_id) references rubric_versions(id)
    );

    create table if not exists publication_records (
      id text primary key,
      job_id text not null,
      revision integer not null,
      platform text not null,
      url text not null,
      published_at text not null,
      status text not null,
      notes text not null,
      created_at text not null,
      updated_at text not null,
      foreign key (job_id, revision)
        references optimized_articles(job_id, revision) on delete cascade
    );

    create table if not exists performance_snapshots (
      id text primary key,
      publication_id text not null,
      source text not null,
      window_label text not null,
      metrics text not null,
      feedback_summary text not null,
      raw_reference text,
      snapshot_at text not null,
      foreign key (publication_id) references publication_records(id) on delete cascade
    );

    create table if not exists calibration_events (
      id text primary key,
      publication_id text not null,
      scoring_run_id text not null,
      performance_snapshot_id text not null,
      direction text not null,
      observations text not null,
      recommended_action text not null,
      created_at text not null,
      foreign key (publication_id) references publication_records(id) on delete cascade,
      foreign key (scoring_run_id) references scoring_runs(id) on delete cascade,
      foreign key (performance_snapshot_id)
        references performance_snapshots(id) on delete cascade
    );

    create index if not exists idx_scoring_runs_job_revision
      on scoring_runs(job_id, revision);

    create index if not exists idx_publication_records_job_revision
      on publication_records(job_id, revision);

    create index if not exists idx_performance_snapshots_publication
      on performance_snapshots(publication_id);
  `);
}
