-- Phase 3a (2026-07-03): 음악 회원 저장 + 사용량 로그 확장 + 음악 숨김 해제
-- 의존성: 2026-07-02-phase2-saju-tarot.sql(ai_usage_log 생성) 적용 후 실행
-- 적용: 클라우드 Supabase + NAS self-host 양쪽

CREATE TABLE IF NOT EXISTS music_tracks (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id uuid NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
  title text,
  story text,
  lyrics text,
  style text,
  audio_url text,
  task_id text,
  created_at timestamptz NOT NULL DEFAULT now()
);
ALTER TABLE music_tracks ENABLE ROW LEVEL SECURITY;
CREATE POLICY music_select_own ON music_tracks FOR SELECT USING (auth.uid() = user_id);

-- ai_usage_log CHECK에 'music' 추가 (phase2의 인라인 CHECK auto-name 제거 후 재정의)
ALTER TABLE ai_usage_log DROP CONSTRAINT IF EXISTS ai_usage_log_service_check;
ALTER TABLE ai_usage_log ADD CONSTRAINT ai_usage_log_service_check CHECK (service IN ('saju','tarot','music'));

DELETE FROM service_settings WHERE id = 'music';
