-- 081_create_playback_analytics.sql
-- Playback analytics for track listening events (BE-API-019)

CREATE TABLE IF NOT EXISTS public.playback_analytics (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    track_id UUID NOT NULL REFERENCES public.tracks(id) ON DELETE CASCADE,
    user_id UUID NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,

    play_time INT NOT NULL DEFAULT 0,
    pause_count INT NOT NULL DEFAULT 0,
    seek_count INT NOT NULL DEFAULT 0,
    completion_rate DECIMAL(5,2) NOT NULL DEFAULT 0,

    started_at TIMESTAMPTZ NOT NULL,
    ended_at TIMESTAMPTZ,

    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_playback_analytics_track_id ON public.playback_analytics(track_id);
CREATE INDEX IF NOT EXISTS idx_playback_analytics_user_id ON public.playback_analytics(user_id);
CREATE INDEX IF NOT EXISTS idx_playback_analytics_created_at ON public.playback_analytics(created_at);

COMMENT ON TABLE public.playback_analytics IS 'Playback analytics for track listening events';
