1import{g as w,r as q,j as e,L as N}from"./react-vendor-C-_weJGF.js";import{b as C,u,c as b}from"./query-DA-B5yju.js";import{g as M,s as d,B as v,a as l}from"./index-sHdJRwRo.js";import{A as h}from"./AdminLayout-83yQHb0s.js";import{I as S}from"./input-DDtkh_8z.js";import{T as k}from"./textarea-COvO_KSb.js";import{L,a as A}from"./leads-DFFauHwb.js";import{q as D,al as E}from"./icons-BZJMmL1V.js";import{f as o}from"./format-Cpjm9erV.js";import"./radix-CMA3xQgM.js";import"./supabase-BppBjPB2.js";import"./motion-CfmjrLVm.js";import"./logo-BWhOaj1e.js";function G(){const{id:r}=w(),n=C(),{user:c}=M(),[m,p]=q.useState(""),{data:t,isLoading:_}=u({queryKey:["crm-lead",r],enabled:!!r,queryFn:async()=>{const{data:s,error:a}=await d.from("leads").select("*").eq("id",r).maybeSingle();if(a)throw a;return s}}),{data:g=[]}=u({queryKey:["crm-lead-activities",r],enabled:!!r,queryFn:async()=>{const{data:s,error:a}=await d.from("lead_activities").select("*").eq("lead_id",r).order("created_at",{ascending:!1});if(a)throw a;return s||[]}}),{data:j=[]}=u({queryKey:["crm-lead-bookings",t==null?void 0:t.email,t==null?void 0:t.phone],enabled:!!t,queryFn:async()=>{const s=[`email.ilike.${t.email}`];t.phone&&s.push(`phone.eq.${t.phone}`);const{data:a,error:y}=await d.from("bookings").select("id, meeting_type_label, starts_at, status").or(s.join(",")).order("starts_at",{ascending:!1});if(y)throw y;return a||[]}}),i=b({mutationFn:async s=>{const{error:a}=await d.from("leads").update(s).eq("id",r);if(a)throw a},onSuccess:()=>{n.invalidateQueries({queryKey:["crm-lead",r]}),n.invalidateQueries({queryKey:["crm-leads"]}),l.success("Lead updated")},onError:()=>l.error("Could not update lead")}),x=b({mutationFn:async()=>{const s=m.trim();if(!s)throw new Error("empty");const{error:a}=await d.from("lead_activities").insert({lead_id:r,note:s.slice(0,4e3),created_by:(c==null?void 0:c.id)??null});if(a)throw a;await d.from("leads").update({last_contacted_at:new Date().toISOString()}).eq("id",r)},onSuccess:()=>{p(""),n.invalidateQueries({queryKey:["crm-lead-activities",r]}),n.invalidateQueries({queryKey:["crm-lead",r]}),l.success("Note added")},onError:()=>l.error("Could not add note")});if(_)return e.jsx(h,{children:e.jsx("p",{className:"text-sm text-muted-foreground",children:"Loading lead..."})});if(!t)return e.jsxs(h,{children:[e.jsx("p",{className:"text-sm text-muted-foreground mb-4",children:"Lead not found."}),e.jsx(N,{to:"/admin/crm",className:"text-sm text-primary",children:"Back to CRM"})]});const f="h-9 px-2 rounded-md bg-secondary border border-border text-sm text-foreground w-full";return e.jsxs(h,{children:[e.jsxs(N,{to:"/admin/crm",className:"inline-flex items-center gap-1.5 text-sm text-muted-foreground hover:text-foreground mb-4",children:[e.jsx(D,{className:"w-4 h-4"})," Back to CRM"]}),e.jsx("h1",{className:"font-display text-2xl font-bold mb-1",children:t.name}),e.jsxs("p",{className:"text-sm text-muted-foreground mb-6",children:["Created ",o(new Date(t.created_at),"MMM d, yyyy p"),t.last_contacted_at?` · Last contacted ${o(new Date(t.last_contacted_at),"MMM d, yyyy")}`:""]}),e.jsxs("div",{className:"grid gap-4 lg:grid-cols-3",children:[e.jsxs("div",{className:"lg:col-span-2 space-y-4",children:[e.jsxs("div",{className:"rounded-lg border border-border bg-card p-4 space-y-3",children:[e.jsx("h2",{className:"font-medium text-sm",children:"Contact details"}),e.jsxs("dl",{className:"grid sm:grid-cols-2 gap-3 text-sm",children:[e.jsxs("div",{children:[e.jsx("dt",{className:"text-xs text-muted-foreground",children:"Email"}),e.jsx("dd",{children:e.jsx("a",{className:"hover:text-primary",href:`mailto:${t.email}`,children:t.email})})]}),e.jsxs("div",{children:[e.jsx("dt",{className:"text-xs text-muted-foreground",children:"Phone"}),e.jsx("dd",{children:t.phone?e.jsx("a",{className:"hover:text-primary",href:`tel:${t.phone}`,children:t.phone}):"-"})]}),e.jsxs("div",{children:[e.jsx("dt",{className:"text-xs text-muted-foreground",children:"Company"}),e.jsx("dd",{children:t.company||"-"})]}),e.jsxs("div",{children:[e.jsx("dt",{className:"text-xs text-muted-foreground",children:"Source"}),e.jsx("dd",{children:t.source.replace("_"," ")})]}),e.jsxs("div",{className:"sm:col-span-2",children:[e.jsx("dt",{className:"text-xs text-muted-foreground",children:"Source page"}),e.jsx("dd",{className:"break-all",children:t.source_page||"-"})]})]}),t.message&&e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground mb-1",children:"Message"}),e.jsx("p",{className:"text-sm whitespace-pre-wrap bg-secondary/50 rounded-md p-3",children:t.message})]})]}),e.jsxs("div",{className:"rounded-lg border border-border bg-card p-4",children:[e.jsx("h2",{className:"font-medium text-sm mb-3",children:"Activity timeline"}),e.jsxs("div",{className:"space-y-2 mb-4",children:[e.jsx(k,{value:m,onChange:s=>p(s.target.value),placeholder:"Add a note about this lead...",className:"bg-secondary min-h-[80px]"}),e.jsx(v,{size:"sm",onClick:()=>x.mutate(),disabled:!m.trim()||x.isPending,children:x.isPending?"Saving...":"Add note"})]}),g.length===0&&e.jsx("p",{className:"text-sm text-muted-foreground",children:"No notes yet."}),e.jsx("ul",{className:"space-y-3",children:g.map(s=>e.jsxs("li",{className:"border-l-2 border-primary/40 pl-3",children:[e.jsx("p",{className:"text-sm whitespace-pre-wrap",children:s.note}),e.jsx("p",{className:"text-xs text-muted-foreground mt-0.5",children:o(new Date(s.created_at),"MMM d, yyyy p")})]},s.id))})]})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"rounded-lg border border-border bg-card p-4 space-y-3",children:[e.jsx("h2",{className:"font-medium text-sm",children:"Pipeline"}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground",children:"Status"}),e.jsx("select",{className:f,value:t.status,onChange:s=>i.mutate({status:s.target.value}),children:L.map(s=>e.jsx("option",{value:s,children:s},s))})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground",children:"Product"}),e.jsxs("select",{className:f,value:t.product??"",onChange:s=>i.mutate({product:s.target.value||null}),children:[e.jsx("option",{value:"",children:"Unassigned"}),A.map(s=>e.jsx("option",{value:s,children:s},s))]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground",children:"Assigned to"}),e.jsx(S,{className:"bg-secondary",defaultValue:t.assigned_to??"",placeholder:"Name or email",onBlur:s=>{const a=s.target.value.trim()||null;a!==(t.assigned_to??null)&&i.mutate({assigned_to:a})}})]}),e.jsx(v,{variant:"outline",size:"sm",className:"w-full",onClick:()=>i.mutate({last_contacted_at:new Date().toISOString()}),children:"Mark contacted today"})]}),e.jsxs("div",{className:"rounded-lg border border-border bg-card p-4",children:[e.jsxs("h2",{className:"font-medium text-sm mb-3 flex items-center gap-1.5",children:[e.jsx(E,{className:"w-4 h-4"})," Booked meetings"]}),j.length===0&&e.jsx("p",{className:"text-sm text-muted-foreground",children:"No bookings matched this lead."}),e.jsx("ul",{className:"space-y-2",children:j.map(s=>e.jsxs("li",{className:"text-sm",children:[e.jsx("p",{className:"font-medium",children:s.meeting_type_label}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[o(new Date(s.starts_at),"MMM d, yyyy p")," · ",s.status]})]},s.id))})]})]})]})]})}export{G as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.