-- Create merged_tiles table for storing merged map tiles at different zoom levels
-- Zoom level 2: 1x1 tiles (512px original tiles)
-- Zoom level 1: 2x2 tiles merged into 512px
-- Zoom level 0: 4x4 tiles merged into 512px

CREATE TABLE merged_tiles (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    -- Tile coordinates at this zoom level
    x INTEGER NOT NULL,
    y INTEGER NOT NULL,
    -- Zoom level (0 = most zoomed out, 2 = most zoomed in)
    zoom_level INTEGER NOT NULL,
    -- Number of original tiles merged (1, 4, or 16)
    merge_factor INTEGER NOT NULL,
    -- Dimensions of the merged image
    width INTEGER NOT NULL,
    height INTEGER NOT NULL,
    -- WebP image data (lossless compression)
    webp_data BLOB NOT NULL,
    webp_size INTEGER NOT NULL,
    -- Metadata
    processed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    -- Track which original tiles were merged (for debugging)
    source_tiles TEXT NOT NULL,

    -- Unique constraint on zoom level + coordinates
    UNIQUE(zoom_level, x, y)
);

-- Index for fast lookups
CREATE INDEX idx_merged_tiles_zoom_coords ON merged_tiles(zoom_level, x, y);
