import{j as e}from"./ui-vendor-CJlQk0Tq.js";import{r as s}from"./react-vendor-f6QdChhz.js";import{u as t,s as a,g as r,h as o,i as n,j as i,f as l,bb as m,L as c,B as d,n as u,U as h,I as p,M as x,P as j,aF as f}from"./index-BzXujQ-F.js";import{T as v}from"./textarea-EMfUVvyA.js";import{S as y,a as N,b as g,c as _,d as b}from"./select-DamQt7wx.js";import{C as w,f as S,i as P}from"./calendar-CRH4glvd.js";import{f as k,a as E}from"./format-CnTu1Fon.js";import{a as C}from"./isSameDay-C75k_Qfm.js";function I({open:I,onOpenChange:D}){const{toast:T}=t(),[F,M]=s.useState([]),[A,B]=s.useState([]),[q,Y]=s.useState([]),[H,O]=s.useState(),[$,z]=s.useState(""),[L,U]=s.useState(""),[V,G]=s.useState([]),[J,K]=s.useState(!1),[Q,R]=s.useState({customerName:"",customerEmail:"",customerPhone:"",notes:"",structureInterest:""});s.useEffect(()=>{I&&(W(),X(),Z())},[I]),s.useEffect(()=>{H&&L&&ee()},[H,L,A,q]);const W=async()=>{const{data:e,error:s}=await a.from("appointment_types").select("*").eq("is_active",!0).order("name");s||M(e||[])},X=async()=>{const{data:e,error:s}=await a.from("business_availability").select("*").eq("is_available",!0);s||B(e||[])},Z=async()=>{const{data:e,error:s}=await a.from("blocked_dates").select("*");s||Y(e||[])},ee=()=>{if(!H||!L)return;const e=H.getDay(),s=k(H,"yyyy-MM-dd"),t=A.find(s=>s.day_of_week===e);if(!t)return void G([]);const a=q.find(e=>e.date===s);if(a&&a.all_day)return void G([]);const r=F.find(e=>e.id===L);if(!r)return;const o=[],n=new Date(`2000-01-01T${t.start_time}`),i=new Date(`2000-01-01T${t.end_time}`),l=r.duration_minutes;let m=new Date(n);for(;m<i;){if(new Date(m.getTime()+6e4*l)<=i){const e=k(m,"HH:mm");(!a||!a.start_time||!a.end_time||e<a.start_time||e>=a.end_time)&&o.push(e)}m=new Date(m.getTime()+18e5)}G(o)};return e.jsx(r,{open:I,onOpenChange:D,children:e.jsxs(o,{className:"max-w-2xl max-h-[90vh] overflow-y-auto",children:[e.jsxs(n,{children:[e.jsxs(i,{className:"flex items-center gap-2",children:[e.jsx(l,{className:"h-5 w-5 text-primary"}),"Schedule an Appointment"]}),e.jsx(m,{children:"Book a time to visit our sales lot or discuss your building needs with our team."})]}),e.jsxs("form",{onSubmit:async e=>{if(e.preventDefault(),H&&$&&L){K(!0);try{const e=F.find(e=>e.id===L),{error:s}=await a.from("appointments").insert({appointment_type_id:L,appointment_date:k(H,"yyyy-MM-dd"),appointment_time:$,duration_minutes:(null==e?void 0:e.duration_minutes)||60,customer_name:Q.customerName,customer_email:Q.customerEmail,customer_phone:Q.customerPhone,customer_address:"",notes:Q.notes,structure_interest:Q.structureInterest,status:"pending-review"});if(s)throw s;await a.functions.invoke("send-appointment-notification",{body:{appointmentType:null==e?void 0:e.name,date:k(H,"PPP"),time:$,customerName:Q.customerName,customerEmail:Q.customerEmail,customerPhone:Q.customerPhone,notes:Q.notes,structureInterest:Q.structureInterest}}),T({title:"Appointment Booked!",description:"Your appointment has been scheduled. You'll receive a confirmation email shortly."}),R({customerName:"",customerEmail:"",customerPhone:"",notes:"",structureInterest:""}),O(void 0),z(""),U(""),D(!1)}catch(s){T({title:"Booking Error",description:"There was an error booking your appointment. Please try again.",variant:"destructive"})}finally{K(!1)}}},className:"space-y-6",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{htmlFor:"appointmentType",children:"Appointment Type"}),e.jsxs(y,{value:L,onValueChange:U,children:[e.jsx(N,{children:e.jsx(g,{placeholder:"Select appointment type"})}),e.jsx(_,{children:F.map(s=>e.jsx(b,{value:s.id,children:e.jsxs("div",{className:"flex flex-col",children:[e.jsx("span",{className:"font-medium",children:s.name}),e.jsxs("span",{className:"text-sm text-muted-foreground",children:[s.duration_minutes," minutes"]})]})},s.id))})]})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-6",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{children:"Select Date"}),e.jsx(w,{mode:"single",selected:H,onSelect:O,disabled:e=>{const s=E(new Date),t=C(s,90);if(S(e,s)||P(e,t))return!0;const a=e.getDay();if(!A.some(e=>e.day_of_week===a))return!0;const r=k(e,"yyyy-MM-dd");return q.some(e=>e.date===r&&e.all_day)},className:"rounded-md border"})]}),e.jsx("div",{className:"space-y-4",children:e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{children:"Available Times"}),H&&L?e.jsx("div",{className:"grid grid-cols-2 gap-2 max-h-60 overflow-y-auto",children:V.length>0?V.map(s=>e.jsxs(d,{type:"button",variant:$===s?"default":"outline",size:"sm",onClick:()=>z(s),className:"justify-start",children:[e.jsx(u,{className:"h-3 w-3 mr-1"}),s]},s)):e.jsx("p",{className:"text-sm text-muted-foreground",children:"No available times for this date"})}):e.jsx("p",{className:"text-sm text-muted-foreground",children:"Select date and appointment type to see available times"})]})})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsx("h3",{className:"text-lg font-semibold",children:"Your Information"}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{htmlFor:"customerName",children:"Full Name *"}),e.jsxs("div",{className:"relative",children:[e.jsx(h,{className:"absolute left-3 top-3 h-4 w-4 text-muted-foreground"}),e.jsx(p,{id:"customerName",placeholder:"Your full name",value:Q.customerName,onChange:e=>R(s=>({...s,customerName:e.target.value})),className:"pl-10",required:!0})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{htmlFor:"customerEmail",children:"Email *"}),e.jsxs("div",{className:"relative",children:[e.jsx(x,{className:"absolute left-3 top-3 h-4 w-4 text-muted-foreground"}),e.jsx(p,{id:"customerEmail",type:"email",placeholder:"your.email@example.com",value:Q.customerEmail,onChange:e=>R(s=>({...s,customerEmail:e.target.value})),className:"pl-10",required:!0})]})]})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{htmlFor:"customerPhone",children:"Phone Number"}),e.jsxs("div",{className:"relative",children:[e.jsx(j,{className:"absolute left-3 top-3 h-4 w-4 text-muted-foreground"}),e.jsx(p,{id:"customerPhone",placeholder:"(555) 123-4567",value:Q.customerPhone,onChange:e=>R(s=>({...s,customerPhone:e.target.value})),className:"pl-10"})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{htmlFor:"structureInterest",children:"Structure Interest"}),e.jsx(p,{id:"structureInterest",placeholder:"Shed, garage, etc.",value:Q.structureInterest,onChange:e=>R(s=>({...s,structureInterest:e.target.value}))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(c,{htmlFor:"notes",children:"Additional Notes"}),e.jsxs("div",{className:"relative",children:[e.jsx(f,{className:"absolute left-3 top-3 h-4 w-4 text-muted-foreground"}),e.jsx(v,{id:"notes",placeholder:"Tell us more about what you're looking for...",value:Q.notes,onChange:e=>R(s=>({...s,notes:e.target.value})),className:"pl-10 min-h-20"})]})]})]}),e.jsxs("div",{className:"flex gap-3 pt-4",children:[e.jsx(d,{type:"button",variant:"outline",onClick:()=>D(!1),children:"Cancel"}),e.jsx(d,{type:"submit",disabled:!H||!$||!L||!Q.customerName||!Q.customerEmail||J,className:"flex-1",children:J?"Booking...":"Book Appointment"})]})]})]})})}export{I as C};