-- Enable foreign key support
PRAGMA foreign_keys = ON;
PRAGMA journal_mode = WAL;
PRAGMA cache_size = -2000;
PRAGMA mmap_size = 30000000000;


-- proper prosody now
--
-- 
--
CREATE TABLE IF NOT EXISTS languages(
    iso6392 TEXT PRIMARY KEY,
    -- bcp47 TEXT PRIMARY KEY,
    -- iso6393 TEXT NOT NULL,
    english TEXT NOT NULL
    -- native TEXT,
    -- iso6391 TEXT,
    -- iso6395 TEXT,
    -- glottolog TEXT
);

CREATE TABLE IF NOT EXISTS idioms(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    spelling TEXT NOT NULL,
    lang TEXT NOT NULL,
    frequency INTEGER,
    FOREIGN KEY (lang) REFERENCES languages(iso6392),
    CONSTRAINT spell_unique UNIQUE (spelling, lang)
);

CREATE INDEX IF NOT EXISTS idx_idioms_spelling ON idioms(spelling);
CREATE TABLE IF NOT EXISTS words(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    spelling TEXT NOT NULL,
    lang TEXT NOT NULL,
    frequency INTEGER,
    notes TEXT,
    FOREIGN KEY (lang) REFERENCES languages(iso6392),
    CONSTRAINT spell_unique UNIQUE (spelling, lang)
);

CREATE INDEX IF NOT EXISTS idx_words_spelling ON words(spelling);

CREATE TABLE IF NOT EXISTS word_rhymes(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    text TEXT NOT NULL,
    lang TEXT NOT NULL,
    notes TEXT,
    CONSTRAINT wrhyme_unique UNIQUE (text, lang)
);
CREATE TABLE IF NOT EXISTS words_wrhymes(
    word_id INTEGER NOT NULL,
    wrhyme_id INTEGER NOT NULL,
    FOREIGN KEY (word_id) REFERENCES words(id),
    FOREIGN KEY (wrhyme_id) REFERENCES word_rhymes(id)
);

-- break up syllables
CREATE TABLE IF NOT EXISTS tones(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    ipa TEXT NOT NULL,
    lang TEXT NOT NULL,
    name TEXT NOT NULL,
    nums INTEGER NOT NULL,
    CONSTRAINT tone_unique UNIQUE (ipa, lang)
);
CREATE TABLE IF NOT EXISTS onsets(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    ipa  TEXT NOT NULL,
    text TEXT NOT NULL,
    lang TEXT NOT NULL,
    CONSTRAINT onsets_unique UNIQUE (ipa, text, lang)
);
CREATE TABLE IF NOT EXISTS medials(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    ipa  TEXT NOT NULL,
    text TEXT NOT NULL,
    lang TEXT NOT NULL,
    CONSTRAINT onsets_unique UNIQUE (ipa, text, lang)
);
CREATE TABLE IF NOT EXISTS nucleus(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    ipa  TEXT NOT NULL,
    text TEXT NOT NULL,
    lang TEXT NOT NULL,
    CONSTRAINT onsets_unique UNIQUE (ipa, text, lang)
);
CREATE TABLE IF NOT EXISTS codas(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    ipa  TEXT NOT NULL,
    text TEXT NOT NULL,
    lang TEXT NOT NULL,
    CONSTRAINT onsets_unique UNIQUE (ipa, text, lang)
);
CREATE TABLE IF NOT EXISTS rhymes(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    ipa  TEXT NOT NULL,
    text TEXT NOT NULL,
    lang TEXT NOT NULL,
    CONSTRAINT onsets_unique UNIQUE (ipa, text, lang)
);
CREATE TABLE IF NOT EXISTS syllables(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    lang TEXT NOT NULL,
    ipa TEXT NOT NULL,
    long INTEGER NOT NULL,
    text TEXT NOT NULL,
    onset INTEGER  NOT NULL,
    medial INTEGER  NOT NULL,
    nucleus INTEGER  NOT NULL,
    coda INTEGER  NOT NULL,
    rhyme INTEGER  NOT NULL,
    tone INTEGER NOT NULL,
    notes TEXT,
    FOREIGN KEY (lang) REFERENCES languages(iso6392),
    FOREIGN KEY (onset) REFERENCES onsets(id),
    FOREIGN KEY (medial) REFERENCES medials(id),
    FOREIGN KEY (nucleus) REFERENCES nucleus(id),
    FOREIGN KEY (coda) REFERENCES codas(id),
    FOREIGN KEY (rhyme) REFERENCES rhymes(id),
    FOREIGN KEY (tone) REFERENCES tones(id),
    CONSTRAINT syllable_unique UNIQUE (text, ipa, lang)
);


-- join tables

CREATE TABLE IF NOT EXISTS syllables_words(
    syl_id INTEGER NOT NULL,
    word_id INTEGER NOT NULL,
    idx INTEGER NOT NULL,
    stressed INTEGER,
    FOREIGN KEY (syl_id) REFERENCES syllables(id),
    FOREIGN KEY (word_id) REFERENCES words(id)
);


CREATE TABLE IF NOT EXISTS words_idioms(
    word_id INTEGER NOT NULL,
    idiom_id INTEGER NOT NULL,
    FOREIGN KEY (idiom_id) REFERENCES idioms(id),
    FOREIGN KEY (word_id) REFERENCES words(id)
);


-- 

CREATE TABLE IF NOT EXISTS word_phonetics(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    word_id INTEGER NOT NULL,
    ipa TEXT NOT NULL,
    syllables INTEGER NOT NULL,
    tag TEXT,
    notes TEXT,
    CONSTRAINT ipa_unique UNIQUE (ipa, word_id)
);
CREATE INDEX IF NOT EXISTS idx_words_ipa ON word_phonetics(ipa, word_id);
