package db

const schema = `
CREATE TABLE IF NOT EXISTS companies (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    ticker TEXT,
    name TEXT NOT NULL,
    cnpj TEXT UNIQUE NOT NULL,
    cvm_code TEXT,
    sector TEXT,
    status TEXT NOT NULL DEFAULT 'ATIVO',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS filings (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    external_id TEXT UNIQUE NOT NULL,
    company_id INTEGER REFERENCES companies(id),
    cnpj TEXT NOT NULL,
    category TEXT NOT NULL,
    type TEXT,
    species TEXT,
    subject TEXT,
    reference_date TEXT,
    delivery_date DATETIME NOT NULL,
    protocol TEXT,
    version TEXT,
    download_url TEXT,
    importance INTEGER DEFAULT 1,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS selic_history (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    date TEXT UNIQUE NOT NULL,
    daily_rate REAL NOT NULL,
    annual_rate REAL,
    target_rate REAL
);

CREATE TABLE IF NOT EXISTS cdi_history (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    date TEXT UNIQUE NOT NULL,
    daily_rate REAL NOT NULL,
    annual_rate REAL
);

CREATE TABLE IF NOT EXISTS ipca_history (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    date TEXT UNIQUE NOT NULL,
    monthly_rate REAL NOT NULL,
    accumulated_12m REAL
);

CREATE TABLE IF NOT EXISTS fx_rates (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    date TEXT NOT NULL,
    pair TEXT NOT NULL,
    rate REAL NOT NULL,
    UNIQUE(date, pair)
);

CREATE VIRTUAL TABLE IF NOT EXISTS companies_fts USING fts5(
    name, ticker, sector, cnpj,
    content='companies', content_rowid='id'
);

CREATE VIRTUAL TABLE IF NOT EXISTS filings_fts USING fts5(
    subject, category, type,
    content='filings', content_rowid='id'
);
`
