-- Database schema for buglab labyrinths storage

CREATE TABLE IF NOT EXISTS labyrinths (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    score INTEGER NOT NULL,
    field_blob BLOB NOT NULL,
    width INTEGER DEFAULT 29,
    height INTEGER DEFAULT 19,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS tags (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT UNIQUE NOT NULL
);

CREATE TABLE IF NOT EXISTS labyrinth_tags (
    labyrinth_id INTEGER REFERENCES labyrinths(id) ON DELETE CASCADE,
    tag_id INTEGER REFERENCES tags(id) ON DELETE CASCADE,
    PRIMARY KEY (labyrinth_id, tag_id)
);

CREATE INDEX IF NOT EXISTS idx_labyrinths_score ON labyrinths(score DESC);
