var e=Object.defineProperty,t=Object.defineProperties,r=Object.getOwnPropertyDescriptors,o=Object.getOwnPropertySymbols,n=Object.prototype.hasOwnProperty,i=Object.prototype.propertyIsEnumerable,s=(t,r,o)=>r in t?e(t,r,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[r]=o,c=(e,t)=>{for(var r in t||(t={}))n.call(t,r)&&s(e,r,t[r]);if(o)for(var r of o(t))i.call(t,r)&&s(e,r,t[r]);return e},d=(e,o)=>t(e,r(o)),l=(e,t,r)=>new Promise((o,n)=>{var i=e=>{try{c(r.next(e))}catch(t){n(t)}},s=e=>{try{c(r.throw(e))}catch(t){n(t)}},c=e=>e.done?o(e.value):Promise.resolve(e.value).then(i,s);c((r=r.apply(e,t)).next())});import{s as a,g as u}from"./index-BCSgDlp8.js";const m=(e,t)=>l(void 0,null,function*(){const r=new Date,o=new Date(r.getTime()),{data:n,error:i}=yield a.from("projects").insert(d(c({},e),{user_id:t.id,timer_start_time:r.toISOString(),timer_expires_time:o.toISOString(),is_unlocked:!1})).select().single();if(i)throw i;const s=(yield u()).map(e=>{const{sections:t,status:r}=(e.id,{sections:[],status:"pending"});return a.from("documents").insert({project_id:n.id,type:e.id,version:1,content:{sections:t},status:r}).select().single()}),l=yield Promise.all(s);for(const e of l)if(e.data&&"marketing_plan"===e.data.type){console.log(`Creating document sections for marketing plan: ${e.data.id}`);const t=[{title:"Executive Summary",order_index:0},{title:"Market Analysis",order_index:1},{title:"Target Market Segmentation",order_index:2},{title:"Unique Value Proposition",order_index:3},{title:"Marketing Channels & Tactics",order_index:4},{title:"Budget Allocation",order_index:5},{title:"Implementation Timeline",order_index:6},{title:"Success Metrics",order_index:7}];for(const r of t)try{const{error:t}=yield a.from("document_sections").insert({document_id:e.data.id,order_index:r.order_index,title:r.title,content:null,status:"pending",created_at:(new Date).toISOString(),updated_at:(new Date).toISOString()});t&&console.error(`Error creating section ${r.title}:`,t)}catch(m){console.error(`Exception creating section ${r.title}:`,m)}}return n});function f(){return l(this,null,function*(){const{data:e,error:t}=yield a.from("projects").select("*").order("created_at",{ascending:!1});if(t)throw t;return e})}function p(e){return l(this,null,function*(){console.log(`[getProject] Called for project ID: ${e}`),yield h(e);const{data:t,error:r}=yield a.from("projects").select("\n      *,\n      documents (*)\n    ").eq("id",e).single();if(r)throw console.error(`[getProject] Error fetching project ${e}:`,r),r;if(!t)throw console.error(`[getProject] Project ${e} not found after ensuring timer.`),new Error("Project not found after ensuring timer.");console.log(`[getProject] Fetched project data for ${e}:`,t);const o=t.documents.map(e=>e.type);console.log(`[getProject] Existing document types for project ${e}:`,o);const n=yield u();console.log(`[getProject] Master list of document types (from getLatestDocumentTypes) for project ${e}:`,n.map(e=>e.id));const i=n.filter(t=>{const r=!o.includes(t.id);return r&&console.log(`[getProject] Document type '${t.id}' (name: '${t.name}') identified as MISSING for project ${e}.`),r});if(console.log(`[getProject] All missing document types identified for project ${e}:`,i.map(e=>e.id)),i.length>0){console.log(`[getProject] Adding ${i.length} missing document types to project ${e}. Details:`,i);const r=i.map(r=>{const{sections:o,status:n}=(r.id,{sections:[],status:"pending"});return console.log(`[getProject] Preparing to insert document type '${r.id}' with status '${n}' for project ${e}.`),a.from("documents").insert({project_id:t.id,type:r.id,version:1,content:{sections:o},status:n}).select().single()});(yield Promise.all(r.map((t,r)=>l(this,null,function*(){try{const o=yield t;return o.error?console.error(`[getProject] Error inserting document type '${i[r].id}' for project ${e}:`,o.error):console.log(`[getProject] Successfully inserted document type '${i[r].id}' for project ${e}. Result:`,o.data),o}catch(o){return console.error(`[getProject] Exception during insert for document type '${i[r].id}' for project ${e}:`,o),{error:o,data:null}}})))).forEach(e=>{e.data&&t.documents.push(e.data)})}else console.log(`[getProject] No missing document types found for project ${e}.`);return console.log(`[getProject] Final project data with documents for ${e} before return:`,t),t})}function g(e){return l(this,null,function*(){const{data:t,error:r}=yield a.from("projects").select("*").eq("id",e).single();if(r)throw r;return t})}function _(e,t){return l(this,null,function*(){const{data:r,error:o}=yield a.from("documents").update(d(c(c({},t.progress?{progress:t.progress}:{}),Object.fromEntries(Object.entries(t).filter(([e])=>"progress"!==e))),{updated_at:(new Date).toISOString()})).eq("id",e).select().single();if(o)throw o;return"completed"===t.status&&(yield function(e){return l(this,null,function*(){const{data:t,error:r}=yield a.from("documents").select("status").eq("project_id",e);if(r)throw r;const o=null==t?void 0:t.every(e=>"completed"===e.status),{error:n}=yield a.from("projects").update({status:o?"completed":"draft"}).eq("id",e);if(n)throw n})}(r.project_id)),r})}function j(e){return l(this,null,function*(){var t;const{data:r,error:o}=yield a.from("documents").select("\n      *,\n      document_sections (*),\n      project:project_id (\n        *,\n        documents (*)\n      )\n    ").eq("id",e).single();if(o)throw o;if(r&&r.project){const e=r.project;if(!!e.timer_expires_time&&new Date>new Date(e.timer_expires_time)&&!e.is_unlocked&&"marketing_plan"===r.type){const e=null==(t=r.document_sections)?void 0:t.find(e=>"Executive Summary"===e.title);r.document_sections=e?[e]:[]}}return r})}function y(e){return l(this,null,function*(){console.log(`Fetching sections for document ID: ${e}`);const{data:t,error:r}=yield a.from("document_sections").select("id, title, content, order_index").eq("document_id",e).order("order_index",{ascending:!0});if(r)throw console.error("Error fetching document sections:",r),r;return t?(console.log(`Found ${t.length} sections.`),t):[]})}const h=e=>l(void 0,null,function*(){const{data:t,error:r}=yield a.from("projects").select("timer_start_time, timer_expires_time, is_unlocked, created_at").eq("id",e).single();if(r)throw console.error("Error fetching project for timer check:",r),r;if(t){if(!t.timer_start_time||!t.timer_expires_time){const r=t.timer_start_time?new Date(t.timer_start_time):new Date(t.created_at||Date.now()),o=new Date((t.created_at?new Date(t.created_at):new Date(Date.now())).getTime()+36e5),n={timer_start_time:r.toISOString(),timer_expires_time:o.toISOString()};null!==t.is_unlocked&&void 0!==t.is_unlocked||(n.is_unlocked=!1);const{error:i}=yield a.from("projects").update(n).eq("id",e);if(i)throw console.error("Error updating project with timer fields:",i),i;console.log(`Project ${e} updated with timer fields.`)}}else console.error("Project not found for timer check:",e)});export{p as a,f as b,m as c,j as d,g as e,y as g,_ as u};