-- Drop merged_tiles table (no longer needed)
DROP TABLE IF EXISTS merged_tiles;
DROP INDEX IF EXISTS idx_merged_tiles_zoom_coords;

-- Drop old minimap_tiles table
DROP TABLE IF EXISTS minimap_tiles;

-- Create new minimap_tiles table with simplified structure
CREATE TABLE minimap_tiles (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    -- Tile coordinates (at zoom level 2, original tile coords)
    x INTEGER NOT NULL,
    y INTEGER NOT NULL,
    -- Zoom level (0 = 4x4 merged, 1 = 2x2 merged, 2 = original)
    zoom INTEGER NOT NULL,
    -- Image dimensions (always 512x512 for merged tiles)
    width INTEGER NOT NULL,
    height INTEGER NOT NULL,
    -- Original file size (only for zoom=2)
    original_file_size INTEGER,
    -- WebP image data (lossless)
    image BLOB NOT NULL,
    image_size INTEGER NOT NULL,
    -- Metadata
    processed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    source_path TEXT NOT NULL,

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

-- Index for fast lookups
CREATE INDEX idx_minimap_tiles_zoom_coords ON minimap_tiles(zoom, x, y);
CREATE INDEX idx_minimap_tiles_coords ON minimap_tiles(x, y);
