-- Users / Profile
CREATE TABLE IF NOT EXISTS profile (
    id INTEGER PRIMARY KEY CHECK (id = 1),
    name TEXT,
    email TEXT,
    location TEXT,
    timezone TEXT,
    salary_target_min REAL,
    salary_target_max REAL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Job postings
CREATE TABLE IF NOT EXISTS jobs (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    url TEXT UNIQUE NOT NULL,
    company TEXT NOT NULL,
    title TEXT NOT NULL,
    description TEXT,
    raw_html TEXT,
    source TEXT, -- "greenhouse", "ashby", "lever", "indeed", etc.
    discovered_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    indexed BOOLEAN DEFAULT 0,
    archived BOOLEAN DEFAULT 0
);

-- Applications / Tracker
CREATE TABLE IF NOT EXISTS applications (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    job_id INTEGER NOT NULL,
    status TEXT NOT NULL DEFAULT 'Evaluated', -- Evaluated, Applied, Responded, Interview, Offer, Rejected, Discarded, SKIP
    score REAL, -- 0-5 rating from Claude
    pdf_generated BOOLEAN DEFAULT 0,
    applied_at TIMESTAMP,
    notes TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (job_id) REFERENCES jobs(id)
);

-- Evaluation reports
CREATE TABLE IF NOT EXISTS reports (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    application_id INTEGER NOT NULL,
    report_md TEXT, -- Full markdown report (A-G blocks)
    report_html TEXT, -- HTML rendering
    generated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (application_id) REFERENCES applications(id)
);

-- Portal scan history (dedup)
CREATE TABLE IF NOT EXISTS scan_history (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    source TEXT NOT NULL, -- portal name
    query_hash TEXT NOT NULL, -- hash of query params
    last_scanned TIMESTAMP,
    jobs_found INTEGER,
    new_jobs INTEGER,
    UNIQUE(source, query_hash)
);

-- User voice samples (from DOCX intake)
CREATE TABLE IF NOT EXISTS voice_samples (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    source_file TEXT, -- DOCX filename uploaded
    transcription TEXT,
    extracted_proof_points TEXT, -- JSON: ["point1", "point2", ...]
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Follow-ups
CREATE TABLE IF NOT EXISTS follow_ups (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    application_id INTEGER NOT NULL,
    follow_up_at TIMESTAMP,
    contacted_at TIMESTAMP,
    notes TEXT,
    FOREIGN KEY (application_id) REFERENCES applications(id)
);

-- Create indexes for performance
CREATE INDEX IF NOT EXISTS idx_jobs_source ON jobs(source);
CREATE INDEX IF NOT EXISTS idx_jobs_company ON jobs(company);
CREATE INDEX IF NOT EXISTS idx_applications_status ON applications(status);
CREATE INDEX IF NOT EXISTS idx_applications_job_id ON applications(job_id);
CREATE INDEX IF NOT EXISTS idx_scan_history_source ON scan_history(source);
CREATE INDEX IF NOT EXISTS idx_follow_ups_application_id ON follow_ups(application_id);
