-- T0509: Create Playlist Version History
-- Create table playlist_versions for tracking playlist versions

-- Table playlist_versions
CREATE TABLE IF NOT EXISTS playlist_versions (
    id BIGSERIAL PRIMARY KEY,
    playlist_id BIGINT NOT NULL REFERENCES playlists(id) ON DELETE CASCADE,
    user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE SET NULL,
    version INTEGER NOT NULL,
    action VARCHAR(50) NOT NULL,
    title VARCHAR(200),
    description TEXT,
    is_public BOOLEAN DEFAULT TRUE,
    cover_url VARCHAR(500),
    tracks_snapshot TEXT,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- Indexes for performance
CREATE INDEX IF NOT EXISTS idx_playlist_versions_playlist_id ON playlist_versions(playlist_id);
CREATE INDEX IF NOT EXISTS idx_playlist_versions_user_id ON playlist_versions(user_id);
CREATE INDEX IF NOT EXISTS idx_playlist_versions_action ON playlist_versions(action);
CREATE INDEX IF NOT EXISTS idx_playlist_versions_created_at ON playlist_versions(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_playlist_versions_playlist_created ON playlist_versions(playlist_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_playlist_versions_playlist_version ON playlist_versions(playlist_id, version);

