import{b as ee,r as p,u as B,c as v,j as e}from"./query-Bw69rE6b.js";import{u as te,L as W}from"./react-CQKYBN-P.js";import{d as H,i as se,u as F,j as ae,D as ne,B as y,W as ie,S as T,s as E,e as C,a as l,l as re}from"./index-CoUu6FiM.js";import{P as oe}from"./PageHeader-qtVByY5c.js";import{C as le,a as ce,b as de}from"./Card-nuKO-lKR.js";import{E as me}from"./EmptyState-CgO0CCgi.js";import{G as ue}from"./GenerateScopeDialog-DxVi8XyM.js";import{M as A}from"./Modal-t7k2CeNu.js";import{S as pe}from"./StatusPill-DBEWzsQ3.js";import{I}from"./InlineEditText-BpIisHVz.js";import"./code-xml-CnSqwYLd.js";import"./Badge-DwoQNRW3.js";import"./pencil-ByuKsEO5.js";/**
 * @license lucide-react v0.577.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const xe=[["rect",{width:"18",height:"18",x:"3",y:"3",rx:"2",key:"afitv7"}],["path",{d:"M12 3v18",key:"108xh3"}]],he=H("columns-2",xe);/**
 * @license lucide-react v0.577.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const fe=[["path",{d:"M21 5H3",key:"1fi0y6"}],["path",{d:"M15 12H3",key:"6jk70r"}],["path",{d:"M17 19H3",key:"z6ezky"}]],K=H("text-align-start",fe);function Ee(){const{scope:s}=se(),a=s?.schema??"",n=s?.table??"",x=F(t=>t.rememberOpenedTable),j=F(t=>t.rememberOpenedScope),k=ee(),o=ae(),h=te(),[U,c]=p.useState(!1),[w,J]=p.useState(null),[$,X]=p.useState(null),[r,f]=p.useState(null);p.useEffect(()=>{s&&a&&n&&(x(a,n),j({profile:s.profile,database:s.database,catalog:s.catalog,schema:a,table:n}))},[s,a,n,x,j]);const d=B({queryKey:["live-snapshot",s?.profile??"",s?.database??"",s?.catalog??"",a,n],queryFn:()=>l.liveSnapshot(s,a,n),enabled:!!s&&!!a&&!!n}),m=B({queryKey:["live-columns",s?.profile??"",s?.database??"",s?.catalog??"",a,n],queryFn:()=>l.liveColumns(s,a,n),enabled:!!s&&!!a&&!!n}),b=d.data?.columns?.length??m.data?.count??0,R=d.data?.columns?.filter(t=>(t.comment||"").trim().length>0).length??0,V=d.data?.table_comment??"",q=d.data?.pending_description??null,z=d.data?.pending_column_descriptions??{},L=d.data?.pending_run_id??null;async function Y(t){s&&(await l.setTableComment(s,a,n,t),k.invalidateQueries({queryKey:["live-snapshot"]}),o.push({title:"Description saved",tone:"success",duration:2e3}))}async function Z(t,i){s&&(await l.setColumnComment(s,a,n,t,i),k.invalidateQueries({queryKey:["live-snapshot"]}),o.push({title:"Comment saved",description:`${a}.${n}.${t}`,tone:"success",duration:2e3}))}const D=v({mutationFn:()=>l.generateTableDescription(s,a,n),onSuccess:t=>{k.invalidateQueries({queryKey:["live-snapshot"]});const i=t.alternatives_count??1;o.push({title:t.run_id?`Queued for review (Run #${t.run_id})`:"Description queued for review",description:i>1?`${i} alternatives generated (${t.verbosity}). Pick one and approve from the Pending page.`:"Approve from the Pending page to write it to the live database.",tone:"success",duration:3200})},onError:t=>o.push({title:"Generation failed",description:t.message,tone:"error"})}),N=v({mutationFn:t=>l.generateColumnDescription(s,a,n,t),onSuccess:(t,i)=>{k.invalidateQueries({queryKey:["live-snapshot"]});const u=t.alternatives_count??1;o.push({title:t.run_id?`Column queued for review (Run #${t.run_id})`:"Column description queued for review",description:u>1?`${a}.${n}.${i} — ${u} alternatives (${t.verbosity}); pick one in /pending.`:`${a}.${n}.${i} — approve from /pending.`,tone:"success",duration:3e3})},onError:t=>o.push({title:"Generation failed",description:t.message,tone:"error"})}),G=v({mutationFn:async()=>{const t=await l.preflightRun({scope:{[a]:[n]},db_profile:s?.profile,database:s?.database,catalog:s?.catalog});return t.blocked_assets.length>0?(f(t.blocked_assets),null):l.submitRun({scope:{[a]:[n]},apply:!1,missing_only:!1,db_profile:s?.profile,database:s?.database,catalog:s?.catalog,doc_profiles:w??void 0,code_profiles:$??void 0})},onSuccess:t=>{t!==null&&(c(!1),o.push({title:"Bulk run queued for review",description:`Streaming every column of ${a}.${n}; results land on the Pending page.`,tone:"info",duration:2600}),h(`/runs/new-${t.job_id}`))},onError:t=>{c(!1),o.push({title:"Could not start generation",description:t.message,tone:"error"})}}),_=v({mutationFn:t=>l.submitRun({scope:{[a]:[n]},apply:!1,missing_only:!1,db_profile:s?.profile,database:s?.database,catalog:s?.catalog,doc_profiles:w??void 0,code_profiles:$??void 0,cache_override_assets:t}),onSuccess:t=>{f(null),c(!1),o.push({title:"Bulk run queued for review",description:`Streaming every column of ${a}.${n} using cached schema; results land on the Pending page.`,tone:"info",duration:2800}),h(`/runs/new-${t.job_id}`)},onError:t=>{f(null),c(!1),o.push({title:"Could not start generation",description:t.message,tone:"error"})}}),[P,S]=p.useState(null),O=v({mutationFn:async()=>{if(!s||!n)throw new Error("Pick a table from the sidebar first.");const t=s.database||s.catalog||"";return{artifacts:(await re({profile:s.profile,database:t,schema:a,table:n})).artifacts,database:t}},onSuccess:t=>{if(!s||!n)return;const{artifacts:i,database:u}=t;if(i.length===0){const g=[s.profile,u,a,n,""].map(encodeURIComponent).join("|");h(`/lineage?seed=${encodeURIComponent(g)}`);return}if(i.length===1){h(`/lineage?artifact=${i[0].id}`);return}S(i)},onError:t=>{o.push({title:"Could not open lineage",description:t.message,tone:"error"})}});return!s||!a||!n?e.jsx(me,{icon:ne,title:"Pick a table from the sidebar",description:"Expand a schema to see its tables."}):e.jsxs(e.Fragment,{children:[e.jsx(oe,{title:n,breadcrumbs:[{label:"Browse",to:"/"},{label:s.profile,to:`/${s.kind==="catalog"?"cat":"db"}/${encodeURIComponent(s.profile)}`},{label:s.database??s.catalog??"",to:E(s)},{label:a,to:E(s,a)},{label:n}],description:e.jsxs("div",{className:"space-y-2",children:[e.jsx(I,{value:V,onSave:Y,multiline:!0,italicEmpty:!0,emptyLabel:"No table description yet — click to add one or use Generate."}),q?e.jsx(Q,{text:q,runId:L,label:"Pending review"}):null]}),actions:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(y,{variant:"secondary",size:"md",leadingIcon:e.jsx(ie,{size:14}),loading:O.isPending,onClick:()=>O.mutate(),title:"Render this table's lineage and open it in the canvas",children:"Open lineage"}),e.jsx(y,{variant:"primary",size:"md",leadingIcon:e.jsx(T,{size:14}),loading:D.isPending||G.isPending,onClick:()=>c(!0),children:"Generate description"})]})}),e.jsxs("div",{className:"grid gap-4 md:grid-cols-3",children:[e.jsx(M,{label:"Columns",value:String(b),icon:he}),e.jsx(M,{label:"With comments",value:b?`${R}/${b}`:"—",icon:K}),e.jsx(M,{label:"Coverage",value:b?`${Math.round(R/b*100)}%`:"—",icon:K})]}),e.jsxs(le,{className:"mt-6",children:[e.jsx(ce,{title:"Columns"}),e.jsx(de,{className:"p-0 overflow-x-auto",children:m.isLoading?e.jsx("div",{className:"px-5 py-4 space-y-2",children:Array.from({length:6}).map((t,i)=>e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(C,{className:"h-3 w-1/6"}),e.jsx(C,{className:"h-3 w-1/12"}),e.jsx(C,{className:"h-3 w-1/12"}),e.jsx(C,{className:"h-3 flex-1"})]},i))}):m.error?e.jsx("div",{className:"px-5 py-6 text-sm text-critical",children:m.error.message}):m.data&&m.data.columns.length>0?e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"border-b border-border bg-surface-subtle/60 text-[10.5px] uppercase tracking-wider text-ink-dim",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-5 py-2 text-left font-semibold",children:"Name"}),e.jsx("th",{className:"px-5 py-2 text-left font-semibold",children:"Type"}),e.jsx("th",{className:"px-5 py-2 text-left font-semibold",children:"Nullable"}),e.jsx("th",{className:"px-5 py-2 text-left font-semibold",children:"Comment"}),e.jsx("th",{className:"px-2 py-2 text-right font-semibold w-20",children:"Generate"})]})}),e.jsx("tbody",{className:"divide-y divide-border",children:m.data.columns.map(t=>{const i=d.data?.columns.find(g=>g.name===t.name),u=N.isPending&&N.variables===t.name;return e.jsxs("tr",{className:"align-top hover:bg-surface-subtle/40",children:[e.jsx("td",{className:"px-5 py-2 font-mono text-xs text-ink",children:t.name}),e.jsx("td",{className:"px-5 py-2 font-mono text-xs text-ink-muted",children:t.dtype}),e.jsx("td",{className:"px-5 py-2",children:e.jsx(pe,{tone:t.nullable?"neutral":"accent",children:t.nullable?"nullable":"required"})}),e.jsx("td",{className:"px-5 py-2 text-ink-muted",children:e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(I,{value:i?.comment??"",onSave:g=>Z(t.name,g),multiline:!0,italicEmpty:!0,emptyLabel:"no comment — click to add"}),z[t.name]?e.jsx(Q,{text:z[t.name],runId:L,label:"Pending",compact:!0}):null]})}),e.jsx("td",{className:"px-2 py-2 text-right",children:e.jsx(y,{variant:"ghost",size:"sm",leadingIcon:e.jsx(T,{size:11}),loading:u,disabled:N.isPending,onClick:()=>N.mutate(t.name),title:`Generate description for ${t.name}`,children:"Gen"})})]},t.name)})})]}):e.jsx("div",{className:"px-5 py-6 text-sm text-ink-dim",children:"This table has no introspectable columns."})})]}),e.jsx("div",{className:"mt-4",children:e.jsx(W,{to:E(s,a),className:"text-xs text-ink-dim hover:text-ink",children:"← Back to schema"})}),e.jsx(ue,{open:U,onClose:()=>c(!1),docProfiles:w,onDocProfilesChange:J,codeProfiles:$,onCodeProfilesChange:X,title:`Generate description for ${a}.${n}`,description:"Pick the scope. Just-the-table writes only the table's own COMMENT (one fast LLM call). Bulk run also generates a description for every column.",singleOption:{label:"Just this table",description:"Writes only the table's COMMENT. One LLM call, columns untouched.",onClick:()=>{c(!1),D.mutate()}},bulkOption:{label:"Whole table — every column too (bulk run)",description:"Spawns analyze.run for the full table; each column gets its own generated description, recorded in history.",onClick:()=>{c(!1),G.mutate()}}}),e.jsx(A,{open:r!==null,onClose:()=>f(null),size:"md",title:e.jsx("span",{children:"Live database can't read this table"}),description:r&&r.length===1?`AMX couldn't reach ${r[0].schema}.${r[0].table} on the live database. The catalog still has its column list from the last /search sync — use that instead?`:`AMX couldn't reach ${r?.length??0} table(s) on the live database. The catalog still has their column lists from the last /search sync — use that instead?`,children:e.jsxs("div",{className:"space-y-3 text-[13px] text-ink",children:[r&&r.length>1?e.jsxs("ul",{className:"max-h-[180px] divide-y divide-surface-border overflow-y-auto rounded-md border border-surface-border text-[12px] font-mono",children:[r.slice(0,8).map(t=>e.jsxs("li",{className:"px-3 py-1.5 text-ink-muted",children:[t.schema,".",t.table]},`${t.schema}.${t.table}`)),r.length>8?e.jsxs("li",{className:"px-3 py-1.5 text-[11px] italic text-ink-dim",children:["+",r.length-8," more"]}):null]}):null,e.jsx("p",{className:"text-[12px] text-ink-muted",children:"Cached schema gives the LLM column names, dtypes, and existing comments — no live samples, PK/FK, or row counts. Approve the descriptions from the Pending page as usual."}),e.jsxs("div",{className:"flex flex-col gap-2 sm:flex-row sm:justify-end",children:[e.jsx(y,{variant:"ghost",size:"md",onClick:()=>f(null),disabled:_.isPending,children:"Cancel"}),e.jsx(y,{variant:"primary",size:"md",loading:_.isPending,onClick:()=>{if(!r)return;const t=r.map(i=>`${i.schema}.${i.table}`);_.mutate(t)},children:"Use cached schema"})]})]})}),e.jsx(A,{open:P!==null,onClose:()=>S(null),size:"md",title:e.jsx("span",{children:"Open lineage"}),description:`${P?.length??0} saved canvases include this table. Pick one to open.`,children:e.jsx("ul",{className:"max-h-[420px] divide-y divide-surface-border overflow-y-auto rounded-md border border-surface-border",children:(P??[]).map(t=>e.jsx("li",{children:e.jsxs("button",{type:"button",onClick:()=>{S(null),h(`/lineage?artifact=${t.id}`)},className:"block w-full px-3 py-2 text-left transition hover:bg-surface-raised",children:[e.jsx("div",{className:"text-[13px] font-medium text-ink",children:t.name}),e.jsxs("div",{className:"mt-0.5 text-[11px] text-fg-muted",children:[t.node_count," nodes · ",t.edge_count," edges ·"," ",t.db_profile||"no profile"]})]})},t.id))})})]})}function M({label:s,value:a,icon:n}){return e.jsxs("div",{className:"rounded-xl border border-border bg-surface-raised p-4 shadow-xs",children:[e.jsxs("div",{className:"flex items-center justify-between text-[10.5px] uppercase tracking-wider text-ink-dim",children:[e.jsx("span",{children:s}),e.jsx(n,{size:14})]}),e.jsx("div",{className:"mt-1.5 font-mono text-xl text-ink tabular-nums",children:a})]})}function Q({text:s,runId:a,label:n,compact:x}){const j=a!=null?`/runs/${a}`:"/pending";return e.jsxs("div",{className:x?"rounded-md border border-amber-300/60 bg-amber-50/60 px-2 py-1.5 text-[12px] text-amber-900 dark:border-amber-500/40 dark:bg-amber-900/20 dark:text-amber-100":"rounded-lg border border-amber-300/60 bg-amber-50/60 p-3 text-sm text-amber-900 dark:border-amber-500/40 dark:bg-amber-900/20 dark:text-amber-100",children:[e.jsxs("div",{className:"flex flex-wrap items-center gap-1.5 text-[10.5px] font-semibold uppercase tracking-wider",children:[e.jsx(T,{size:11}),e.jsx("span",{children:n}),a!=null?e.jsxs("span",{className:"font-mono normal-case opacity-80",children:["· Run #",a]}):null,e.jsx(W,{to:j,className:"ml-auto rounded-sm px-1.5 py-0.5 text-[10.5px] font-semibold uppercase tracking-wider underline-offset-2 hover:underline",children:"Review →"})]}),e.jsx("div",{className:x?"mt-1 italic":"mt-1.5 italic leading-snug",children:s})]})}export{Ee as default};
