

-- 2. Criar tabela de grupos de repertório
CREATE TABLE IF NOT EXISTS repertoire_groups (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    name VARCHAR(255) NOT NULL,
    description TEXT,
    color VARCHAR(7) DEFAULT '#3B82F6', -- Cor em hexadecimal
    position INTEGER DEFAULT 0, -- Para ordenação drag-and-drop
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Criar índices
CREATE INDEX IF NOT EXISTS idx_repertoire_groups_user_id ON repertoire_groups(user_id);
CREATE INDEX IF NOT EXISTS idx_repertoire_groups_position ON repertoire_groups(user_id, position);

-- 3. Criar tabela de músicas
CREATE TABLE IF NOT EXISTS songs (
    id SERIAL PRIMARY KEY,
    repertoire_group_id INTEGER NOT NULL REFERENCES repertoire_groups(id) ON DELETE CASCADE,
    title VARCHAR(255) NOT NULL,
    artist VARCHAR(255) NOT NULL,
    youtube_url TEXT,
    youtube_video_id VARCHAR(20), -- ID extraído da URL do YouTube
    duration INTEGER, -- Duração em segundos
    position INTEGER DEFAULT 0, -- Para ordenação drag-and-drop dentro do grupo
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Criar índices
CREATE INDEX IF NOT EXISTS idx_songs_repertoire_group_id ON songs(repertoire_group_id);
CREATE INDEX IF NOT EXISTS idx_songs_position ON songs(repertoire_group_id, position);
CREATE INDEX IF NOT EXISTS idx_songs_youtube_video_id ON songs(youtube_video_id);