import{r as e}from"./rolldown-runtime-QTnfLwEv.js";import{n as t,t as n}from"./jsx-runtime-CIxEorsV.js";import{D as r,G as i,N as a,Q as o,Si as s,St as c,b as l,it as u,k as d,m as f,ma as p,o as m,u as h,w as g,wt as _,za as v}from"./bundle-mjs-Du8wrU3Z.js";import{n as y}from"./dist-Co--cbpv.js";import{t as b}from"./reportService-jkXBIb_o.js";var x=e(t(),1),S=e(n(),1);function C({open:e,onOpenChange:t,report:n,onSuccess:C}){let[w,T]=(0,x.useState)(`regular`),[E,D]=(0,x.useState)({}),[O,k]=(0,x.useState)(!1),[A,j]=(0,x.useState)(``);(0,x.useEffect)(()=>{if(e&&n.sql_parameters){let e={};n.sql_parameters.forEach(t=>{e[t.name]=t.default?.toString()||``}),D(e)}},[e,n.sql_parameters]);let M=async()=>{k(!0),j(``);try{let e={},r=[];if(n.sql_parameters?.forEach(t=>{let n=E[t.name];if(t.required&&(!n||n.trim()===``)){r.push(t.name);return}if(!(!n||n.trim()===``))if(t.type===`number`){let r=parseFloat(n);if(isNaN(r))throw Error(`Invalid number value for parameter "${t.name}"`);e[t.name]=r}else t.type===`boolean`?e[t.name]=n===`true`||n===`1`:e[t.name]=n}),r.length>0){j(`Please provide required parameters: ${r.join(`, `)}`),k(!1);return}let i=await b.createView(n.id,{parameter_defaults:e,view_type:w});i.success?(y.success(`View "${i.view_name}" has been created successfully.`),C?.(),t(!1)):j(i.error||`Failed to create view`)}catch(e){let t=e.response?.data?.error||e.message||`Failed to create view`;j(t),y.error(t)}finally{k(!1)}},N=n.sql_parameters&&n.sql_parameters.length>0;return(0,S.jsx)(c,{open:e,onOpenChange:t,children:(0,S.jsxs)(g,{className:`max-w-2xl`,children:[(0,S.jsxs)(a,{children:[(0,S.jsxs)(r,{className:`flex items-center gap-2`,children:[(0,S.jsx)(p,{className:`h-5 w-5`}),`Create Database View`]}),(0,S.jsx)(h,{children:`Create a PostgreSQL view from this report's SQL query. The view will be stored in the database and can be used by other tools.`})]}),(0,S.jsxs)(`div`,{className:`space-y-6 py-4`,children:[(0,S.jsxs)(u,{children:[(0,S.jsx)(s,{className:`h-4 w-4`}),(0,S.jsx)(_,{children:`The database view will use the exact same SQL query and field structure as this report. Once created, it will auto-update when you modify the report SQL.`})]}),(0,S.jsxs)(`div`,{className:`space-y-3`,children:[(0,S.jsx)(i,{children:`View Type`}),(0,S.jsxs)(l,{value:w,onValueChange:e=>T(e),children:[(0,S.jsxs)(`div`,{className:`flex items-start space-x-3 space-y-0`,children:[(0,S.jsx)(m,{value:`regular`,id:`regular`}),(0,S.jsxs)(`div`,{className:`flex-1`,children:[(0,S.jsx)(i,{htmlFor:`regular`,className:`font-medium cursor-pointer`,children:`Regular View`}),(0,S.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Always shows current data from base tables. Recommended for most cases.`})]})]}),(0,S.jsxs)(`div`,{className:`flex items-start space-x-3 space-y-0`,children:[(0,S.jsx)(m,{value:`materialized`,id:`materialized`}),(0,S.jsxs)(`div`,{className:`flex-1`,children:[(0,S.jsx)(i,{htmlFor:`materialized`,className:`font-medium cursor-pointer`,children:`Materialized View`}),(0,S.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Snapshot of data at creation time. Faster queries but requires manual refresh.`})]})]})]})]}),N&&(0,S.jsxs)(`div`,{className:`space-y-4`,children:[(0,S.jsxs)(`div`,{children:[(0,S.jsx)(i,{className:`text-base`,children:`Parameter Default Values`}),(0,S.jsx)(`p`,{className:`text-sm text-muted-foreground mt-1`,children:`Provide default values for query parameters. These will be used when querying the view.`})]}),(0,S.jsx)(`div`,{className:`space-y-3`,children:n.sql_parameters.map(e=>(0,S.jsxs)(`div`,{className:`space-y-2`,children:[(0,S.jsxs)(i,{htmlFor:e.name,className:`text-sm font-medium`,children:[e.name,e.required&&(0,S.jsx)(`span`,{className:`text-destructive ml-1`,children:`*`})]}),(0,S.jsx)(o,{id:e.name,type:e.type===`date`?`date`:e.type===`number`?`number`:`text`,value:E[e.name]||``,onChange:t=>D({...E,[e.name]:t.target.value}),placeholder:`Enter ${e.name}`,required:e.required})]},e.name))})]}),A&&(0,S.jsxs)(u,{variant:`destructive`,children:[(0,S.jsx)(v,{className:`h-4 w-4`}),(0,S.jsx)(_,{children:A})]})]}),(0,S.jsxs)(f,{children:[(0,S.jsx)(d,{variant:`outline`,onClick:()=>t(!1),children:`Cancel`}),(0,S.jsx)(d,{onClick:M,disabled:O,children:O?`Creating...`:`Create View`})]})]})})}export{C as t};