"use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[3226],{283:(e,t,r)=>{r.d(t,{A:()=>l,AuthProvider:()=>o});var a=r(5155),i=r(2115),s=r(2099);let n=(0,i.createContext)(void 0);function o(e){let{children:t}=e,[r,o]=(0,i.useState)(null),[l,c]=(0,i.useState)(null),[u,d]=(0,i.useState)(!0);(0,i.useEffect)(()=>{s.N.auth.getSession().then(e=>{var t;let{data:{session:r}}=e;c(r),o(null!=(t=null==r?void 0:r.user)?t:null),d(!1)});let{data:{subscription:e}}=s.N.auth.onAuthStateChange(async(e,t)=>{var r;c(t),o(null!=(r=null==t?void 0:t.user)?r:null),d(!1),"SIGNED_IN"===e&&(null==t?void 0:t.user)&&!t.user.email_confirmed_at&&await p(t.user)});return()=>e.unsubscribe()},[]);let p=async e=>{try{var t,r;let{error:a}=await s.N.from("profiles").insert({id:e.id,email:e.email,full_name:(null==(t=e.user_metadata)?void 0:t.full_name)||null,avatar_url:(null==(r=e.user_metadata)?void 0:r.avatar_url)||null})}catch(e){}},y=async(e,t)=>{let{error:r}=await s.N.auth.signInWithPassword({email:e,password:t});return{error:r}},_=async(e,t,r)=>{let{error:a}=await s.N.auth.signUp({email:e,password:t,options:{data:{full_name:r}}});return{error:a}},f=async()=>{let{error:e}=await s.N.auth.signOut();return{error:e}},m=async e=>{let{error:t}=await s.N.auth.resetPasswordForEmail(e,{redirectTo:"".concat(window.location.origin,"/auth/reset-password")});return{error:t}};return(0,a.jsx)(n.Provider,{value:{user:r,session:l,loading:u,signIn:y,signUp:_,signOut:f,resetPassword:m},children:t})}function l(){let e=(0,i.useContext)(n);if(void 0===e)throw Error("useAuth must be used within an AuthProvider");return e}},2099:(e,t,r)=>{r.d(t,{N:()=>n});var a=r(321);let i="https://eulbkkpnpzbydikovmcv.supabase.co",s="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImV1bGJra3BucHpieWRpa292bWN2Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDkwNjc5NjgsImV4cCI6MjA2NDY0Mzk2OH0.zIKb8vKTTqmnH4jkLFGTNWEW1Pucrc0QMScG5IR6ETQ";if(!i||!s)throw Error("Missing Supabase environment variables");let n=(0,a.UU)(i,s,{auth:{autoRefreshToken:!0,persistSession:!0,detectSessionInUrl:!1,storageKey:"sb-auth-token",storage:window.localStorage,flowType:"pkce"},db:{schema:"public"},global:{headers:{"X-Client-Info":"movie-streaming-app"}},realtime:{params:{eventsPerSecond:2}}})},3226:(e,t,r)=>{r.d(t,{x:()=>n});var a=r(2115),i=r(2099),s=r(283);let n=e=>{let{user:t}=(0,s.A)(),[r,n]=(0,a.useState)(null),[o,l]=(0,a.useState)([]),[c,u]=(0,a.useState)([]),[d,p]=(0,a.useState)(!1),[y,_]=(0,a.useState)(null),f=(0,a.useRef)(!1),m=(0,a.useRef)(!0),h=()=>Math.random().toString(36).substring(2,8).toUpperCase(),w=(0,a.useCallback)(async e=>{if(!t)return m.current&&_("You must be logged in to create a party"),null;m.current&&(p(!0),_(null));try{let r=h(),a={host_id:t.id,invite_code:r,...e},{data:s,error:n}=await i.N.from("watch_parties").insert(a).select().single();if(!m.current)return null;if(n)throw n;let{error:o}=await i.N.from("party_participants").insert({party_id:s.id,user_id:t.id,is_host:!0});if(!m.current)return null;if(o)throw o;return s.id}catch(e){return m.current&&_("Failed to create party"),null}finally{m.current&&p(!1)}},[t]),g=(0,a.useCallback)(async e=>{if(!t)throw m.current&&_("You must be logged in to join a party"),Error("User not logged in");if(!f.current){f.current=!0,m.current&&(p(!0),_(null));try{let{data:r,error:a}=await i.N.from("watch_parties").select("id, is_active, video_url, selected_source_index").eq("id",e).maybeSingle();if(!m.current)return;if(a)throw Error("Failed to check party: ".concat(a.message));if(!r)throw Error("Party not found");if(!r.is_active)throw Error("Party is no longer active");let{data:s,error:n}=await i.N.from("party_participants").select("id, last_active").eq("party_id",e).eq("user_id",t.id).maybeSingle();if(!m.current)return;if(s){let{error:r}=await i.N.from("party_participants").update({last_active:new Date().toISOString()}).eq("id",s.id);if(!m.current)return;if(r);else{let r=new Date(s.last_active).getTime();if(Date.now()-r>3e5)try{m.current&&await i.N.from("party_messages").insert({party_id:e,user_id:t.id,message:"rejoined the party",message_type:"system"})}catch(e){}}return}let{error:o}=await i.N.from("party_participants").insert({party_id:e,user_id:t.id,is_host:!1});if(!m.current)return;if(o)throw o;if(m.current)try{await i.N.from("party_messages").insert({party_id:e,user_id:t.id,message:"joined the party",message_type:"system"})}catch(e){}return{videoUrl:r.video_url||void 0,selectedSourceIndex:r.selected_source_index||void 0}}catch(e){throw m.current&&_(e instanceof Error?e.message:"Failed to join party"),e}finally{m.current&&p(!1)}}},[t]),v=(0,a.useCallback)(async r=>{if(!t)throw Error("You must be logged in to leave a party");let a=r||e;if(!a)throw Error("No party ID provided");try{let{error:e}=await i.N.from("party_participants").update({last_active:new Date(0).toISOString()}).eq("party_id",a).eq("user_id",t.id);if(!m.current)return;if(e)throw Error(e.message||"Failed to leave party");m.current&&await i.N.from("party_messages").insert({party_id:a,user_id:t.id,message:"left the party",message_type:"system"}),f.current=!1}catch(e){throw m.current&&_("Failed to leave party"),e}},[t,e]),b=(0,a.useCallback)(async(e,r)=>{if(t)try{let{error:a}=await i.N.from("watch_parties").update({is_active:"active"===r,updated_at:new Date().toISOString()}).eq("id",e).eq("host_id",t.id);if(!m.current)return;if(a)throw a;m.current&&await i.N.from("party_messages").insert({party_id:e,user_id:t.id,message:{waiting:"paused the party",active:"started the party",ended:"ended the party"}[r],message_type:"system"})}catch(e){throw m.current&&_("Failed to update party status"),e instanceof Error?e:Error(String(e))}},[t]),N=(0,a.useCallback)(async(e,r,a)=>{if(t)try{let{error:s}=await i.N.from("watch_parties").update({video_url:r,selected_source_index:a,updated_at:new Date().toISOString()}).eq("id",e).eq("host_id",t.id);if(!m.current)return;if(s)throw s}catch(e){m.current&&_("Failed to update party video source")}},[t]),S=(0,a.useCallback)(async e=>{if(!t)throw Error("You must be logged in to delete a party");try{let{error:r}=await i.N.from("party_participants").delete().eq("party_id",e);if(r)throw Error(r.message||"Failed to delete party participants");let{error:a}=await i.N.from("party_messages").delete().eq("party_id",e);if(a)throw Error(a.message||"Failed to delete party messages");let{error:s}=await i.N.from("watch_parties").delete().eq("id",e).eq("host_id",t.id);if(!m.current)return;if(s)throw Error(s.message||"Failed to delete party")}catch(e){throw m.current&&_("Failed to delete party"),e instanceof Error?e:Error(String(e))}},[t]),E=(0,a.useCallback)(async e=>{if(!t)return m.current&&_("You must be logged in to join a party"),null;m.current&&(p(!0),_(null));try{let{data:r,error:a}=await i.N.from("watch_parties").select("id, is_active, video_url, selected_source_index").eq("invite_code",e.toUpperCase()).maybeSingle();if(!m.current)return null;if(a)throw Error("Database error: ".concat(a.message));if(!r)throw Error("Invalid invite code");if(!r.is_active)return m.current&&_("This party has ended"),null;let{data:s}=await i.N.from("party_participants").select("id, last_active").eq("party_id",r.id).eq("user_id",t.id).maybeSingle();if(!m.current)return null;if(s){if(await i.N.from("party_participants").update({last_active:new Date().toISOString()}).eq("id",s.id),!m.current)return null;m.current&&await i.N.from("party_messages").insert({party_id:r.id,user_id:t.id,message:"rejoined the party",message_type:"system"})}else{let{error:e}=await i.N.from("party_participants").insert({party_id:r.id,user_id:t.id,is_host:!1});if(!m.current)return null;if(e)throw e;m.current&&await i.N.from("party_messages").insert({party_id:r.id,user_id:t.id,message:"joined the party",message_type:"system"})}return{partyId:r.id,videoUrl:r.video_url||void 0,selectedSourceIndex:r.selected_source_index||void 0}}catch(e){return m.current&&_(e instanceof Error?e.message:"Failed to join party"),null}finally{m.current&&p(!1)}},[t]),I=(0,a.useCallback)(async()=>{if(e)try{let{data:t,error:r}=await i.N.from("party_messages").select("*").eq("party_id",e).order("created_at",{ascending:!0}).limit(100);!r&&t&&u(t)}catch(e){}},[e]),q=(0,a.useCallback)(async function(r){let a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"chat";if(!t||!e)throw Error("You must be logged in and in a party to send messages");let{data:s}=await i.N.auth.getSession(),n={party_id:e,user_id:t.id,message:r,message_type:a};try{let{data:e,error:t}=await i.N.from("party_messages").insert(n).select().single();if(!m.current)return;if(t){if("42501"===t.code)throw Error("Authentication error: Please try logging out and back in");throw t}m.current&&u(t=>t.some(t=>t.id===e.id)?t:[...t,e])}catch(e){throw m.current&&_("Failed to send message: "+((null==e?void 0:e.message)||"Unknown error")),e}},[t,e]),k=(0,a.useCallback)(async()=>{if(!t)return[];try{let{data:e,error:r}=await i.N.from("watch_parties").select("*").eq("host_id",t.id).order("created_at",{ascending:!1});if(r)throw r;let{data:a,error:s}=await i.N.from("party_participants").select("\n party_id,\n watch_parties!inner(*)\n ").eq("user_id",t.id).gte("last_active",new Date(Date.now()-864e5).toISOString());if(s)throw s;let n=(null==a?void 0:a.map(e=>e.watch_parties).filter(e=>null!=e&&"object"==typeof e&&e.host_id!==t.id))||[];return[...e||[],...n].filter((e,t,r)=>t===r.findIndex(t=>t.id===e.id))}catch(e){throw e}},[t]),C=(0,a.useCallback)(async(e,r)=>{if(!t)return null;try{let{data:a,error:s}=await i.N.from("watch_parties").select("*").eq("host_id",t.id).eq("media_id",e).eq("media_type",r).in("status",["waiting","active"]).order("created_at",{ascending:!1}).limit(1);if(s)return null;if(a&&a.length>0)return a[0];return null}catch(e){return null}},[t]);return(0,a.useEffect)(()=>{f.current=!1},[null==t?void 0:t.id,e]),(0,a.useEffect)(()=>{if(!e){n(null),l([]),u([]);return}let t=!0;m.current=!0,(async()=>{if(m.current){p(!0),_(null);try{let{data:r,error:a}=await i.N.from("watch_parties").select("*").eq("id",e).maybeSingle();if(!t||!m.current)return;if(a)return void _("Party not found: ".concat(a.message));if(!r)return void _("Party not found");r&&!r.status&&(r.status=r.is_active?"active":"waiting"),n(r);let{data:s,error:o}=await i.N.from("party_participants").select("*").eq("party_id",e).order("joined_at",{ascending:!0});t&&m.current&&(o?l([]):l(s||[]));let{data:c,error:d}=await i.N.from("party_messages").select("*").eq("party_id",e).order("created_at",{ascending:!0}).limit(100);t&&m.current&&(d?u([]):u(c||[]))}catch(e){t&&m.current&&_(e instanceof Error?e.message:"Failed to load party data")}finally{t&&m.current&&p(!1)}}})();let r=i.N.channel("party-participants-".concat(e)).on("postgres_changes",{event:"*",schema:"public",table:"party_participants",filter:"party_id=eq.".concat(e)},e=>{if(t&&m.current){if("INSERT"===e.eventType){let t=e.new;l(e=>e.some(e=>e.id===t.id)?e:[...e,t])}else if("UPDATE"===e.eventType){let t=e.new;l(e=>e.map(e=>e.id===t.id?t:e))}else if("DELETE"===e.eventType){let t=e.old;l(e=>e.filter(e=>e.id!==t.id))}}}).subscribe((e,t)=>{}),a=i.N.channel("party-messages-".concat(e),{config:{broadcast:{self:!0}}}).on("postgres_changes",{event:"INSERT",schema:"public",table:"party_messages",filter:"party_id=eq.".concat(e)},async e=>{if(!t||!m.current)return;let r=e.new;u(e=>e.some(e=>e.id===r.id)?e:[...e,r])}).subscribe((e,t)=>{}),s=i.N.channel("party-".concat(e)).on("postgres_changes",{event:"UPDATE",schema:"public",table:"watch_parties",filter:"id=eq.".concat(e)},e=>{t&&m.current&&n(e.new)}).subscribe();return()=>{t=!1,m.current=!1,r.unsubscribe(),a.unsubscribe(),s.unsubscribe()}},[e]),{currentParty:r,participants:o,messages:c,loading:d,error:y,createParty:w,joinParty:g,joinPartyByCode:E,sendMessage:q,leaveParty:v,updatePartyStatus:b,updatePartyVideoSource:N,deleteParty:S,getUserParties:k,checkExistingPartyForMedia:C,refreshMessages:I,resetJoinAttempt:()=>{f.current=!1},isHost:!!r&&!!t&&r.host_id===t.id,isParticipant:o.some(e=>e.user_id===(null==t?void 0:t.id)&&new Date(e.last_active).getTime()>Date.now()-3e5)}}}}]);