1import{r as o,s as f,j as e,n as x,b as G,v as D}from"./index-Tma0Mwcy.js";import{B as j}from"./Button-BMwlDwH4.js";import{I as S}from"./Input-CF-LsJ-Y.js";import{C as v}from"./Card-D83HJyz5.js";import{E as P}from"./external-link-djkVKw6X.js";import{C as i}from"./check-circle-2-DN9WOha1.js";import{S as _}from"./save-JGa7jTC3.js";function q(){const[c,N]=o.useState(""),[d,y]=o.useState(""),[g,k]=o.useState(!1),[u,C]=o.useState(!1),[h,w]=o.useState(!1),[b,t]=o.useState(null),[s,E]=o.useState(null);o.useEffect(()=>{T()},[]);const T=async()=>{try{const{data:a,error:r}=await f.from("system_settings").select("key, value").in("key",["google_calendar_api_key","google_calendar_id"]);if(r)throw r;if(a){const n=a.find(l=>l.key==="google_calendar_api_key"),p=a.find(l=>l.key==="google_calendar_id");n&&N(n.value||""),p&&y(p.value||"")}}catch(a){console.error("Error al cargar configuración:",a)}},B=async()=>{if(!c.trim()||!d.trim()){t({type:"error",text:"Por favor completa todos los campos"});return}k(!0),t(null);try{const a=[{key:"google_calendar_api_key",value:c.trim()},{key:"google_calendar_id",value:d.trim()}];for(const r of a){const{error:n}=await f.from("system_settings").upsert(r,{onConflict:"key"});if(n)throw n}t({type:"success",text:"Configuración guardada correctamente"})}catch(a){console.error("Error al guardar configuración:",a),t({type:"error",text:a.message||"Error al guardar la configuración"})}finally{k(!1)}},A=async()=>{var a;if(!c.trim()||!d.trim()){t({type:"error",text:"Por favor guarda la configuración primero"});return}C(!0),t(null);try{const r=await fetch(`https://www.googleapis.com/calendar/v3/calendars/${encodeURIComponent(d)}/events?maxResults=1&key=${c}`);if(r.ok)t({type:"success",text:"Conexión exitosa con Google Calendar"});else{const n=await r.json();throw new Error(((a=n.error)==null?void 0:a.message)||"Error al conectar con Google Calendar")}}catch(r){console.error("Error al probar conexión:",r),t({type:"error",text:r.message||"Error al probar la conexión"})}finally{C(!1)}},I=async()=>{var a;w(!0),t(null),E(null);try{const n=(a=(await f.auth.getSession()).data.session)==null?void 0:a.access_token;if(!n){t({type:"error",text:"No autorizado"});return}const l=await fetch("https://zsmlmujjwbwvhplnuzke.supabase.co/functions/v1/test-google-calendar-config",{method:"GET",headers:{Authorization:`Bearer ${n}`}});if(l.ok){const m=await l.json();E(m),m.status==="ok"?t({type:"success",text:"Diagnóstico completado: Todo funciona correctamente"}):m.status==="incomplete_configuration"?t({type:"error",text:"Configuración incompleta. Revisa los detalles abajo."}):t({type:"error",text:"Se encontraron problemas. Revisa los detalles abajo."})}else{const m=await l.json();throw new Error(m.error||"Error al ejecutar diagnóstico")}}catch(r){console.error("Error en diagnóstico:",r),t({type:"error",text:r.message||"Error al ejecutar el diagnóstico"})}finally{w(!1)}};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold text-[rgb(var(--text))]",children:"Configuración de Google Calendar"}),e.jsx("p",{className:"text-sm text-[rgb(var(--text-muted))] mt-1",children:"Sincroniza las citas con tu calendario de Google"})]}),e.jsxs("a",{href:"https://console.cloud.google.com",target:"_blank",rel:"noopener noreferrer",className:"inline-flex items-center gap-2 text-sm text-[rgb(var(--primary))] hover:underline",children:[e.jsx(P,{className:"w-4 h-4"}),"Google Cloud Console"]})]}),e.jsx(v,{children:e.jsxs("div",{className:"p-6 space-y-6",children:[e.jsx("div",{className:"bg-[#2563EB]/10 dark:bg-blue-900/20 border border-[#2563EB]/20 dark:border-blue-800 rounded-lg p-4",children:e.jsxs("div",{className:"flex gap-3",children:[e.jsx(i,{className:"w-5 h-5 text-[#2563EB] dark:text-blue-400 flex-shrink-0 mt-0.5"}),e.jsxs("div",{className:"text-sm text-[#374151] dark:text-blue-200 space-y-2",children:[e.jsx("p",{className:"font-medium",children:"Estado actual del calendario:"}),e.jsxs("div",{className:"ml-2",children:[e.jsx("p",{className:"font-semibold",children:"â El calendario funciona correctamente sin Google Calendar OAuth"}),e.jsx("p",{className:"mt-1",children:"Las citas se gestionan directamente desde la base de datos."}),e.jsx("p",{className:"mt-2 text-xs",children:"La integración con Google Calendar es OPCIONAL y solo se usa para sincronización avanzada."})]})]})]})}),e.jsx("div",{className:"bg-yellow-50 dark:bg-yellow-900/20 border border-yellow-200 dark:border-yellow-800 rounded-lg p-4",children:e.jsxs("div",{className:"flex gap-3",children:[e.jsx(x,{className:"w-5 h-5 text-yellow-600 dark:text-yellow-400 flex-shrink-0 mt-0.5"}),e.jsxs("div",{className:"text-sm text-yellow-800 dark:text-yellow-200 space-y-2",children:[e.jsx("p",{className:"font-medium",children:"Configuración de Google Calendar (Opcional):"}),e.jsxs("p",{className:"text-xs",children:["Esta sección es OBSOLETA. Usa la página de ",e.jsx("strong",{children:"Google Calendar OAuth"})," en su lugar."]}),e.jsx("p",{className:"text-xs mt-2",children:"La configuración con API Key ya no es suficiente. Necesitas usar OAuth 2.0 para sincronización bidireccional."})]})]})}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-[rgb(var(--text))] mb-2",children:"Google Calendar API Key"}),e.jsx(S,{type:"password",value:c,onChange:a=>N(a.target.value),placeholder:"AIzaSyC...",className:"font-mono text-sm"}),e.jsx("p",{className:"text-xs text-[rgb(var(--text-muted))] mt-1",children:"La API Key que creaste en Google Cloud Console"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-[rgb(var(--text))] mb-2",children:"Calendar ID"}),e.jsx(S,{type:"text",value:d,onChange:a=>y(a.target.value),placeholder:"[email protected]",className:"font-mono text-sm"}),e.jsx("p",{className:"text-xs text-[rgb(var(--text-muted))] mt-1",children:"El ID del calendario de Google (normalmente tu email o un ID de grupo)"})]})]}),b&&e.jsxs("div",{className:`flex items-center gap-2 p-4 rounded-lg ${b.type==="success"?"bg-[#2563EB]/10 dark:bg-blue-900/20 border border-[#2563EB]/20 dark:border-blue-800 text-white dark:text-blue-200":"bg-red-500/10 dark:bg-red-900/20 border border-red-500/20 dark:border-red-800 text-red-400 dark:text-red-200"}`,children:[b.type==="success"?e.jsx(i,{className:"w-5 h-5 flex-shrink-0"}):e.jsx(x,{className:"w-5 h-5 flex-shrink-0"}),e.jsx("p",{className:"text-sm",children:b.text})]}),e.jsxs("div",{className:"flex flex-wrap gap-3 pt-4",children:[e.jsxs(j,{onClick:B,disabled:g||u||h,className:"flex items-center gap-2",children:[e.jsx(_,{className:"w-4 h-4"}),g?"Guardando...":"Guardar configuración"]}),e.jsxs(j,{onClick:A,disabled:g||u||h||!c||!d,variant:"outline",className:"flex items-center gap-2",children:[e.jsx(G,{className:"w-4 h-4"}),u?"Probando...":"Probar conexión"]}),e.jsxs(j,{onClick:I,disabled:g||u||h,variant:"outline",className:"flex items-center gap-2",children:[e.jsx(D,{className:"w-4 h-4"}),h?"Diagnosticando...":"Diagnóstico OAuth"]})]})]})}),s&&e.jsx(v,{children:e.jsxs("div",{className:"p-6 space-y-4",children:[e.jsx("h3",{className:"text-lg font-semibold text-[rgb(var(--text))]",children:"Resultados del diagnóstico OAuth"}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"bg-[#f1f3f5] dark:bg-gray-900/50 border border-[#e5e7eb] dark:border-gray-700 rounded-lg p-4",children:[e.jsx("h4",{className:"text-sm font-semibold text-[rgb(var(--text))] mb-2",children:"Configuración"}),e.jsxs("div",{className:"space-y-1 text-sm",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[s.configuration.hasCl
1ientId?e.jsx(i,{className:"w-4 h-4 text-[#2563EB]"}):e.jsx(x,{className:"w-4 h-4 text-red-400"}),e.jsxs("span",{className:"text-[rgb(var(--text-muted))]",children:["Client ID: ",s.configuration.hasClientId?"Configurado":"Falta"]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[s.configuration.hasClientSecret?e.jsx(i,{className:"w-4 h-4 text-[#2563EB]"}):e.jsx(x,{className:"w-4 h-4 text-red-400"}),e.jsxs("span",{className:"text-[rgb(var(--text-muted))]",children:["Client Secret: ",s.configuration.hasClientSecret?"Configurado":"Falta"]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[s.configuration.hasRefreshToken?e.jsx(i,{className:"w-4 h-4 text-[#2563EB]"}):e.jsx(x,{className:"w-4 h-4 text-red-400"}),e.jsxs("span",{className:"text-[rgb(var(--text-muted))]",children:["Refresh Token: ",s.configuration.hasRefreshToken?"Configurado":"Falta"]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[s.configuration.hasCalendarId?e.jsx(i,{className:"w-4 h-4 text-[#2563EB]"}):e.jsx(x,{className:"w-4 h-4 text-red-400"}),e.jsxs("span",{className:"text-[rgb(var(--text-muted))]",children:["Calendar ID: ",s.configuration.hasCalendarId?"Configurado":"Falta"]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(i,{className:"w-4 h-4 text-[#9ca3af]"}),e.jsxs("span",{className:"text-[rgb(var(--text-muted))]",children:["Timezone: ",s.configuration.timezone]})]})]})]}),s.tokenTest&&e.jsxs("div",{className:`border rounded-lg p-4 ${s.tokenTest.success?"bg-[#2563EB]/10 dark:bg-blue-900/20 border-[#2563EB]/20 dark:border-blue-800":"bg-red-500/10 dark:bg-red-900/20 border-red-500/20 dark:border-red-800"}`,children:[e.jsx("h4",{className:"text-sm font-semibold text-[rgb(var(--text))] mb-2",children:"Test de Access Token"}),s.tokenTest.success?e.jsxs("div",{className:"space-y-1 text-sm text-[#374151] dark:text-blue-200",children:[e.jsx("p",{children:"â Access token obtenido correctamente"}),e.jsxs("p",{className:"text-xs",children:["Expires in: ",s.tokenTest.expires
1In,"s"]}),e.jsxs("p",{className:"text-xs",children:["Scopes: ",s.tokenTest.scope]})]}):e.jsxs("div",{className:"space-y-1 text-sm text-red-400 dark:text-red-200",children:[e.jsx("p",{children:"â Error al obtener access token"}),e.jsx("p",{className:"text-xs font-mono mt-2 whitespace-pre-wrap",children:s.tokenTest.error}),e.jsxs("div",{className:"mt-3 p-3 bg-[#f8f9fb] dark:bg-gray-900 rounded border border-red-300 dark:border-red-700",children:[e.jsx("p",{className:"font-semibold mb-1",children:"Solución:"}),e.jsx("p",{className:"text-xs",children:"Verifica que el Refresh Token sea válido. Es posible que haya expirado o que los permisos de OAuth hayan sido revocados. Ve a la página de configuración de Google Calendar OAuth para regenerar el token."})]})]})]}),s.freeBusyTest&&e.jsxs("div",{className:`border rounded-lg p-4 ${s.freeBusyTest.success?"bg-[#2563EB]/10 dark:bg-blue-900/20 border-[#2563EB]/20 dark:border-blue-800":"bg-red-500/10 dark:bg-red-900/20 border-red-500/20 dark:border-red-800"}`,children:[e.jsx("h4",{className:"text-sm font-semibold text-[rgb(var(--text))] mb-2",children:"Test FreeBusy (próximos 2 dÃas)"}),s.freeBusyTest.success?e.jsxs("div",{className:"space-y-2 text-sm text-[#374151] dark:text-blue-200",children:[e.jsx("p",{children:"â FreeBusy API funciona correctamente"}),e.jsxs("p",{className:"text-xs",children:["Periodo: ",new Date(s.freeBusyTest.timeMin).toLocaleDateString("es-ES")," - ",new Date(s.freeBusyTest.timeMax).toLocaleDateString("es-ES")]}),e.jsxs("p",{className:"text-xs font-semibold",children:["Periodos ocupados encontrados: ",s.freeBusyTest.busyCount]}),s.freeBusyTest.busyPeriods.length>0&&e.jsxs("div",{className:"mt-2 p-2 bg-[#f8f9fb] dark:bg-gray-900 rounded border border-blue-300 dark:border-blue-700",children:[e.jsx("p",{className:"text-xs font-semibold mb-1",children:"Periodos ocupados:"}),e.jsx("div",{className:"space-y-1",children:s.freeBusyTest.busyPeriods.map((a,r)=>e.jsxs("div",{className:"text-xs font-mono",children:[new Date(a.start).toLocaleString("es-ES")," - ",new Date(a.end).toLocaleString("es-ES")]},r))})]})]}):e.jsxs("div",{className:"space-y-1 text-sm text-red-400 dark:text-red-200",children:[e.jsx("p",{children:"â Error en FreeBusy API"}),e.jsx("p",{className:"text-xs font-mono mt-2 whitespace-pre-wrap",children:s.freeBusyTest.error})]})]}),s.eventsTest&&e.jsxs("div",{className:`border rounded-lg p-4 ${s.eventsTest.success?"bg-[#2563EB]/10 dark:bg-blue-900/20 border-[#2563EB]/20 dark:border-blue-800":"bg-red-500/10 dark:bg-red-900/20 border-red-500/20 dark:border-red-800"}`,children:[e.jsx("h4",{className:"text-sm font-semibold text-[rgb(var(--text))] mb-2",children:"Test de Eventos (próximos 2 dÃas)"}),s.eventsTest.success?e.jsxs("div",{className:"space-y-2 text-sm text-[#374151] dark:text-blue-200",children:[e.jsx("p",{children:"â Events API funciona correctamente"}),e.jsxs("p",{className:"text-xs font-semibold",children:["Eventos encontrados: ",s.eventsTest.eventCount]}),s.eventsTest.events.length>0&&e.jsxs("div",{className:"mt-2 p-2 bg-[#f8f9fb] dark:bg-gray-900 rounded border border-blue-300 dark:border-blue-700",children:[e.jsx("p",{className:"text-xs font-semibold mb-1",children:"Lista de eventos:"}),e.jsx("div",{className:"space-y-2",children:s.eventsTest.events.map((a,r)=>e.jsxs("div",{className:"text-xs p-2 bg-[#f1f3f5] dark:bg-gray-800 rounded",children:[e.jsx("p",{className:"font-semibold",children:a.summary||"Sin tÃtulo"}),e.jsxs("p",{className:"text-[rgb(var(--text-muted))]",children:[new Date(a.start).toLocaleString("es-ES")," - ",new Date(a.end).toLocaleString("es-ES")]}),e.jsxs("p",{className:"text-[rgb(var(--text-muted))]",children:["Estado: ",a.status," | Transparencia: ",a.transparency||"opaque (ocupado)"]})]},r))})]})]}):e.jsxs("div",{className:"space-y-1 text-sm text-red-400 dark:text-red-200",children:[e.jsx("p",{children:"â Error en Events API"}),e.jsx("p",{className:"text-xs font-mono mt-2 whitespace-pre-wrap",children:s.eventsTest.error})]})]}),s.diagnosis&&e.jsxs("div",{className:`border rounded-lg p-4 ${s.diagnosis.recommendation.includes("PROBLEMA")?"bg-orange-50 dark:bg-orange-900/20 border-orange-200 dark:border-orange-800":s.diagnosis.recommendation.includes("correctamente")?"bg-[#2563EB]/10 dark:bg-blue-900/20 border-[#2563EB]/20 dark:border-blue-800":"bg-[#f1f3f5] dark:bg-gray-900/20 border-[#e5e7eb] dark:border-gray-800"}`,children:[e.jsx("h4",{className:"text-sm font-semibold text-[rgb(var(--text))] mb-2",children:"Diagnóstico Final"}),e.jsxs("div",{className:"space-y-2 text-sm text-[rgb(var(--text))]",children:[e.jsx("p",{className:"font-medium",children:s.diagnosis.message}),e.jsx("p",{className:s.diagnosis.recommendation.includes("PROBLEMA")?"text-orange-800 dark:text-orange-200":"",children:s.diagnosis.recommendation}),s.diagnosis.recommendation.includes("PROBLEMA")&&e.jsxs("div",{className:"mt-3 p-3 bg-[#f8f9fb] dar
1k:bg-gray-900 rounded border border-orange-300 dark:border-orange-700",children:[e.jsx("p",{className:"font-semibold mb-2",children:"Solución:"}),e.jsxs("ol",{className:"text-xs list-decimal list-inside space-y-1",children:[e.jsx("li",{children:"Abre Google Calendar en tu navegador"}),e.jsx("li",{children:"Haz clic en cada evento que debe bloquear disponibilidad"}),e.jsx("li",{children:'En los detalles del evento, busca "Mostrar como" o "Show as"'}),e.jsx("li",{children:'Cámbialo de "Disponible" a "Ocupado"'}),e.jsx("li",{children:"Guarda los cambios"}),e.jsx("li",{children:"Vuelve a ejecutar este diagnóstico"})]})]})]})]})]})]})}),e.jsx(v,{children:e.jsxs("div",{className:"p-6 space-y-4",children:[e.jsx("h3",{className:"text-lg font-semibold text-[rgb(var(--text))]",children:"Cómo funciona la sincronización"}),e.jsxs("div",{className:"space-y-3 text-sm text-[rgb(var(--text-muted))]",children:[e.jsx("p",{children:"Una vez configurado, las citas se sincronizarán automáticamente con tu calendario de Google:"}),e.jsxs("ul",{className:"list-disc list-inside space-y-2 ml-4",children:[e.jsxs("li",{children:[e.jsx("strong",{children:"Nueva cita:"})," Se crea un evento en Google Calendar"]}),e.jsxs("li",{children:[e.jsx("strong",{children:"Modificar cita:"})," Se actualiza el evento en Google Calendar"]}),e.jsxs("li",{children:[e.jsx("strong",{children:"Cancelar cita:"})," Se elimina el evento de Google Calendar"]})]}),e.jsx("p",{className:"pt-2",children:"Los clientes recibirán invitaciones por email automáticamente si tienen configurado su correo."})]})]})})]})}export{q as AdminCalendarConfigPage};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.