-- Migration: Create stream_segments table
-- Description: Table pour stocker les segments HLS générés
-- Date: 2025-01-27

CREATE TABLE IF NOT EXISTS stream_segments (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    track_id UUID NOT NULL,
    quality TEXT NOT NULL CHECK (quality IN ('low', 'medium', 'high', 'hi_res')),
    segment_index INTEGER NOT NULL,
    path TEXT NOT NULL,
    duration FLOAT NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    
    -- Contraintes
    CONSTRAINT stream_segments_track_id_fkey FOREIGN KEY (track_id) REFERENCES tracks(id) ON DELETE CASCADE,
    CONSTRAINT stream_segments_unique UNIQUE (track_id, quality, segment_index)
);

-- Index pour performance (requêtes fréquentes)
CREATE INDEX IF NOT EXISTS idx_stream_segments_track_id_quality ON stream_segments(track_id, quality);
CREATE INDEX IF NOT EXISTS idx_stream_segments_track_id_quality_index ON stream_segments(track_id, quality, segment_index);
CREATE INDEX IF NOT EXISTS idx_stream_segments_created_at ON stream_segments(created_at);

