import{r as i,j as a,N as K,H as Je,O as h,z as j,I as re,_ as Ge,n as d,J as y}from"./index-DuuohOak.js";import{u as Ve}from"./useAdmin-Cd7hRYKP.js";const We="_adminContainer_j11pf_5",qe="_adminHeader_j11pf_13",Ke="_adminSection_j11pf_20",Ye="_modelSelectionArea_j11pf_29",Qe="_modelSelectionRow_j11pf_36",Xe="_modelsTable_j11pf_89",Ze="_promptSubsection_j11pf_97",ea="_promptSubsectionTitle_j11pf_109",aa="_modelCode_j11pf_152",la="_modelId_j11pf_157",sa="_modelActions_j11pf_163",oa="_addModelButtonContainer_j11pf_169",ta="_addModelForm_j11pf_177",ia="_addModelFormTitle_j11pf_185",da="_addModelFormGrid_j11pf_193",na="_addModelFormField_j11pf_206",ra="_addModelFormCheckbox_j11pf_250",ca="_addModelFormActions_j11pf_268",ma="_modalOverlay_j11pf_277",ha="_modalContent_j11pf_291",pa="_modalBody_j11pf_302",_a="_modalTitle_j11pf_312",xa="_modalText_j11pf_320",ua="_modalTextLeft_j11pf_332",ja="_modalButtons_j11pf_355",va="_modalForm_j11pf_363",ga="_formField_j11pf_372",fa="_formLabel_j11pf_376",ba="_formInput_j11pf_385",ya="_textarea_j11pf_407",l={adminContainer:We,adminHeader:qe,adminSection:Ke,modelSelectionArea:Ye,modelSelectionRow:Qe,modelsTable:Xe,promptSubsection:Ze,promptSubsectionTitle:ea,modelCode:aa,modelId:la,modelActions:sa,addModelButtonContainer:oa,addModelForm:ta,addModelFormTitle:ia,addModelFormGrid:da,addModelFormField:na,addModelFormCheckbox:ra,addModelFormActions:ca,modalOverlay:ma,modalContent:ha,modalBody:pa,modalTitle:_a,modalText:xa,modalTextLeft:ua,modalButtons:ja,modalForm:va,formField:ga,formLabel:fa,formInput:ba,textarea:ya},I="selectedVlmModel";function Ca(){const{isAuthenticated:v,isLoading:ce,login:me,logout:he,verifyToken:Y}=Ve(),[E,Q]=i.useState(""),[X,g]=i.useState(""),[Z,ee]=i.useState(!1),[N,S]=i.useState([]),[pe,C]=i.useState(""),[_e,M]=i.useState(""),[ae,P]=i.useState([]),[xe,L]=i.useState([]),[ue,B]=i.useState([]),[je,k]=i.useState(!1),[O,D]=i.useState(null),[U,f]=i.useState({schema_id:"",title:"",version:"",schema:{}}),[ve,w]=i.useState(!1),[ge,T]=i.useState(!1),[fe,z]=i.useState(null),[F,R]=i.useState(null),[n,c]=i.useState({p_code:"",label:"",metadata_instructions:"",image_type:"crisis_map",is_active:!1}),[le,H]=i.useState(!1),[be,J]=i.useState(!1),[G,V]=i.useState(null),[t,m]=i.useState({m_code:"",label:"",model_type:"custom",provider:"huggingface",model_id:"",is_available:!1,is_fallback:!1}),[ye,A]=i.useState(!1),[Ne,W]=i.useState(!1),[Se,_]=i.useState(!1),[se,oe]=i.useState(""),[Ce,Me]=i.useState(""),[ke,x]=i.useState(""),[we,$]=i.useState(""),u=i.useCallback(()=>{fetch("/api/models").then(e=>{if(!e.ok)throw new Error(`HTTP ${e.status}: ${e.statusText}`);return e.json()}).then(e=>{if(console.log("Models data received:",e),e&&Array.isArray(e.models)){S(e.models);const s=localStorage.getItem(I);if(e.models.length>0)if(s==="random")C("random");else if(s&&e.models.find(o=>o.m_code===s&&o.is_available))C(s);else{const o=e.models.find(r=>r.is_available)||e.models[0];C(o.m_code),localStorage.setItem(I,o.m_code)}fetch("/api/admin/fallback-model",{headers:{Authorization:`Bearer ${localStorage.getItem("adminToken")}`}}).then(o=>{if(!o.ok)throw new Error(`HTTP ${o.status}: ${o.statusText}`);return o.json()}).then(o=>{if(console.log("Fallback model data received:",o),o.fallback_model)M(o.fallback_model.m_code);else{const r=e.models.find(p=>p.m_code==="STUB_MODEL"&&p.is_available);M(r?"STUB_MODEL":"")}}).catch(o=>{console.error("Error fetching fallback model:",o);const r=e.models.find(p=>p.m_code==="STUB_MODEL"&&p.is_available);M(r?"STUB_MODEL":"")})}else console.error("Expected models object but got:",e),S([])}).catch(e=>{console.error("Error fetching models:",e),S([])})},[]),b=i.useCallback(()=>{console.log("=== fetchPrompts called ==="),fetch("/api/prompts").then(e=>{if(!e.ok)throw new Error(`HTTP ${e.status}: ${e.statusText}`);return e.json()}).then(e=>{console.log("Prompts data received:",e),Array.isArray(e)?P(e):(console.error("Expected array but got:",e),P([])),console.log("State update triggered with:",e||[])}).catch(e=>{console.error("Error fetching prompts:",e),P([])})},[]),te=i.useCallback(()=>{fetch("/api/image-types").then(e=>{if(!e.ok)throw new Error(`HTTP ${e.status}: ${e.statusText}`);return e.json()}).then(e=>{console.log("Image types data received:",e),Array.isArray(e)?L(e):(console.error("Expected array but got:",e),L([]))}).catch(e=>{console.error("Error fetching image types:",e),L([])})},[]),q=i.useCallback(()=>{console.log("=== fetchSchemas called ==="),fetch("/api/schemas",{headers:{Authorization:`Bearer ${localStorage.getItem("adminToken")}`}}).then(e=>{if(!e.ok)throw new Error(`HTTP ${e.status}: ${e.statusText}`);return e.json()}).then(e=>{console.log("Schemas data received:",e),Array.isArray(e)?B(e):(console.error("Expected array but got:",e),B([]))}).catch(e=>{console.error("Error fetching schemas:",e),B([])})},[]);i.useEffect(()=>{v&&(u(),b(),te(),q())},[v,u,b,te,q]),i.useEffect(()=>{v&&Y()},[v,Y]);const ie=e=>{R(e),c({p_code:e.p_code,label:e.label||"",metadata_instructions:e.metadata_instructions||"",image_type:e.image_type||"crisis_map",is_active:e.is_active||!1}),w(!0)},Te=async()=>{try{if(!F){alert("No prompt selected for editing");return}const e=await fetch(`/api/prompts/${F.p_code}`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({label:n.label,metadata_instructions:n.metadata_instructions,image_type:n.image_type,is_active:n.is_active})});if(e.ok)b(),w(!1),R(null),c({p_code:"",label:"",metadata_instructions:"",image_type:"crisis_map",is_active:!1});else{const s=await e.json();alert(`Failed to update prompt: ${s.error||"Unknown error"}`)}}catch{alert("Error updating prompt")}},de=async(e,s)=>{try{const o=await fetch(`/api/prompts/${e}/toggle-active?image_type=${s}`,{method:"POST",headers:{"Content-Type":"application/json"}});if(o.ok)b();else{const r=await o.json();alert(`Failed to toggle prompt active status: ${r.detail||"Unknown error"}`)}}catch{alert("Error toggling prompt active status")}},ne=e=>{z(e),c({p_code:"",label:"",metadata_instructions:"",image_type:e,is_active:!1}),T(!0)},Fe=async()=>{try{const e=await fetch("/api/prompts",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)});if(e.ok)b(),T(!1),z(null),c({p_code:"",label:"",metadata_instructions:"",image_type:"crisis_map",is_active:!1});else{const s=await e.json();alert(`Failed to create prompt: ${s.detail||"Unknown error"}`)}}catch{alert("Error creating prompt")}},Ae=e=>{D(e),f({schema_id:e.schema_id,title:e.title,version:e.version,schema:e.schema}),k(!0)},$e=async()=>{try{if(!O){alert("No schema selected for editing");return}const e=await fetch(`/api/schemas/${O.schema_id}`,{method:"PUT",headers:{"Content-Type":"application/json",Authorization:`Bearer ${localStorage.getItem("adminToken")}`},body:JSON.stringify(U)});if(e.ok)q(),k(!1),D(null),f({schema_id:"",title:"",version:"",schema:{}});else{const s=await e.json();alert(`Failed to save schema: ${s.detail||"Unknown error"}`)}}catch(e){console.error("Error saving schema:",e),alert("Error saving schema")}},Ie=()=>{k(!1),D(null),f({schema_id:"",title:"",version:"",schema:{}})},Ee=async(e,s)=>{try{const o=await fetch(`/api/models/${e}/toggle`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({is_available:!s})});if(o.ok)S(r=>(r||[]).map(p=>p.m_code===e?{...p,is_available:!s}:p));else{const r=await o.json();alert(`Failed to toggle model availability: ${r.error||"Unknown error"}`)}}catch{alert("Error toggling model availability")}},Pe=e=>{C(e),e==="random"?localStorage.setItem(I,"random"):localStorage.setItem(I,e)},Le=async e=>{try{const s=await fetch(`/api/admin/models/${e}`,{method:"PUT",headers:{"Content-Type":"application/json",Authorization:`Bearer ${localStorage.getItem("adminToken")}`},body:JSON.stringify({is_fallback:!0})});if(s.ok)M(e),u();else{const o=await s.json();alert(`Failed to set fallback model: ${o.detail||"Unknown error"}`)}}catch{alert("Error setting fallback model")}},Be=async()=>{try{const e=await fetch("/api/admin/models",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${localStorage.getItem("adminToken")}`},body:JSON.stringify(t)});if(e.ok){const s=` Model "${t.label}" added successfully! ⚠️ IMPORTANT: Model will NOT work until you complete these steps: 1. 🔑 Ensure API key is set and valid. 2. 📝 Verify model_id format. 3. 📚 Check model specific documentation for details. `;Me(s),W(!0),H(!1),m({m_code:"",label:"",model_type:"custom",provider:"huggingface",model_id:"",is_available:!1,is_fallback:!1}),u()}else{const s=await e.json();alert(`Failed to add model: ${s.detail||"Unknown error"}`)}}catch{alert("Error adding model")}},Oe=e=>{V(e),m({m_code:e.m_code,label:e.label,model_type:e.model_type||"custom",provider:e.provider||e.config?.provider||"huggingface",model_id:e.model_id||e.config?.model_id||e.m_code,is_available:e.is_available,is_fallback:e.is_fallback}),J(!0)},De=async()=>{try{console.log("Updating model with data:",t);const e={label:t.label,model_type:t.model_type,provider:t.provider,model_id:t.model_id,is_available:t.is_available};if(console.log("Update payload:",e),!G){alert("No model selected for editing");return}const s=await fetch(`/api/admin/models/${G.m_code}`,{method:"PUT",headers:{"Content-Type":"application/json",Authorization:`Bearer ${localStorage.getItem("adminToken")}`},body:JSON.stringify(e)});if(console.log("Update response status:",s.status),s.ok){const o=await s.json();console.log("Update successful:",o),J(!1),V(null),m({m_code:"",label:"",model_type:"custom",provider:"huggingface",model_id:"",is_available:!1,is_fallback:!1}),console.log("Refreshing models..."),u()}else{const o=await s.json();console.error("Update failed:",o),alert(`Failed to update model: ${o.detail||"Unknown error"}`)}}catch(e){console.error("Update error:",e),alert("Error updating model")}},Ue=async e=>{oe(e),A(!0)},ze=async()=>{try{const e=await fetch(`/api/admin/models/${se}`,{method:"DELETE",headers:{Authorization:`Bearer ${localStorage.getItem("adminToken")}`}});if(e.ok)A(!1),oe(""),u();else{const s=await e.json();alert(`Failed to delete model: ${s.detail||"Unknown error"}`)}}catch{alert("Error deleting model")}},Re=async e=>{if(e.preventDefault(),!E.trim()){g("Please enter a password");return}ee(!0),g("");try{await me(E)||g("Invalid password")}catch{g("Login failed. Please try again.")}finally{ee(!1)}},He=()=>{he(),Q(""),g("")};return ce?a.jsx(K,{children:a.jsx("div",{className:"flex items-center justify-center min-h-[400px]",children:a.jsxs("div",{className:"text-center",children:[a.jsx("div",{className:"animate-spin rounded-full h-12 w-12 border-b-2 border-ifrcRed mx-auto mb-4"}),a.jsx("p",{className:"text-gray-600",children:"Loading..."})]})})}):v?a.jsxs(K,{children:[a.jsxs("div",{className:l.adminContainer,children:[a.jsx("div",{className:l.adminHeader,children:a.jsx(d,{name:"logout",variant:"secondary",onClick:He,children:"Logout"})}),a.jsxs("div",{className:l.adminSection,children:[a.jsx(j,{heading:"VLM Model Selection",headingLevel:2,withHeaderBorder:!0,withInternalPadding:!0,children:a.jsxs("div",{className:l.modelSelectionArea,children:[a.jsx("p",{className:"text-gray-700",children:"Select which Vision Language Model to use for caption generation."}),a.jsxs("div",{className:l.modelSelectionRow,children:[a.jsx(y,{label:"Model",name:"selected-model",value:pe,onChange:e=>{const s=e||N.find(o=>o.m_code==="STUB_MODEL"&&o.is_available)?.m_code||"STUB_MODEL";Pe(s)},options:[{value:"random",label:"Random"},...(N||[]).filter(e=>e.is_available).map(e=>({value:e.m_code,label:e.label}))],keySelector:e=>e.value,labelSelector:e=>e.label}),a.jsx(y,{label:"Fallback",name:"fallback-model",value:_e,onChange:e=>Le(e||"STUB_MODEL"),options:(N||[]).filter(e=>e.is_available).map(e=>({value:e.m_code,label:e.label})),keySelector:e=>e.value,labelSelector:e=>e.label})]})]})}),a.jsx(j,{heading:"Model Management",headingLevel:2,withHeaderBorder:!0,withInternalPadding:!0,children:a.jsxs("div",{className:l.modelManagementArea,children:[a.jsx("div",{className:l.modelsTable,children:a.jsxs("table",{children:[a.jsx("thead",{children:a.jsxs("tr",{children:[a.jsx("th",{children:"Code"}),a.jsx("th",{children:"Label"}),a.jsx("th",{children:"Provider"}),a.jsx("th",{children:"Model ID"}),a.jsx("th",{children:"Available"}),a.jsx("th",{children:"Actions"})]})}),a.jsx("tbody",{children:(N||[]).map(e=>a.jsxs("tr",{children:[a.jsx("td",{className:l.modelCode,children:e.m_code}),a.jsx("td",{children:e.label}),a.jsx("td",{children:e.provider||e.config?.provider||"huggingface"}),a.jsx("td",{className:l.modelId,children:e.model_id||e.config?.model_id||e.m_code||"N/A"}),a.jsx("td",{children:a.jsx(d,{name:`toggle-${e.m_code}`,variant:e.is_available?"primary":"secondary",size:1,onClick:()=>Ee(e.m_code,e.is_available),children:e.is_available?"Enabled":"Disabled"})}),a.jsx("td",{children:a.jsxs("div",{className:l.modelActions,children:[a.jsx(d,{name:`edit-${e.m_code}`,variant:"secondary",size:1,onClick:()=>Oe(e),children:"Edit"}),a.jsx(d,{name:`delete-${e.m_code}`,variant:"secondary",size:1,onClick:()=>Ue(e.m_code),children:"Delete"})]})})]},e.m_code))})]})}),!le&&a.jsx("div",{className:l.addModelButtonContainer,children:a.jsx(d,{name:"show-add-form",variant:"primary",onClick:()=>H(!0),children:"Add New Model"})}),le&&a.jsxs("div",{className:l.addModelForm,children:[a.jsx("h4",{className:l.addModelFormTitle,children:"Add New Model"}),a.jsxs("div",{className:l.addModelFormGrid,children:[a.jsx("div",{className:l.addModelFormField,children:a.jsx(h,{label:"Model Code",name:"model-code",value:t.m_code,onChange:e=>m({...t,m_code:e||""}),placeholder:"e.g., NEW_MODEL_123"})}),a.jsx("div",{className:l.addModelFormField,children:a.jsx(h,{label:"Label",name:"model-label",value:t.label,onChange:e=>m({...t,label:e||""}),placeholder:"e.g., New Model Name"})}),a.jsx("div",{className:l.addModelFormField,children:a.jsx(y,{label:"Provider",name:"model-provider",value:t.provider,onChange:e=>m({...t,provider:e||"huggingface"}),options:[{value:"huggingface",label:"HuggingFace"},{value:"openai",label:"OpenAI"},{value:"google",label:"Google"}],keySelector:e=>e.value,labelSelector:e=>e.label})}),a.jsx("div",{className:l.addModelFormField,children:a.jsx(h,{label:"Model ID",name:"model-id",value:t.model_id,onChange:e=>m({...t,model_id:e||""}),placeholder:"e.g., org/model-name"})}),a.jsx("div",{className:l.addModelFormField,children:a.jsxs("div",{className:l.addModelFormCheckbox,children:[a.jsx("input",{type:"checkbox",checked:t.is_available,onChange:e=>m({...t,is_available:e.target.checked})}),a.jsx("span",{children:"Available for use"})]})})]}),a.jsxs("div",{className:l.addModelFormActions,children:[a.jsx(d,{name:"save-model",variant:"primary",onClick:Be,disabled:!t.m_code||!t.label||!t.model_id,children:"Save Model"}),a.jsx(d,{name:"cancel-add",variant:"secondary",onClick:()=>H(!1),children:"Cancel"})]})]}),be&&a.jsxs("div",{className:l.addModelForm,children:[a.jsxs("h4",{className:l.addModelFormTitle,children:["Edit Model: ",G?.label]}),a.jsxs("div",{className:l.addModelFormGrid,children:[a.jsx("div",{className:l.addModelFormField,children:a.jsx(h,{label:"Model Code",name:"model-code",value:t.m_code,onChange:e=>m({...t,m_code:e||""}),placeholder:"e.g., NEW_MODEL_123",disabled:!0})}),a.jsx("div",{className:l.addModelFormField,children:a.jsx(h,{label:"Label",name:"model-label",value:t.label,onChange:e=>m({...t,label:e||""}),placeholder:"e.g., New Model Name"})}),a.jsx("div",{className:l.addModelFormField,children:a.jsx(y,{label:"Provider",name:"model-provider",value:t.provider,onChange:e=>m({...t,provider:e||"huggingface"}),options:[{value:"huggingface",label:"HuggingFace"},{value:"openai",label:"OpenAI"},{value:"google",label:"Google"}],keySelector:e=>e.value,labelSelector:e=>e.label})}),a.jsx("div",{className:l.addModelFormField,children:a.jsx(h,{label:"Model ID",name:"model-id",value:t.model_id,onChange:e=>m({...t,model_id:e||""}),placeholder:"e.g., org/model-name"})}),a.jsx("div",{className:l.addModelFormField,children:a.jsxs("div",{className:l.addModelFormCheckbox,children:[a.jsx("input",{type:"checkbox",checked:t.is_available,onChange:e=>m({...t,is_available:e.target.checked})}),a.jsx("span",{children:"Available for use"})]})})]}),a.jsxs("div",{className:l.addModelFormActions,children:[a.jsx(d,{name:"update-model",variant:"primary",onClick:De,disabled:!t.m_code||!t.label||!t.model_id,children:"Update Model"}),a.jsx(d,{name:"cancel-edit",variant:"secondary",onClick:()=>{J(!1),V(null),m({m_code:"",label:"",model_type:"custom",provider:"huggingface",model_id:"",is_available:!1,is_fallback:!1})},children:"Cancel"})]})]})]})}),a.jsx(j,{heading:"Prompt Management",headingLevel:2,withHeaderBorder:!0,withInternalPadding:!0,children:a.jsxs("div",{className:l.modelManagementArea,children:[a.jsxs("div",{className:l.promptSubsection,children:[a.jsx("h4",{className:l.promptSubsectionTitle,children:"Crisis Maps"}),a.jsx("div",{className:l.modelsTable,children:a.jsxs("table",{children:[a.jsx("thead",{children:a.jsxs("tr",{children:[a.jsx("th",{children:"Code"}),a.jsx("th",{children:"Label"}),a.jsx("th",{children:"Status"}),a.jsx("th",{children:"Actions"})]})}),a.jsx("tbody",{children:(ae||[]).filter(e=>e.image_type==="crisis_map").sort((e,s)=>e.p_code.localeCompare(s.p_code)).map(e=>a.jsxs("tr",{children:[a.jsx("td",{className:l.modelCode,children:e.p_code}),a.jsx("td",{className:l.promptLabel,children:e.label||"No label"}),a.jsx("td",{children:a.jsx(d,{name:`toggle-crisis-${e.p_code}`,variant:e.is_active?"primary":"secondary",size:1,onClick:()=>de(e.p_code,"crisis_map"),children:e.is_active?"Active":"Inactive"})}),a.jsx("td",{children:a.jsxs("div",{className:l.modelActions,children:[a.jsx(d,{name:`view-${e.p_code}`,variant:"secondary",size:1,onClick:()=>{x(`=== Prompt Details === Code: ${e.p_code} Label: ${e.label} Image Type: ${e.image_type} Active: ${e.is_active} Metadata Instructions: ${e.metadata_instructions||"No instructions available"}`),$(`Prompt: ${e.p_code}`),_(!0)},children:"View"}),a.jsx(d,{name:`edit-${e.p_code}`,variant:"secondary",size:1,onClick:()=>ie(e),children:"Edit"})]})})]},e.p_code))})]})}),a.jsx("div",{className:l.addModelButtonContainer,children:a.jsx(d,{name:"add-crisis-prompt",variant:"primary",onClick:()=>ne("crisis_map"),children:"Add New Crisis Map Prompt"})})]}),a.jsxs("div",{className:l.promptSubsection,children:[a.jsx("h4",{className:l.promptSubsectionTitle,children:"Drone Images"}),a.jsx("div",{className:l.modelsTable,children:a.jsxs("table",{children:[a.jsx("thead",{children:a.jsxs("tr",{children:[a.jsx("th",{children:"Code"}),a.jsx("th",{children:"Label"}),a.jsx("th",{children:"Status"}),a.jsx("th",{children:"Actions"})]})}),a.jsx("tbody",{children:(ae||[]).filter(e=>e.image_type==="drone_image").sort((e,s)=>e.p_code.localeCompare(s.p_code)).map(e=>a.jsxs("tr",{children:[a.jsx("td",{className:l.modelCode,children:e.p_code}),a.jsx("td",{className:l.promptLabel,children:e.label||"No label"}),a.jsx("td",{children:a.jsx(d,{name:`toggle-drone-${e.p_code}`,variant:e.is_active?"primary":"secondary",size:1,onClick:()=>de(e.p_code,"drone_image"),children:e.is_active?"Active":"Inactive"})}),a.jsx("td",{children:a.jsxs("div",{className:l.modelActions,children:[a.jsx(d,{name:`view-${e.p_code}`,variant:"secondary",size:1,onClick:()=>{x(`=== Prompt Details === Code: ${e.p_code} Label: ${e.label} Image Type: ${e.image_type} Active: ${e.is_active} Metadata Instructions: ${e.metadata_instructions||"No instructions available"}`),$(`Prompt: ${e.p_code}`),_(!0)},children:"View"}),a.jsx(d,{name:`edit-${e.p_code}`,variant:"secondary",size:1,onClick:()=>ie(e),children:"Edit"})]})})]},e.p_code))})]})}),a.jsx("div",{className:l.addModelButtonContainer,children:a.jsx(d,{name:"add-drone-prompt",variant:"primary",onClick:()=>ne("drone_image"),children:"Add New Drone Image Prompt"})})]})]})}),a.jsx(j,{heading:"Schema Management",headingLevel:2,withHeaderBorder:!0,withInternalPadding:!0,children:a.jsx("div",{className:l.modelManagementArea,children:a.jsx("div",{className:l.modelsTable,children:a.jsxs("table",{children:[a.jsx("thead",{children:a.jsxs("tr",{children:[a.jsx("th",{children:"Schema ID"}),a.jsx("th",{children:"Schema Content"}),a.jsx("th",{children:"Actions"})]})}),a.jsx("tbody",{children:(ue||[]).sort((e,s)=>e.schema_id.localeCompare(s.schema_id)).map(e=>a.jsxs("tr",{children:[a.jsx("td",{className:l.modelCode,children:e.schema_id}),a.jsx("td",{className:l.promptLabel,style:{maxWidth:"400px",overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:JSON.stringify(e.schema)}),a.jsx("td",{children:a.jsxs("div",{className:l.modelActions,children:[a.jsx(d,{name:`view-schema-${e.schema_id}`,variant:"secondary",size:1,onClick:()=>{x(`=== Schema Details === Schema ID: ${e.schema_id} Schema Definition: ${JSON.stringify(e.schema,null,2)}`),$(`Schema: ${e.schema_id}`),_(!0)},children:"View"}),a.jsx(d,{name:`edit-schema-${e.schema_id}`,variant:"secondary",size:1,onClick:()=>Ae(e),children:"Edit"})]})})]},e.schema_id))})]})})})}),a.jsx(j,{heading:"Utilities",headingLevel:2,withHeaderBorder:!0,withInternalPadding:!0,children:a.jsx("div",{className:"flex flex-wrap gap-4",children:a.jsx(d,{name:"test-connection",variant:"secondary",onClick:async()=>{x("Testing API connection..."),$("Connection Test Results");try{const e=await fetch("/api/models");if(e.ok){const s=await e.json(),o=`✅ API connection successful! Found ${s.models?.length||0} models in database. Available models: ${(s.models||[]).filter(r=>r.is_available).map(r=>`- ${r.label} (${r.m_code})`).join(` `)||"None"}`;x(o)}else{const s=`❌ API connection failed: HTTP ${e.status}`;x(s)}_(!0)}catch(e){const s=`❌ Connection error: ${e}`;x(s),_(!0)}},children:"Test Connection"})})})]})]}),ye&&a.jsx("div",{className:l.modalOverlay,onClick:()=>A(!1),children:a.jsx("div",{className:l.modalContent,onClick:e=>e.stopPropagation(),children:a.jsxs("div",{className:l.modalBody,children:[a.jsx("h3",{className:l.modalTitle,children:"Delete Model"}),a.jsxs("p",{className:l.modalText,children:["Are you sure you want to delete model ",a.jsx("span",{className:l.modelCode,children:se}),"? This action cannot be undone."]}),a.jsxs("div",{className:l.modalButtons,children:[a.jsx(d,{name:"cancel-delete",variant:"tertiary",onClick:()=>A(!1),children:"Cancel"}),a.jsx(d,{name:"confirm-delete",variant:"secondary",onClick:ze,children:"Delete"})]})]})})}),Ne&&a.jsx("div",{className:l.modalOverlay,onClick:()=>W(!1),children:a.jsx("div",{className:l.modalContent,onClick:e=>e.stopPropagation(),children:a.jsxs("div",{className:l.modalBody,children:[a.jsx("h3",{className:l.modalTitle,children:"Model Added Successfully!"}),a.jsx("div",{className:`${l.modalText} ${l.modalTextLeft}`,children:Ce}),a.jsx("div",{className:l.modalButtons,children:a.jsx(d,{name:"close-setup-instructions",variant:"secondary",onClick:()=>W(!1),children:"Got it!"})})]})})}),Se&&a.jsx("div",{className:l.modalOverlay,onClick:()=>_(!1),children:a.jsx("div",{className:l.modalContent,onClick:e=>e.stopPropagation(),children:a.jsxs("div",{className:l.modalBody,children:[a.jsx("h3",{className:l.modalTitle,children:we}),a.jsx("div",{className:`${l.modalText} ${l.modalTextLeft}`,children:a.jsx("div",{className:"whitespace-pre-wrap font-mono text-sm leading-relaxed",children:ke})}),a.jsx("div",{className:l.modalButtons,children:a.jsx(d,{name:"close-test-results",variant:"secondary",onClick:()=>_(!1),children:"Close"})})]})})}),ve&&a.jsx("div",{className:l.modalOverlay,onClick:()=>w(!1),children:a.jsx("div",{className:l.modalContent,onClick:e=>e.stopPropagation(),children:a.jsxs("div",{className:l.modalBody,children:[a.jsxs("h3",{className:l.modalTitle,children:["Edit Prompt: ",F?.p_code]}),a.jsxs("div",{className:l.modalForm,children:[a.jsxs("div",{className:l.formField,children:[a.jsx("label",{className:l.formLabel,children:"Code:"}),a.jsx(h,{name:"prompt-code",value:F?.p_code,onChange:()=>{},disabled:!0,className:l.formInput})]}),a.jsxs("div",{className:l.formField,children:[a.jsx("label",{className:l.formLabel,children:"Label:"}),a.jsx(h,{name:"prompt-label",value:n.label,onChange:e=>c(s=>({...s,label:e||""})),className:l.formInput})]}),a.jsxs("div",{className:l.formField,children:[a.jsx("label",{className:l.formLabel,children:"Image Type:"}),a.jsx(y,{name:"prompt-image-type",value:n.image_type,onChange:e=>c(s=>({...s,image_type:e||"crisis_map"})),options:xe||[],keySelector:e=>e.image_type,labelSelector:e=>e.label})]}),a.jsxs("div",{className:l.formField,children:[a.jsx("label",{className:l.formLabel,children:"Active Status:"}),a.jsxs("div",{className:l.addModelFormCheckbox,children:[a.jsx("input",{type:"checkbox",checked:n.is_active,onChange:e=>c(s=>({...s,is_active:e.target.checked}))}),a.jsx("span",{children:"Active for this image type"})]})]}),a.jsxs("div",{className:l.formField,children:[a.jsx("label",{className:l.formLabel,children:"Metadata Instructions:"}),a.jsx("textarea",{name:"prompt-instructions",value:n.metadata_instructions,onChange:e=>c(s=>({...s,metadata_instructions:e.target.value})),className:`${l.formInput} ${l.textarea}`,rows:8})]})]}),a.jsxs("div",{className:l.modalButtons,children:[a.jsx(d,{name:"cancel-edit-prompt",variant:"tertiary",onClick:()=>{w(!1),R(null),c({p_code:"",label:"",metadata_instructions:"",image_type:"crisis_map",is_active:!1})},children:"Cancel"}),a.jsx(d,{name:"save-prompt",variant:"primary",onClick:Te,children:"Save Changes"})]})]})})}),ge&&a.jsx("div",{className:l.modalOverlay,onClick:()=>T(!1),children:a.jsx("div",{className:l.modalContent,onClick:e=>e.stopPropagation(),children:a.jsxs("div",{className:l.modalBody,children:[a.jsxs("h3",{className:l.modalTitle,children:["Add New ",fe==="crisis_map"?"Crisis Map":"Drone Image"," Prompt"]}),a.jsxs("div",{className:l.modalForm,children:[a.jsxs("div",{className:l.formField,children:[a.jsx("label",{className:l.formLabel,children:"Code:"}),a.jsx(h,{name:"prompt-code",value:n.p_code,onChange:e=>c(s=>({...s,p_code:e||""})),placeholder:"e.g., CUSTOM_CRISIS_MAP_001",className:l.formInput})]}),a.jsxs("div",{className:l.formField,children:[a.jsx("label",{className:l.formLabel,children:"Label:"}),a.jsx(h,{name:"prompt-label",value:n.label,onChange:e=>c(s=>({...s,label:e||""})),placeholder:"Enter prompt description...",className:l.formInput})]}),a.jsxs("div",{className:l.formField,children:[a.jsx("label",{className:l.formLabel,children:"Image Type:"}),a.jsx(h,{name:"prompt-image-type",value:n.image_type,onChange:()=>{},disabled:!0,className:l.formInput})]}),a.jsxs("div",{className:l.formField,children:[a.jsx("label",{className:l.formLabel,children:"Active Status:"}),a.jsxs("div",{className:l.addModelFormCheckbox,children:[a.jsx("input",{type:"checkbox",checked:n.is_active,onChange:e=>c(s=>({...s,is_active:e.target.checked}))}),a.jsx("span",{children:"Active for this image type"})]})]}),a.jsxs("div",{className:l.formField,children:[a.jsx("label",{className:l.formLabel,children:"Metadata Instructions:"}),a.jsx("textarea",{name:"prompt-instructions",value:n.metadata_instructions,onChange:e=>c(s=>({...s,metadata_instructions:e.target.value})),placeholder:"Enter metadata extraction instructions...",className:`${l.formInput} ${l.textarea}`,rows:8})]})]}),a.jsxs("div",{className:l.modalButtons,children:[a.jsx(d,{name:"cancel-add-prompt",variant:"tertiary",onClick:()=>{T(!1),z(null),c({p_code:"",label:"",metadata_instructions:"",image_type:"crisis_map",is_active:!1})},children:"Cancel"}),a.jsx(d,{name:"save-new-prompt",variant:"primary",onClick:Fe,disabled:!n.p_code||!n.label,children:"Create Prompt"})]})]})})}),je&&a.jsx("div",{className:l.modalOverlay,onClick:()=>k(!1),children:a.jsx("div",{className:l.modalContent,onClick:e=>e.stopPropagation(),children:a.jsxs("div",{className:l.modalBody,children:[a.jsxs("h3",{className:l.modalTitle,children:["Edit Schema: ",O?.schema_id]}),a.jsxs("div",{className:l.modalForm,children:[a.jsxs("div",{className:l.formField,children:[a.jsx("label",{className:l.formLabel,children:"Schema ID:"}),a.jsx(h,{name:"schema-id",value:U.schema_id,onChange:e=>f(s=>({...s,schema_id:e||""})),className:l.formInput,disabled:!0})]}),a.jsxs("div",{className:l.formField,children:[a.jsx("label",{className:l.formLabel,children:"Schema Definition (JSON):"}),a.jsx("textarea",{name:"schema-definition",value:JSON.stringify(U.schema,null,2),onChange:e=>{try{const s=JSON.parse(e.target.value);f(o=>({...o,schema:s}))}catch{}},className:`${l.formInput} ${l.textarea}`,rows:20,style:{fontFamily:"monospace"}})]})]}),a.jsxs("div",{className:l.modalButtons,children:[a.jsx(d,{name:"cancel-edit-schema",variant:"tertiary",onClick:Ie,children:"Cancel"}),a.jsx(d,{name:"save-schema",variant:"primary",onClick:$e,children:"Save Changes"})]})]})})})]}):a.jsx(K,{children:a.jsxs("div",{className:"mx-auto max-w-md px-4 sm:px-6 lg:px-8 py-6 sm:py-10",children:[a.jsx("div",{className:"text-center mb-8",children:a.jsx(Je,{level:2,children:"Admin Login"})}),a.jsxs("form",{onSubmit:Re,className:"space-y-6",children:[a.jsxs("div",{children:[a.jsx("label",{htmlFor:"password",className:"block text-sm font-medium text-gray-700 mb-2",children:"Password"}),a.jsx(h,{id:"password",name:"password",type:"password",value:E,onChange:e=>Q(e||""),placeholder:"Enter admin password",required:!0,className:"w-full",disabled:Z})]}),X&&a.jsx("div",{children:a.jsx("p",{className:"text-sm text-ifrcRed font-medium",children:X})}),a.jsx("div",{className:"flex justify-center",children:a.jsx(j,{withInternalPadding:!0,className:"p-2",children:Z?a.jsxs("div",{className:re.loadingContainer,children:[a.jsx(Ge,{className:"text-ifrcRed"}),a.jsx("p",{className:re.loadingText,children:"Logging in..."})]}):a.jsx(d,{name:"login",type:"submit",variant:"primary",size:2,children:"Login"})})})]})]})})}export{Ca as default};