var{useState:useState,useEffect:useEffect,useRef:useRef}=window.React||{};window.SpecialistDashboard=({user:user,onLogout:onLogout,onHome:onHome,onUserUpdate:onUserUpdate})=>{const{Icons:Icons,firestore:firestore,firebaseStorageFns:firebaseStorageFns,firebaseStorage:firebaseStorage,db:db}=window;const[patients,setPatients]=useState([]);const[loading,setLoading]=useState(true);const[activeTab,setActiveTab]=useState("TABLERO");const[selectedPatient,setSelectedPatient]=useState(null);const[organization,setOrganization]=useState(null);const[showAdoptModal,setShowAdoptModal]=useState(false);const[adoptEmail,setAdoptEmail]=useState("");const[adopting,setAdopting]=useState(false);const[adoptError,setAdoptError]=useState(null);const[adoptSuccess,setAdoptSuccess]=useState(false);const[foundUser,setFoundUser]=useState(null);const[profileEmail,setProfileEmail]=useState(user?.email||"");const[profilePhone,setProfilePhone]=useState(user?.phone||"");const[profilePic,setProfilePic]=useState(user?.avatar||"");const[uploadingProfilePic,setUploadingProfilePic]=useState(false);const[assignmentFile,setAssignmentFile]=useState(null);const[assignmentTitle,setAssignmentTitle]=useState("");const[uploadingAssignment,setUploadingAssignment]=useState(false);const[uploadProgress,setUploadProgress]=useState(0);const[patientTasks,setPatientTasks]=useState([]);const fileInputRef=useRef(null);const profilePicInputRef=useRef(null);useEffect(()=>{if(!user||!user.centerId&&!user.center_id)return;const orgId=user.centerId||user.center_id;firestore.getDoc(firestore.doc(db,"organizations",orgId)).then(doc=>{if(doc.exists())setOrganization(doc.data())})},[user]);useEffect(()=>{if(!user)return;const q=firestore.query(firestore.collection(db,"patients"),firestore.where("assignedSpecialistId","==",user.uid||user.id));return firestore.onSnapshot(q,snap=>{setPatients(snap.docs.map(d=>({id:d.id,...d.data()})));setLoading(false)})},[user]);useEffect(()=>{if(!selectedPatient)return setPatientTasks([]);const q=firestore.query(firestore.collection(db,"patients",selectedPatient.id,"tasks"),firestore.orderBy("timestamp","desc"));return firestore.onSnapshot(q,snap=>setPatientTasks(snap.docs.map(d=>({id:d.id,...d.data()}))))},[selectedPatient]);const handleSearchAdopt=async e=>{e.preventDefault();setAdopting(true);setAdoptError(null);setFoundUser(null);try{const q=firestore.query(firestore.collection(db,"users"),firestore.where("email","==",adoptEmail.toLowerCase().trim()));const snap=await firestore.getDocs(q);if(!snap.empty){setFoundUser({id:snap.docs[0].id,...snap.docs[0].data()})}else{setAdoptError("Usuario no encontrado en Neuro-Wellness 360.")}}catch(err){setAdoptError(err.message)}finally{setAdopting(false)}};const handleConfirmAdopt=async()=>{if(!foundUser)return;setAdopting(true);try{const orgId=user.centerId||user.center_id;const updateObj={orgId:orgId,assignedSpecialistId:user.uid||user.id,status:"clinical"};await firestore.updateDoc(firestore.doc(db,"users",foundUser.id),updateObj);const patientDoc=await firestore.getDoc(firestore.doc(db,"patients",foundUser.id));const patientData={nombre:foundUser.nickname||foundUser.name||adoptEmail.split("@")[0],email:foundUser.email,assignedSpecialistId:user.uid||user.id,centerId:orgId,status:"clinical"};if(patientDoc.exists())await firestore.updateDoc(firestore.doc(db,"patients",foundUser.id),patientData);else await firestore.setDoc(firestore.doc(db,"patients",foundUser.id),patientData);setAdoptSuccess(true);setAdoptEmail("");setTimeout(()=>{setShowAdoptModal(false);setAdoptSuccess(false);setFoundUser(null)},2e3)}catch(err){setAdoptError(err.message)}finally{setAdopting(false)}};const handleInviteNew=async()=>{setAdopting(true);try{await firestore.addDoc(firestore.collection(db,"mail"),{to:adoptEmail,from:"NeuroHUB <myneurohubglobal@gmail.com>",message:{subject:"Invitación a NeuroHUB Wellness",html:`<h3>¡Hola!</h3><p>Tu especialista quiere invitarte a NeuroHUB.</p><a href="https://neurohub.health">Registrarse aquí</a>`}});alert("Invitación enviada.");setShowAdoptModal(false)}catch(err){setAdoptError(err.message)}finally{setAdopting(false)}};const handleProfileUpdate=async e=>{e.preventDefault();try{const updates={email:profileEmail,phone:profilePhone};if(profilePic!==user.avatar)updates.avatar=profilePic;await firestore.updateDoc(firestore.doc(db,"users",user.uid||user.id),updates);onUserUpdate(updates);setIsEditingProfile(false);alert("Perfil actualizado")}catch(err){alert(err.message)}};const handleProfilePicChange=async e=>{const file=e.target.files[0];if(!file)return;setUploadingProfilePic(true);try{const storageRef=firebaseStorageFns.ref(firebaseStorage,`specialists/${user.uid||user.id}/avatars/${Date.now()}_${file.name}`);const snap=await firebaseStorageFns.uploadBytes(storageRef,file);const url=await firebaseStorageFns.getDownloadURL(snap.ref);setProfilePic(url)}catch(err){alert(err.message)}finally{setUploadingProfilePic(false)}};const handleAssignmentUpload=async e=>{e.preventDefault();if(!assignmentFile||!selectedPatient||!assignmentTitle.trim())return;setUploadingAssignment(true);setUploadProgress(10);try{const storageRef=firebaseStorageFns.ref(firebaseStorage,`assignments/${selectedPatient.id}/${Date.now()}_${assignmentFile.name}`);const snap=await firebaseStorageFns.uploadBytes(storageRef,assignmentFile);setUploadProgress(80);const url=await firebaseStorageFns.getDownloadURL(snap.ref);await firestore.addDoc(firestore.collection(db,"patients",selectedPatient.id,"tasks"),{title:assignmentTitle,fileName:assignmentFile.name,downloadURL:url,timestamp:firestore.serverTimestamp(),assignedBy:user.uid||user.id});setUploadProgress(100);setAssignmentFile(null);setAssignmentTitle("");if(fileInputRef.current)fileInputRef.current.value="";setTimeout(()=>{setUploadProgress(0);setUploadingAssignment(false)},1500)}catch(err){alert(err.message);setUploadingAssignment(false)}};return React.createElement("div",{className:"min-h-screen bg-slate-50 relative flex font-sans"},React.createElement("div",{className:"w-64 bg-slate-900 text-white min-h-screen flex flex-col shadow-xl z-10 shrink-0 sticky top-0"},React.createElement("div",{className:"p-6 border-b border-slate-800"},React.createElement("div",{className:"flex items-center gap-3 mb-2"},React.createElement("img",{src:"./assets/NeuroHUB-brain.png",className:"w-8 h-8 object-contain",alt:"Logo"}),React.createElement("span",{className:"font-black text-xl italic"},"NeuroCenter")),React.createElement("div",{className:"text-xs font-bold text-slate-400 pl-11"},"Panel Especialista")),React.createElement("nav",{className:"flex-1 p-4 space-y-2"},[{id:"TABLERO",icon:Icons.LayoutDashboard,label:"Mis Pacientes"},{id:"PERFIL",icon:Icons.UserCircle,label:"Ajustar Perfil"}].map(item=>React.createElement("button",{key:item.id,onClick:()=>{setActiveTab(item.id);setSelectedPatient(null)},className:`w-full flex items-center gap-3 px-4 py-3 rounded-xl transition-all font-bold text-sm ${activeTab===item.id&&!selectedPatient?"bg-indigo-500 text-white shadow-lg":"text-slate-400 hover:bg-slate-800 hover:text-white"}`},React.createElement(item.icon,{size:18}),item.label))),React.createElement("div",{className:"p-4 border-t border-slate-800"},React.createElement("button",{onClick:onHome,className:"w-full flex items-center gap-3 px-4 py-3 rounded-xl transition-all font-bold text-sm hover:bg-slate-800 text-slate-300"},React.createElement(Icons.Home,{size:18}),"NeuroHUB Home"),React.createElement("button",{onClick:onLogout,className:"w-full flex items-center gap-3 px-4 py-3 rounded-xl font-bold text-sm text-red-400 hover:bg-red-500/10 mt-2"},React.createElement(Icons.LogOut,{size:18}),"Cerrar Sesión"))),React.createElement("div",{className:"flex-1 flex flex-col h-screen overflow-hidden"},React.createElement("header",{className:"bg-white border-b border-slate-200 px-8 py-5 shrink-0 flex justify-between items-center z-10"},React.createElement("div",null,React.createElement("h1",{className:"text-2xl font-black text-slate-900"},selectedPatient?`Paciente: ${selectedPatient.nombre}`:activeTab==="TABLERO"?"Mis Pacientes":"Perfil"),selectedPatient&&React.createElement("button",{onClick:()=>setSelectedPatient(null),className:"text-sm text-indigo-500 font-bold mt-1"},"← Volver")),React.createElement("div",{className:"flex gap-2"},!selectedPatient&&activeTab==="TABLERO"&&React.createElement("button",{onClick:()=>setShowAdoptModal(true),className:"px-4 py-2 bg-indigo-600 text-white rounded-xl font-black text-xs uppercase tracking-widest shadow-lg shadow-indigo-600/20 flex items-center gap-2"},React.createElement(Icons.Plus,{size:16}),"Vincular Paciente"),organization&&React.createElement("div",{className:"flex items-center gap-3 ml-4"},React.createElement("div",{className:"text-right"},React.createElement("p",{className:"text-[9px] font-black text-slate-400 uppercase"},"Centro"),React.createElement("h2",{className:"text-sm font-black text-slate-900"},organization.name)),organization.logo?React.createElement("img",{src:organization.logo,className:"w-10 h-10 object-contain rounded border border-slate-100"}):React.createElement(Icons.Building,{size:20})))),React.createElement("div",{className:"flex-1 overflow-y-auto p-8"},!loading&&!selectedPatient&&activeTab==="TABLERO"&&React.createElement("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6"},patients.map(p=>React.createElement("div",{key:p.id,onClick:()=>setSelectedPatient(p),className:"bg-white p-6 rounded-2xl border border-slate-200 shadow-sm hover:shadow-md transition-all cursor-pointer group"},React.createElement("div",{className:"w-16 h-16 bg-indigo-50 text-indigo-600 rounded-full flex items-center justify-center text-2xl font-black mb-4 mx-auto group-hover:bg-indigo-600 group-hover:text-white transition-all shadow-sm"},p.nombre?.[0]||"P"),React.createElement("h3",{className:"text-lg font-black text-slate-900 text-center mb-1"},p.nombre),React.createElement("p",{className:"text-xs font-bold text-slate-400 text-center uppercase tracking-widest mb-4"},p.email),React.createElement("div",{className:"pt-4 border-t border-slate-100 flex justify-between items-center text-xs text-slate-500"},React.createElement("span",null,p.status==="clinical"?"✨ Control Clínico":"Autónomo"),React.createElement("span",{className:"font-bold text-indigo-500"},"Ver →"))))),selectedPatient&&React.createElement("div",{className:"max-w-4xl mx-auto space-y-8"},React.createElement("div",{className:"bg-white rounded-2xl border border-slate-200 shadow-sm p-6 flex items-start gap-6"},React.createElement("div",{className:"w-20 h-20 bg-indigo-50 text-indigo-600 rounded-2xl flex items-center justify-center text-3xl font-black shrink-0"},selectedPatient.nombre?.[0]),React.createElement("div",{className:"flex-1"},React.createElement("h2",{className:"text-2xl font-black text-slate-900 mb-1"},selectedPatient.nombre),React.createElement("p",{className:"text-sm text-slate-500"},selectedPatient.email))),React.createElement("div",{className:"bg-white rounded-2xl border border-slate-200 shadow-sm p-6"},React.createElement("h3",{className:"text-lg font-black text-slate-900 flex items-center gap-2 mb-6"},React.createElement(Icons.FileText,{size:20,className:"text-indigo-500"}),"Plan de Trabajo Semanal"),React.createElement("form",{onSubmit:handleAssignmentUpload,className:"space-y-4 mb-8 bg-slate-50 p-4 rounded-xl"},React.createElement("div",{className:"grid grid-cols-2 gap-4"},React.createElement("input",{type:"text",value:assignmentTitle,onChange:e=>setAssignmentTitle(e.target.value),placeholder:"Título: Ej. Ejercicios de Atención",className:"w-full p-3 border border-slate-200 rounded-xl outline-none"}),React.createElement("input",{type:"file",ref:fileInputRef,onChange:e=>setAssignmentFile(e.target.files[0]),className:"w-full p-2 border border-slate-200 rounded-xl bg-white"})),React.createElement("div",{className:"flex justify-end gap-4 items-center"},uploadingAssignment&&React.createElement("div",{className:"flex-1 h-2 bg-slate-200 rounded-full overflow-hidden"},React.createElement("div",{className:"h-full bg-indigo-600 transition-all",style:{width:`${uploadProgress}%`}})),React.createElement("button",{type:"submit",disabled:uploadingAssignment||!assignmentTitle||!assignmentFile,className:"px-6 py-2 bg-indigo-600 text-white font-bold rounded-xl shadow-lg disabled:opacity-50"},uploadingAssignment?"Subiendo...":"Asignar Material"))),React.createElement("div",{className:"space-y-3"},patientTasks.map(task=>React.createElement("div",{key:task.id,className:"flex items-center justify-between p-4 border border-slate-100 rounded-xl hover:bg-slate-50"},React.createElement("div",null,React.createElement("h4",{className:"font-bold text-slate-800"},task.title),React.createElement("p",{className:"text-[10px] text-slate-400 font-bold uppercase"},new Date(task.timestamp?.toDate()).toLocaleDateString())),React.createElement("a",{href:task.downloadURL,target:"_blank",className:"p-2 hover:bg-indigo-100 rounded-full text-indigo-500"},React.createElement(Icons.Download,{size:18}))))))),activeTab==="PERFIL"&&React.createElement("div",{className:"max-w-xl mx-auto bg-white p-8 rounded-3xl border border-slate-200 shadow-sm"},React.createElement("h2",{className:"text-xl font-black mb-6"},"Mi Perfil Profesional"),React.createElement("form",{onSubmit:handleProfileUpdate,className:"space-y-6"},React.createElement("div",{className:"flex flex-col items-center"},React.createElement("div",{className:"w-24 h-24 rounded-full overflow-hidden border-4 border-slate-100 bg-slate-50 flex items-center justify-center mb-4"},profilePic?React.createElement("img",{src:profilePic,className:"w-full h-full object-cover"}):React.createElement(Icons.User,{size:40,className:"text-slate-300"})),React.createElement("input",{type:"file",ref:profilePicInputRef,onChange:handleProfilePicChange,className:"hidden",accept:"image/*"}),React.createElement("button",{type:"button",onClick:()=>profilePicInputRef.current.click(),className:"text-xs font-black text-indigo-600 uppercase tracking-widest"},"Cambiar Foto")),React.createElement("div",{className:"space-y-4"},React.createElement("input",{type:"email",value:profileEmail,onChange:e=>setProfileEmail(e.target.value),className:"w-full p-4 border rounded-xl bg-slate-50",placeholder:"Email"}),React.createElement("input",{type:"tel",value:profilePhone,onChange:e=>setProfilePhone(e.target.value),className:"w-full p-4 border rounded-xl bg-slate-50",placeholder:"Teléfono"})),React.createElement("button",{type:"submit",className:"w-full py-4 bg-slate-900 text-white rounded-xl font-black uppercase tracking-widest text-xs"},"Guardar"))))),showAdoptModal&&React.createElement("div",{className:"fixed inset-0 z-[100000] flex items-center justify-center p-8 bg-slate-950/90 backdrop-blur-sm animate-fadeIn"},React.createElement("div",{className:"bg-white rounded-[2.5rem] p-10 max-w-md w-full relative"},React.createElement("button",{onClick:()=>{setShowAdoptModal(false);setFoundUser(null);setAdoptError(null)},className:"absolute top-6 right-6 text-slate-400"},React.createElement(Icons.X,{size:24})),React.createElement("h2",{className:"text-2xl font-black mb-2"},"Vincular Paciente"),React.createElement("p",{className:"text-slate-500 text-sm mb-8"},"Ingresa el correo para buscar en Neuro-Wellness 360."),React.createElement("form",{onSubmit:handleSearchAdopt,className:"space-y-4 mb-6"},React.createElement("input",{type:"email",required:true,value:adoptEmail,onChange:e=>setAdoptEmail(e.target.value),className:"w-full p-5 border border-slate-200 rounded-2xl outline-none focus:border-indigo-600 font-bold",placeholder:"paciente@email.com",disabled:foundUser||adopting}),!foundUser&&React.createElement("button",{disabled:adopting||!adoptEmail,className:"w-full py-5 bg-indigo-600 text-white rounded-2xl font-black uppercase text-xs"},adopting?"Buscando...":"Buscar")),foundUser&&React.createElement("div",{className:"bg-indigo-50 p-6 rounded-2xl border border-indigo-100 mb-6 text-center"},React.createElement("div",{className:"w-16 h-16 bg-white rounded-full flex items-center justify-center mx-auto mb-4 border-2 border-indigo-200"},foundUser.avatar||"👤"),React.createElement("h3",{className:"font-black text-slate-900"},foundUser.nickname||foundUser.name),React.createElement("p",{className:"text-xs text-slate-500 mb-6"},foundUser.email),!adoptSuccess?React.createElement("button",{onClick:handleConfirmAdopt,disabled:adopting,className:"w-full py-4 bg-indigo-600 text-white rounded-xl font-bold text-xs uppercase"},"Vincular a mi Panel"):React.createElement("div",{className:"text-emerald-600 font-black"},"✅ Vinculado con éxito")),adoptError&&React.createElement("div",{className:"p-6 rounded-2xl bg-amber-50 border border-amber-100 text-center"},React.createElement("p",{className:"text-sm text-amber-700 font-bold mb-4"},adoptError),React.createElement("button",{onClick:handleInviteNew,className:"w-full py-4 bg-amber-600 text-white rounded-xl font-bold text-xs uppercase"},"Enviar Invitación por Email")))))};