CREATE TABLE IF NOT EXISTS categories (
    id          SERIAL PRIMARY KEY,
    name        VARCHAR(100) NOT NULL,
    sort_order  INT NOT NULL DEFAULT 0,
    created_at  TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at  TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    UNIQUE (name)
);

CREATE TABLE IF NOT EXISTS words (
    id               SERIAL PRIMARY KEY,
    category_id      INT NOT NULL REFERENCES categories(id) ON DELETE CASCADE,
    word             VARCHAR(255) NOT NULL,
    translation      VARCHAR(255) NOT NULL,
    phonetic         VARCHAR(255),
    example_sentence TEXT,
    audio_url        VARCHAR(500),
    image_url        VARCHAR(500),
    sort_order       INT NOT NULL DEFAULT 0,
    created_at       TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at       TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_words_category_id ON words(category_id);
CREATE INDEX IF NOT EXISTS idx_words_word ON words(word);
