-- T0321: Create Track Versioning Database Model
-- Create table track_versions for track versioning

-- Table track_versions
CREATE TABLE IF NOT EXISTS track_versions (
    id BIGSERIAL PRIMARY KEY,
    track_id BIGINT NOT NULL REFERENCES tracks(id) ON DELETE CASCADE,
    version_number INTEGER NOT NULL,
    file_path VARCHAR(500) NOT NULL,
    file_size BIGINT NOT NULL,
    changelog TEXT,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP,
    
    -- Unique constraint: one version number per track
    UNIQUE(track_id, version_number)
);

-- Indexes for performance
CREATE INDEX IF NOT EXISTS idx_track_versions_track_id ON track_versions(track_id);
CREATE INDEX IF NOT EXISTS idx_track_versions_created_at ON track_versions(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_track_versions_track_version ON track_versions(track_id, version_number DESC);

-- Index for soft deletes
CREATE INDEX IF NOT EXISTS idx_track_versions_deleted_at ON track_versions(deleted_at);

