-- Shared spelling curriculum and per-user familiarity tracking. CREATE TABLE IF NOT EXISTS spelling_words ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), canonical_word TEXT NOT NULL UNIQUE, active BOOLEAN NOT NULL DEFAULT true ); CREATE TABLE IF NOT EXISTS spelling_items ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), spelling_word_id UUID NOT NULL REFERENCES spelling_words (id) ON DELETE CASCADE, displayed_word TEXT NOT NULL, is_correctly_spelled BOOLEAN NOT NULL, correct_answer NUMERIC NOT NULL, active BOOLEAN NOT NULL DEFAULT true, UNIQUE (spelling_word_id, displayed_word) ); CREATE INDEX IF NOT EXISTS spelling_items_active_idx ON spelling_items (spelling_word_id) WHERE active = true; CREATE TABLE IF NOT EXISTS user_spelling_progress ( firebase_uid TEXT NOT NULL REFERENCES users (firebase_uid) ON DELETE CASCADE, spelling_item_id UUID NOT NULL REFERENCES spelling_items (id) ON DELETE CASCADE, familiarity_score INTEGER NOT NULL DEFAULT 0, attempt_count INTEGER NOT NULL DEFAULT 0, correct_count INTEGER NOT NULL DEFAULT 0, last_attempted_at TIMESTAMPTZ, last_outcome TEXT, PRIMARY KEY (firebase_uid, spelling_item_id) ); CREATE INDEX IF NOT EXISTS user_spelling_progress_uid_idx ON user_spelling_progress (firebase_uid);