import{c as e}from"./security-CTzQb9Ko.js";import{$i as t,$r as n,Bn as r,Or as i,Qi as a}from"./bundle-mjs-lcbR1pPJ.js";import{h as o}from"./dist-C0UdmnhX.js";import{n as s}from"./dist-7Rqo10GL.js";import{t as c}from"./label-D8JZSQwG.js";import{t as l}from"./input-D6oSyFvM.js";import{a as u,i as d,n as f,o as p,r as m,t as h}from"./dialog-BOTNIFXD.js";import{n as g,t as _}from"./alert-CoQSTVxM.js";import{Q as v,X as y,Z as b}from"./index-DktFsC-h.js";var x=e(t(),1),S=e(a(),1);function C({open:e,onOpenChange:t,report:a,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&&a.sql_parameters){let e={};a.sql_parameters.forEach(t=>{e[t.name]=t.default?.toString()||``}),D(e)}},[e,a.sql_parameters]);let M=async()=>{k(!0),j(``);try{let e={},n=[];if(a.sql_parameters?.forEach(t=>{let r=E[t.name];if(t.required&&(!r||r.trim()===``)){n.push(t.name);return}if(!(!r||r.trim()===``))if(t.type===`number`){let n=parseFloat(r);if(isNaN(n))throw Error(`Invalid number value for parameter "${t.name}"`);e[t.name]=n}else t.type===`boolean`?e[t.name]=r===`true`||r===`1`:e[t.name]=r}),n.length>0){j(`Please provide required parameters: ${n.join(`, `)}`),k(!1);return}let r=await v.createView(a.id,{parameter_defaults:e,view_type:w});r.success?(s.success(`View "${r.view_name}" has been created successfully.`),C?.(),t(!1)):j(r.error||`Failed to create view`)}catch(e){let t=e.response?.data?.error||e.message||`Failed to create view`;j(t),s.error(t)}finally{k(!1)}},N=a.sql_parameters&&a.sql_parameters.length>0;return(0,S.jsx)(h,{open:e,onOpenChange:t,children:(0,S.jsxs)(f,{className:`max-w-2xl`,children:[(0,S.jsxs)(u,{children:[(0,S.jsxs)(p,{className:`flex items-center gap-2`,children:[(0,S.jsx)(i,{className:`h-5 w-5`}),`Create Database View`]}),(0,S.jsx)(m,{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)(_,{children:[(0,S.jsx)(r,{className:`h-4 w-4`}),(0,S.jsx)(g,{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)(c,{children:`View Type`}),(0,S.jsxs)(y,{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)(b,{value:`regular`,id:`regular`}),(0,S.jsxs)(`div`,{className:`flex-1`,children:[(0,S.jsx)(c,{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)(b,{value:`materialized`,id:`materialized`}),(0,S.jsxs)(`div`,{className:`flex-1`,children:[(0,S.jsx)(c,{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)(c,{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:a.sql_parameters.map(e=>(0,S.jsxs)(`div`,{className:`space-y-2`,children:[(0,S.jsxs)(c,{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)(l,{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)(_,{variant:`destructive`,children:[(0,S.jsx)(n,{className:`h-4 w-4`}),(0,S.jsx)(g,{children:A})]})]}),(0,S.jsxs)(d,{children:[(0,S.jsx)(o,{variant:`outline`,onClick:()=>t(!1),children:`Cancel`}),(0,S.jsx)(o,{onClick:M,disabled:O,children:O?`Creating...`:`Create View`})]})]})})}export{C as t};