PageSourceSearch

https://incommand.ai/assets/ClientDetail-C_tHIqRz.js

js incommand.ai collected 2026-10-05 15:00:56 UTC 72,900 bytes, 17 lines download raw bytes

1import{j as e}from"./vendor-radix-BnNg3BH5.js";import{r as o,L as be,i as Os,d as qs}from"./vendor-react-67mgcWXU.js";import{d as Vs,b as Fe,u as ce,D as Ce,e as Se,f as ke,g as Pe,h as De,F as ne,B as g,v as A,G as ze,L as ie,s as _,I as W,w as Ue,x as Oe,y as qe,z as Ve,A as se,a4 as Te,H as fs,aa as $s,C as Ie,a0 as Gs,k as L,T as Ws,l as Ks,m as Ys,ab as Qs,X as Ye,a6 as Js,U as Xs,V as Zs,W as he,Y as ue,E as et}from"./index-Epr9psQo.js";import{u as st}from"./useClientFinancials-CAdp5Uzs.js";import{f as pe,a as tt}from"./formatLabel-C4RR0vUw.js";import{C,d as S,a as T,b as R,c as re}from"./card-DpKBVi42.js";import{M as H}from"./metric-DK0Tcl4m.js";import{B as M}from"./badge-Cl7apleB.js";import{A as Qe,b as at,a as Je}from"./avatar-DkXZyfFy.js";import{T as is,a as ls,b as de,c as K,d as cs,e as q}from"./table-023rHGnC.js";import{S as je}from"./skeleton-CXDkv5zX.js";import{D as fe,a as ge,b as Ne,e as ee}from"./dropdown-menu-Duf4NJvQ.js";import{S as Xe}from"./scroll-area-OnGFamwr.js";import{B as Be}from"./brain-BAvvPRM1.js";import{R as gs}from"./refresh-cw-j1umLsSM.js";import{m as rt}from"./vendor-motion-CSzk-Fqg.js";import{T as nt}from"./target-CyHaUH9t.js";import{C as $e}from"./circle-check-Cv8aCTuA.js";import{T as le}from"./trending-up-CXZcoU_y.js";import{C as it}from"./chevron-right-DQtszqux.js";import{S as lt}from"./shield-BI9NGrnO.js";import{T as me}from"./triangle-alert-C6ISfBPd.js";import{M as ct}from"./message-square-7cL_4-It.js";import{I as ot}from"./info-DuvYtGx9.js";import{C as Ns}from"./circle-alert-CFN03ksH.js";import{a as Ze}from"./vendor-query-PMAhJHCS.js";import{G as dt}from"./GenerateBattlecardWizard-CC8Inmeu.js";import{S as mt}from"./swords-b82pQzfy.js";import{P as Re}from"./plus-DRTmwF2W.js";import{A as xt}from"./arrow-right-LjC5fuTq.js";import{f as _e}from"./format-Bw8okCdv.js";import{G as ht}from"./grid-3x3-ClCYB6FB.js";import{S as ut}from"./separator-C2IB9E9R.js";import{S as pt}from"./sparkles-BcySMUTB.js";import{C as jt}from"./copy-C-Qie2rJ.js";import{D as es}from"./download-CDzT20jl.js";import{M as ft}from"./index-BMcbOcU5.js";import{H as os}from"./heart-A_SpDsLW.js";import{M as ss}from"./minus-CdxVbKUw.js";import{T as ts}from"./trending-down-CFwiQb3W.js";import{P as ae}from"./pencil-PwBd4M7u.js";import{G as gt}from"./globe-DQzIP_ik.js";import{M as Nt}from"./map-pin-CFiJTnw_.js";import{U as vt}from"./user-BG8cpmCD.js";import{M as vs}from"./mail-BWzQtSAz.js";import{P as ys}from"./phone-OWBuGvEd.js";import{P as ws}from"./progress-OXAVVpg3.js";import{A as Ge}from"./activity-DehK1-wv.js";import{D as ve}from"./dollar-sign-CC-jyV1q.js";import{P as yt}from"./package-DW7ssg6N.js";import{C as ds}from"./clock-BYWbH6MV.js";import{C as wt}from"./chart-column-6WlVTHnt.js";import{u as bt}from"./useTerminology-Ci7zIKmj.js";import{U as Ee}from"./upload-2-rh2XwF.js";import{T as We}from"./trash-2-EIFrPvgK.js";import{B as Ke}from"./building-2-DODpaxwc.js";import{U as _t}from"./user-x-Dq8IMr2H.js";import{U as bs}from"./users-uAR6_Qvs.js";import{GranolaMeetingBrowser as Ct}from"./GranolaMeetingBrowser-BDUWnfl4.js";import{A as St}from"./arrow-left-BKCvbsa9.js";import{P as kt}from"./percent-De53lZjx.js";import{B as ms}from"./briefcase-DJg8wfqE.js";import{E as Pt}from"./external-link-DnfaRxEP.js";import{E as xs}from"./ellipsis-C6BfvtHH.js";import{M as Dt}from"./mic-eFw98pWv.js";import{F as Tt}from"./file-spreadsheet-Q6qsWG3M.js";import{F as Rt}from"./file-check-Du927KJ7.js";import"./vendor-supabase-CTlrDMdV.js";import"./vendor-charts-Dck3qfzI.js";import"./vendor-editor-CEJKB4cc.js";import"./circle-CPv7M1K7.js";import"./checkbox-CaPhqoy3.js";import"./firecrawl-ByFAwZpN.js";import"./search-BnpyGpZl.js";import"./en-US-DJiZ5LCQ.js";import"./sheet-BMVGKQ_l.js";import"./settings-CFzYyjTl.js";import"./calendar-Ce69BthR.js";import"./link-2-Bu8oQt2W.js";/**
2 * @license lucide-react v0.462.0 - ISC
3 *
4 * This source code is licensed under the ISC license.
5 * See the LICENSE file in the root directory of this source tree.
6 */const Et=Vs("FileArchive",[["path",{d:"M10 12v-1",key:"v7bkov"}],["path",{d:"M10 18v-2",key:"1cjy8d"}],["path",{d:"M10 7V6",key:"dljcrl"}],["path",{d:"M14 2v4a2 2 0 0 0 2 2h4",key:"tnqrlb"}],["path",{d:"M15.5 22H18a2 2 0 0 0 2-2V7l-5-5H6a2 2 0 0 0-2 2v16a2 2 0 0 0 .274 1.01",key:"gkbcor"}],["circle",{cx:"10",cy:"20",r:"2",key:"1xzdoj"}]]),Ft=["client","opposing_party","witness","related_entity","adverse_party"];function It({open:t,onOpenChange:u,clientId:a,projectId:n,onSuccess:x}){const{currentTenant:j}=Fe(),{toast:c}
6=ce(),d=o.useRef(null),[r,h]=o.useState([]),[f,w]=o.useState(!1),[l,m]=o.useState(!1),[N,y]=o.useState(0),V=()=>{const b=`party_name,party_type,relationship_notes
7"Acme Corporation",opposing_party,"Defendant in Smith v. Acme case"
8"John Smith",witness,"Key witness for plaintiff"
9"Smith Industries",related_entity,"Parent company of Acme"
10"Jane Doe",adverse_party,"Counterparty in transaction"`,E=new Blob([b],{type:"text/csv"}),F=URL.createObjectURL(E),p=document.createElement("a");p.href=F,p.download="conflict_parties_template.csv",document.body.appendChild(p),p.click(),document.body.removeChild(p),URL.revokeObjectURL(F)},z=b=>{const E=b.split(`
11`).filter(p=>p.trim());return E.length<2?[]:E.slice(1).map(p=>{const D=p.match(/(?:^|,)("(?:[^"]*(?:""[^"]*)*)"|[^,]*)/g);if(!D||D.length<1)return{party_name:"",party_type:"",relationship_notes:"",isValid:!1,error:"Invalid CSV format"};const k=v=>v.replace(/^,?"?|"?$/g,"").replace(/""/g,'"').trim(),B=k(D[0]||""),I=k(D[1]||"related_entity"),O=k(D[2]||"");return B?Ft.includes(I)?{party_name:B,party_type:I,relationship_notes:O,isValid:!0}:{party_name:B,party_type:"related_entity",relationship_notes:O,isValid:!0,error:`Invalid type "${I}", defaulting to "related_entity"`}:{party_name:B,party_type:I,relationship_notes:O,isValid:!1,error:"Party name is required"}})},P=b=>{const E=b.target.files?.[0];if(!E)return;const F=new FileReader;F.onload=p=>{const D=p.target?.result,k=z(D);h(k),m(!1),y(0)},F.readAsText(E)},X=async()=>{if(!j?.id||r.length===0)return;const b=r.filter(E=>E.isValid);if(b.length===0){c({title:"No valid parties",description:"Please fix the errors in your CSV file and try again.",variant:"destructive"});return}w(!0);try{const{error:E}=await _.from("conflict_parties").insert(b.map(F=>({tenant_id:j.id,client_id:a||null,project_id:n||null,party_name:F.party_name,party_type:F.party_type,relationship_notes:F.relationship_notes||null})));if(E)throw E;y(b.length),m(!0),c({title:"Import Successful",description:`${b.length} parties have been imported.`}),x()}catch(E){console.error("Error importing parties:",E),c({title:"Import Failed",description:"An error occurred while importing parties.",variant:"destructive"})}finally{w(!1)}},Y=()=>{h([]),m(!1),y(0),d.current&&(d.current.value=""),u(!1)},$=r.filter(b=>b.isValid).length,G=r.filter(b=>!b.isValid).length;return e.jsx(Ce,{open:t,onOpenChange:Y,children:e.jsxs(Se,{className:"sm:max-w-lg",children:[e.jsxs(ke,{children:[e.jsxs(Pe,{className:"flex items-center gap-2",children:[e.jsx(Ee,{className:"h-5 w-5"}),"Bulk Import Parties"]}),e.jsx(De,{children:"Upload a CSV file to import multiple conflict parties at once."})]}),e.jsxs("div",{className:"space-y-4 py-4",children:[e.jsxs("div",{className:"flex items-center justify-between p-3 rounded-lg border bg-muted/30",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ne,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{className:"text-sm",children:"Download CSV template"})]}),e.jsxs(g,{variant:"outline",size:"sm",onClick:V,children:[e.jsx(es,{className:"h-4 w-4 mr-1"}),"Template"]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{htmlFor:"csvFile",children:"Upload CSV File"}),e.jsx("input",{ref:d,id:"csvFile",type:"file",accept:".csv",onChange:P,className:`block w-full text-sm text-muted-foreground
12                file:mr-4 file:py-2 file:px-4
13                file:rounded-md file:border-0
14                file:text-sm file:font-medium
15                file:bg-primary file:text-primary-foreground
16                hover:file:bg-primary/90
17                file:cursor-pointer`}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"CSV format: party_name, party_type, relationship_notes"})]}),r.length>0&&e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"font-medium text-sm",children:"Preview"}),e.jsxs(M,{variant:"secondary",children:[r.length," rows"]}),$>0&&e.jsxs(M,{variant:"default",className:"bg-success",children:[$," valid"]}),G>0&&e.jsxs(M,{variant:"destructive",children:[G," errors"]})]}),e.jsx(Xe,{className:"h-[200px] rounded-lg border",children:e.jsx("div",{className:"p-2 space-y-1",children:r.map((b,E)=>e.jsxs("div",{className:`flex items-center justify-between p-2 rounded text-sm ${b.isValid?"bg-muted/30":"bg-destructive/10"}`,children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("span",{className:"font-medium truncate",children:b.party_name||"(empty)"}),e.jsxs("span",{className:"text-muted-foreground ml-2",children:["• ",b.party_type]})]}),b.isValid?e.jsx($e,{className:"h-4 w-4 text-success shrink-0"}):e.jsxs("div",{className:"flex items-center gap-1 text-destructive shrink-0",children:[e.jsx(me,{className:"h-4 w-4"}),e.jsx("span",{className:"text-xs",children:b.error})]})]},E))})})]}),l&&e.jsxs("div",{className:"flex items-center gap-2 p-3 rounded-lg bg-success/10 text-success",children:[e.jsx($e,{className:"h-5 w-5"}),e.jsxs("span",{className:"font-medium",children:[N," parties imported successfully!"]})]})]}),e.jsxs(ze,{className:"gap-2",children:[e.jsx(g,{variant:"outline",onClick:Y,children:l?"Close"
17:"Cancel"}),!l&&e.jsx(g,{onClick:X,disabled:$===0||f,children:f?e.jsxs(e.Fragment,{children:[e.jsx(ie,{className:"mr-2 h-4 w-4 animate-spin"}),"Importing..."]}):e.jsxs(e.Fragment,{children:[e.jsx(Ee,{className:"mr-2 h-4 w-4"}),"Import ",$," Parties"]})})]})]})})}const He=[{value:"client",label:"Client",icon:Ke},{value:"opposing_party",label:"Opposing Party",icon:_t},{value:"witness",label:"Witness",icon:bs},{value:"related_entity",label:"Related Entity",icon:Ke},{value:"adverse_party",label:"Adverse Party",icon:me}];function Mt({clientId:t,projectId:u}){const{currentTenant:a}=Fe(),{isLegalVertical:n,projectLabel:x}=bt(),{toast:j}=ce(),[c,d]=o.useState([]),[r,h]=o.useState(!0),[f,w]=o.useState(!1),[l,m]=o.useState(""),[N,y]=o.useState("related_entity"),[V,z]=o.useState(""),[P,X]=o.useState(!1),[Y,$]=o.useState(!1);if(!n)return null;o.useEffect(()=>{G()},[t,u,a?.id]);const G=async()=>{if(!a?.id||!t&&!u){h(!1);return}try{let p=_.from("conflict_parties").select("*").eq("tenant_id",a.id).order("created_at",{ascending:!1});t&&(p=p.eq("client_id",t)),u&&(p=p.eq("project_id",u));const{data:D,error:k}=await p;if(k)throw k;d(D||[])}catch(p){console.error("Error loading conflict parties:",p)}finally{h(!1)}},b=async()=>{if(!(!a?.id||!l.trim())){w(!0);try{const{error:p}=await _.from("conflict_parties").insert({tenant_id:a.id,client_id:t||null,project_id:u||null,party_name:l.trim(),party_type:N,relationship_notes:V.trim()||null});if(p)throw p;j({title:"Party Added",description:`${l} has been added to conflict records.`}),m(""),y("related_entity"),z(""),X(!1),G()}catch(p){console.error("Error adding conflict party:",p),j({title:"Error",description:"Failed to add party. Please try again.",variant:"destructive"})}finally{w(!1)}}},E=async p=>{try{const{error:D}=await _.from("conflict_parties").delete().eq("id",p);if(D)throw D;j({title:"Party Removed",description:"The party has been removed from conflict records."}),G()}catch(D){console.error("Error deleting conflict party:",D),j({title:"Error",description:"Failed to remove party. Please try again.",variant:"destructive"})}},F=p=>He.find(D=>D.value===p)||He[3];return r?e.jsx(C,{children:e.jsx(S,{className:"py-6 flex items-center justify-center",children:e.jsx(ie,{className:"h-5 w-5 animate-spin text-muted-foreground"})})}):e.jsxs(C,{children:[e.jsx(T,{className:"pb-3",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs(R,{className:"text-base flex items-center gap-2",children:[e.jsx(me,{className:"h-4 w-4 text-warning"}),"Conflict Parties"]}),e.jsx(re,{children:"Track related parties for conflict of interest checks"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(g,{variant:"outline",size:"sm",onClick:()=>$(!0),children:[e.jsx(Ee,{className:"h-4 w-4 mr-1"}),"Import"]}),e.jsxs(g,{variant:"outline",size:"sm",onClick:()=>X(!P),children:[e.jsx(Re,{className:"h-4 w-4 mr-1"}),"Add Party"]})]})]})}),e.jsxs(S,{className:"space-y-4",children:[P&&e.jsxs("div",{className:"space-y-3 p-4 rounded-lg border bg-muted/30",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{htmlFor:"partyName",children:"Party Name"}),e.jsx(W,{id:"partyName",value:l,onChange:p=>m(p.target.value),placeholder:"Enter name..."})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{htmlFor:"partyType",children:"Type"}),e.jsxs(Ue,{value:N,onValueChange:y,children:[e.jsx(Oe,{children:e.jsx(qe,{})}),e.jsx(Ve,{children:He.map(p=>e.jsx(se,{value:p.value,children:p.label},p.value))})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{htmlFor:"partyNotes",children:"Notes (optional)"}),e.jsx(Te,{id:"partyNotes",value:V,onChange:p=>z(p.target.value),placeholder:"Relationship details...",rows:2})]}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(g,{variant:"ghost",size:"sm",onClick:()=>X(!1),children:"Cancel"}),e.jsxs(g,{size:"sm",onClick:b,disabled:!l.trim()||f,children:[f?e.jsx(ie,{className:"h-4 w-4 animate-spin mr-1"}):null,"Add Party"]})]})]}),c.length===0?e.jsxs("p",{className:"text-sm text-muted-foreground text-center py-4",children:["No conflict parties recorded for this ",u?x.toLowerCase():"client","."]}):e.jsx("div",{className:"space-y-2",children:c.map(p=>{const D=F(p.party_type),k=D.icon;return e.jsxs("div",{className:"flex items-start justify-between p-3 rounded-lg border bg-card",children:[e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx(k,{className:"h-4 w-4 text-muted-foreground mt-0.5"}),e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:p.party_name}),e.jsxs("div",{className:"flex items-center gap-2 mt-1",children:[e.jsx(M,{variant:"outline",className:"text-xs",children:D.label}),p.relationship_notes&&e.jsx("span",{className:"text-xs text-muted-foreground",children:p.relationship_notes})]})]})]}),e.jsx(g,{variant:"ghost",size:"icon",className:"h-8 w-8 text-muted-foreground hover:text-destructive",onClick:()=>E(p.id),"aria-label":"Delete",children:e.jsx(We,{className:"h-4 w-4"})})]}
17,p.id)})})]}),e.jsx(It,{open:Y,onOpenChange:$,clientId:t,projectId:u,onSuccess:G})]})}const At={strong:"bg-success/10 text-success border-success/20",healthy:"bg-success/10 text-success border-success/20",neutral:"bg-warning/10 text-warning border-warning/20",at_risk:"bg-destructive/10 text-destructive border-destructive/20",critical:"bg-destructive/10 text-destructive border-destructive/20"},Bt={strong:"Strong",healthy:"Healthy",neutral:"Neutral",at_risk:"At Risk",critical:"Critical"},Ht={opportunity:e.jsx(le,{className:"h-4 w-4 text-success"}),risk:e.jsx(me,{className:"h-4 w-4 text-destructive"}),observation:e.jsx(ot,{className:"h-4 w-4 text-muted-foreground"}),action_required:e.jsx(Ns,{className:"h-4 w-4 text-warning"})};function Lt({clientId:t,clientName:u}){const{toast:a}=ce(),[n,x]=o.useState(null),[j,c]=o.useState(!1),d=async()=>{c(!0);try{const{data:r,error:h}=await _.functions.invoke("generate-client-briefing",{body:{client_id:t}});if(h)throw h;if(r.error)throw new Error(r.error);x(r.briefing),a({title:"Briefing Generated",description:"AI intelligence briefing is ready."})}catch(r){console.error("Error generating briefing:",r),a({title:"Error",description:r.message||"Failed to generate briefing",variant:"destructive"})}finally{c(!1)}};return e.jsxs(C,{children:[e.jsx(T,{children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"h-10 w-10 rounded-lg bg-primary/10 flex items-center justify-center",children:e.jsx(Be,{className:"h-5 w-5 text-primary"})}),e.jsxs("div",{children:[e.jsx(R,{children:"Intelligence Briefing"}),e.jsx(re,{children:n?`Generated ${new Date(n.generated_at).toLocaleDateString()}`:"AI-powered client insights"})]})]}),e.jsxs(g,{onClick:d,disabled:j,variant:n?"outline":"default",children:[j?e.jsx(ie,{className:"h-4 w-4 animate-spin mr-2"}):e.jsx(gs,{className:"h-4 w-4 mr-2"}),n?"Refresh":"Generate"]})]})}),e.jsx(S,{children:n?e.jsx(Xe,{className:"h-[500px] pr-4",children:e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"p-4 rounded-lg bg-muted/50 border",children:[e.jsxs("div",{className:"flex items-center justify-between mb-2",children:[e.jsx("p",{className:"text-sm font-medium",children:"Executive Summary"}),e.jsx(M,{className:At[n.relationship_health],children:Bt[n.relationship_health]})]}),e.jsx("p",{className:"text-sm text-muted-foreground",children:n.executive_summary})]}),e.jsxs("div",{className:"grid grid-cols-4 gap-3",children:[e.jsxs("div",{className:"text-center p-3 rounded-lg bg-card border",children:[e.jsxs(H,{size:"md",children:["$",(n.metrics.revenue_ytd/1e3).toFixed(0),"K"]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Revenue YTD"})]}),e.jsxs("div",{className:"text-center p-3 rounded-lg bg-card border",children:[e.jsx(H,{size:"md",children:n.metrics.active_projects}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Active Projects"})]}),e.jsxs("div",{className:"text-center p-3 rounded-lg bg-card border",children:[e.jsxs(H,{size:"md",children:[n.metrics.hours_last_90_days.toFixed(0),"h"]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Hours (90d)"})]}),e.jsxs("div",{className:"text-center p-3 rounded-lg bg-card border",children:[e.jsxs(H,{size:"md",children:[n.metrics.billable_rate.toFixed(0),"%"]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Billable Rate"})]})]}),n.key_insights.length>0&&e.jsxs("div",{children:[e.jsxs("p",{className:"text-sm font-medium mb-3 flex items-center gap-2",children:[e.jsx(fs,{className:"h-4 w-4"}),"Key Insights"]}),e.jsx("div",{className:"space-y-2",children:n.key_insights.map((r,h)=>e.jsx(rt.div,{initial:{opacity:0,x:-10},animate:{opacity:1,x:0},transition:{delay:h*.05},className:"p-3 rounded-lg border bg-card",children:e.jsxs("div",{className:"flex items-start gap-2",children:[Ht[r.type],e.jsxs("div",{className:"flex-1",children:[e.jsx("p",{className:"text-sm font-medium",children:r.title}),e.jsx("p",{className:"text-sm text-muted-foreground",children:r.insight})]})]})},h))})]}),n.recommended_actions.length>0&&e.jsxs("div",{children:[e.jsxs("p",{className:"text-sm font-medium mb-3 flex items-center gap-2",children:[e.jsx(nt,{className:"h-4 w-4"}),"Recommended Actions"]}),e.jsx("ul",{className:"space-y-2",children:n.recommended_actions.map((r,h)=>e.jsxs("li",{className:"flex items-start gap-2 text-sm",children:[e.jsx($e,{className:"h-4 w-4 mt-0.5 text-primary shrink-0"}),e.jsx("span",{children:r})]},h))})]}),n.expansion_opportunities.length>0&&e.jsxs("div",{children:[e.jsxs("p",{className:"text-sm font-medium mb-3 flex items-center gap-2",children:[e.jsx(le,{className:"h-4 w-4 text-success"}),"Expansion Opportunities"]}),e.jsx("ul",{className:"space-y-2",children:n.expansion_opportunities.map((r,h)=>e.jsxs("li",{className:"flex items-start gap-2 text-sm text-muted-foreground",children:[e.jsx(it,{className:"h-4 w-4 mt-0.5 shrink-0"}),e.jsx("span",{children:r})]},h))})]}),n.relationship_risks.length>0&&e.jsxs("div",{children:[e.jsxs("p",{className:"text-sm font-medium mb-3 flex items-center gap-2",children:[e.jsx(lt,{className:"h-4 w-4 text-destructive"}),"Relationship Risks"]}),e.jsx("ul",{className:"space-y-2",children:n.relationship_risks.map((r,h)=>e.jsxs("li",{className:"flex items-start gap-2 text-sm text-muted-foreground",children:[e.jsx(me,{className:"h-4 w-4 mt-0.5 text-destructive shrink-0"}),e.jsx("span",{children:r})]},h))})]}),n.talking_points.length>0&&e.jsxs("div",{children:[e.jsxs("p",{className:"text-sm font-medium mb-3 flex items-center gap-2",children:[e.jsx(ct,{className:"h-4 w-4"}),"Talking Points for Next Meeting"]}),e.jsx("ul",{className:"space-y-2",children:n.talking_points.map((r,h)=>e.jsxs("li",{className:"flex items-start gap-2 text-sm text-muted-foreground",children:[e.jsxs("span",{className:"font-medium text-foreground",children:[h+1,"."]}
17),e.jsx("span",{children:r})]},h))})]})]})}):e.jsxs("div",{className:"text-center py-12",children:[e.jsx(Be,{className:"h-16 w-16 text-muted-foreground/20 mx-auto mb-4"}),e.jsx("p",{className:"text-muted-foreground mb-2",children:"No briefing generated yet"}),e.jsxs("p",{className:"text-sm text-muted-foreground mb-4",children:["Generate an AI-powered intelligence briefing for ",u]}),e.jsxs(g,{onClick:d,disabled:j,children:[j?e.jsx(ie,{className:"h-4 w-4 animate-spin mr-2"}):e.jsx(Be,{className:"h-4 w-4 mr-2"}),"Generate Briefing"]})]})})]})}function zt({clientId:t,clientName:u,website:a,industry:n}){const[x,j]=o.useState(!1),{data:c,refetch:d}=Ze({queryKey:["client-battlecards",t],queryFn:async()=>{const{data:r,error:h}=await _.from("battlecards").select("id, company_name, battlecard_type, is_approved, updated_at").eq("client_id",t).order("updated_at",{ascending:!1});if(h)throw h;return r||[]}});return e.jsxs(C,{children:[e.jsxs(T,{className:"flex flex-row items-start justify-between space-y-0",children:[e.jsxs("div",{children:[e.jsxs(R,{className:"flex items-center gap-2",children:[e.jsx(mt,{className:"h-5 w-5"}),"Battlecards"]}),e.jsx(re,{children:"Account intelligence battlecards linked to this client"})]}),e.jsxs(g,{variant:"outline",size:"sm",onClick:()=>j(!0),children:[e.jsx(Re,{className:"h-4 w-4 mr-2"}),"Generate Battlecard"]})]}),e.jsx(S,{children:c&&c.length>0?e.jsx("div",{className:"space-y-2",children:c.map(r=>e.jsxs(be,{to:`/intelligence/battlecards/${r.id}`,className:"flex items-center justify-between rounded-md border p-3 hover:bg-muted/50 transition-colors",children:[e.jsxs("div",{className:"flex items-center gap-3 min-w-0",children:[e.jsx("span",{className:"font-medium truncate",children:r.company_name}),e.jsx(M,{variant:"outline",className:"capitalize shrink-0",children:r.battlecard_type||"prospect"}),r.is_approved&&e.jsx(M,{variant:"secondary",className:"bg-success/10 text-success shrink-0",children:"Approved"})]}),e.jsxs("div",{className:"flex items-center gap-2 text-sm text-muted-foreground shrink-0",children:["Updated ",_e(new Date(r.updated_at),"MMM d, yyyy"),e.jsx(xt,{className:"h-4 w-4"})]})]},r.id))}):e.jsxs("div",{className:"text-center py-6 text-muted-foreground text-sm",children:["No battlecard yet — generate one to get living account intelligence for ",u,"."]})}),e.jsx(dt,{open:x,onOpenChange:j,onComplete:()=>d(),initialClient:{id:t,name:u,website:a,industry:n}})]})}const hs={sold:{label:"Sold",className:"bg-success/10 text-success"},pitched:{label:"Pitched",className:"bg-warning/10 text-warning"},whitespace:{label:"Whitespace",className:"bg-info/10 text-info"}};function Ut({clientId:t,tenantId:u}){const{data:a,isLoading:n}=Ze({queryKey:["client-whitespace",t],queryFn:async()=>{const[c,d]=await Promise.all([_.from("service_catalog").select("id, name, category, sort_order").eq("tenant_id",u).eq("is_active",!0).order("category").order("sort_order"),_.from("proposals").select("id, proposal_status, signed_at").eq("client_id",t)]);if(c.error)throw c.error;if(d.error)throw d.error;const r=d.data||[],h=new Set(r.filter(m=>m.proposal_status==="signed"||m.signed_at).map(m=>m.id)),f=r.map(m=>m.id),w=new Set,l=new Set;if(f.length>0){const{data:m,error:N}=await _.from("proposal_services").select("proposal_id, service_id").in("proposal_id",f);if(N)throw N;for(const y of m||[])h.has(y.proposal_id)?w.add(y.service_id):l.add(y.service_id)}return(c.data||[]).map(m=>({id:m.id,name:m.name,category:m.category,status:w.has(m.id)?"sold":l.has(m.id)?"pitched":"whitespace"}))}});if(n||!a||a.length===0)return null;const x=new Map;for(const c of a){const d=c.category||"Other";x.has(d)||x.set(d,[]),x.get(d).push(c)}const j=a.filter(c=>c.status==="whitespace").length;return e.jsxs(C,{children:[e.jsxs(T,{children:[e.jsxs(R,{className:"flex items-center gap-2",children:[e.jsx(ht,{className:"h-5 w-5"}),"Whitespace Analysis"]}),e.jsxs(re,{children:["Service catalog coverage for this client — ",j," of ",a.length," services are untouched expansion opportunities"]})]}
17),e.jsx(S,{className:"space-y-4",children:Array.from(x.entries()).map(([c,d])=>e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium mb-2 capitalize",children:c}),e.jsx("div",{className:"flex flex-wrap gap-2",children:d.map(r=>e.jsxs(M,{variant:"secondary",className:hs[r.status].className,children:[r.name," · ",hs[r.status].label]},r.id))})]},c))})]})}function Ot({clientId:t,clientName:u}){const[a,n]=o.useState(!1),[x,j]=o.useState(!1),[c,d]=o.useState(null),[r,h]=o.useState(!1),{toast:f}=ce(),w=async()=>{j(!0);try{const{data:N,error:y}=await _.functions.invoke("generate-qbr-pack",{body:{client_id:t}});if(y)throw y;if(N?.error)throw new Error(N.error);d(N.pack_markdown)}catch(N){Gs.error("QBR pack error:",N),f({title:"Error generating QBR pack",description:N instanceof Error?N.message:"Please try again.",variant:"destructive"})}finally{j(!1)}},l=async()=>{c&&(await navigator.clipboard.writeText(c),h(!0),f({title:"QBR pack copied to clipboard"}),setTimeout(()=>h(!1),2e3))},m=()=>{if(!c)return;const N=u.replace(/[^\w\s-]/g,"").trim(),y=new Date().toISOString().slice(0,10),V=new Blob([c],{type:"text/markdown"}),z=document.createElement("a"),P=URL.createObjectURL(V);z.href=P,z.download=`QBR Prep - ${N} - ${y}.md`,document.body.appendChild(z),z.click(),z.remove(),URL.revokeObjectURL(P)};return e.jsxs(Ce,{open:a,onOpenChange:n,children:[e.jsx($s,{asChild:!0,children:e.jsxs(g,{variant:"outline",size:"sm",children:[e.jsx(ne,{className:"h-4 w-4 mr-2"}),"QBR Prep Pack"]})}),e.jsxs(Se,{className:"max-w-2xl max-h-[85vh] flex flex-col",children:[e.jsxs(ke,{children:[e.jsxs(Pe,{className:"flex items-center gap-2",children:[e.jsx(ne,{className:"h-5 w-5 text-primary"}),"QBR Prep Pack"]}),e.jsxs(De,{children:["Internal preparation pack for a quarterly business review with ",u]})]}),c?e.jsxs("div",{className:"flex flex-col flex-1 min-h-0",children:[e.jsxs("div",{className:"flex items-center justify-between mb-3",children:[e.jsx(M,{variant:"secondary",className:"bg-success/10 text-success",children:"Internal only"}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(g,{variant:"outline",size:"sm",onClick:l,children:[r?e.jsx(Ie,{className:"h-4 w-4 mr-1"}):e.jsx(jt,{className:"h-4 w-4 mr-1"}),r?"Copied":"Copy"]}),e.jsxs(g,{variant:"outline",size:"sm",onClick:m,children:[e.jsx(es,{className:"h-4 w-4 mr-1"}),"Download"]}),e.jsx(g,{variant:"outline",size:"sm",onClick:w,disabled:x,children:x?e.jsx(ie,{className:"h-4 w-4 animate-spin"}):"Regenerate"})]})]}),e.jsx(ut,{className:"mb-3"}),e.jsx(Xe,{className:"flex-1 max-h-[50vh]",children:e.jsx("div",{className:"prose prose-sm dark:prose-invert max-w-none pr-4",children:e.jsx(ft,{children:c})})})]}):e.jsxs("div",{className:"space-y-3 py-2",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Composes an internal prep pack from delivery status, commercials, account health, recent intelligence, and whitespace expansion opportunities — with a suggested agenda."}),e.jsx(g,{onClick:w,disabled:x,className:"w-full",children:x?e.jsxs(e.Fragment,{children:[e.jsx(ie,{className:"h-4 w-4 mr-2 animate-spin"}),"Composing pack..."]}):e.jsxs(e.Fragment,{children:[e.jsx(pt,{className:"h-4 w-4 mr-2"}),"Generate QBR Prep Pack"]})})]})]})]})}const qt=t=>t>=80||t>=60?"text-success-soft-foreground bg-success-soft border-success/30":t>=40||t>=20?"text-warning-soft-foreground bg-warning-soft border-warning/30":"text-destructive-soft-foreground bg-destructive-soft border-destructive/30",Vt=t=>t>=80?"Excellent":t>=60?"Good":t>=40?"Fair":t>=20?"At Risk":"Critical",$t=t=>t>=70?e.jsx(le,{className:"h-3 w-3"}):t>=40?e.jsx(ss,{className:"h-3 w-3"}):e.jsx(ts,{className:"h-3 w-3"});function Gt({score:t,lastCalculatedAt:u,size:a="md",showLabel:n=!0,className:x}){if(t==null)return e.jsxs("div",{className:L("flex items-center gap-1.5 text-muted-foreground",a==="sm"&&"text-xs",a==="md"&&"text-sm",a==="lg"&&"text-base",x),children:[e.jsx(os,{className:L("text-muted-foreground/50",a==="sm"&&"h-3 w-3",a==="md"&&"h-4 w-4",a==="lg"&&"h-5 w-5")}),e.jsx("span",{children:"No score"})]});const j=qt(t),c=Vt(t),d=$t(t),r={sm:"px-1.5 py-0.5 text-xs gap-1",md:"px-2 py-1 text-sm gap-1.5",lg:"px-3 py-1.5 text-base gap-2"},h=e.jsxs("div",{className:L("inline-flex items-center rounded-full border font-medium",j,r[a],x),children:[e.jsx(os,{className:L(a==="sm"&&"h-3 w-3",a==="md"&&"h-4 w-4",a==="lg"&&"h-5 w-5")}),e.jsx("span",{className:"font-semibold",children:t}),n&&e.jsxs(e.Fragment,{children:[e.jsx("span",{className:"opacity-60",children:"·"}),e.jsxs("span",{className:"flex items-center gap-0.5",children:[d,c]})]})]}),f=e.jsxs("div",{className:"space-y-1",children:[e.jsxs("div",{className:"font-medium",children:["Relationship Health: ",t,"/100"]}),e.jsx("div",{className:"text-xs text-muted-foreground",children:"Based on communication sentiment and signals from the last 90 days."}),u&&e.jsxs("div",{className:"text-xs text-muted-foreground",children:["Last updated: ",new Date(u).toLocaleDateString()]})]});return e.jsxs(Ws,{children:[e.jsx(Ks,{asChild:!0,children:h}),e.jsx(Ys,{side:"top",className:"max-w-xs",children:f})]})}const Wt=["active","inactive","prospect"],Kt=["Technology","Healthcare","Finance","Manufacturing","Retail","Professional Services","Other"];function us({value:t,placeholder:u,onSave:a,type:n="text"}
17){const[x,j]=o.useState(!1),[c,d]=o.useState(t),[r,h]=o.useState(!1),f=o.useRef(null);o.useEffect(()=>{x&&(f.current?.focus(),f.current?.select())},[x]),o.useEffect(()=>{d(t)},[t]);const w=async()=>{if(c===t){j(!1);return}h(!0);try{await a(c),j(!1)}finally{h(!1)}},l=()=>{d(t),j(!1)};return x?e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(W,{ref:f,type:n,value:c,onChange:m=>d(m.target.value),onKeyDown:m=>{m.key==="Enter"&&w(),m.key==="Escape"&&l()},onBlur:()=>!r&&l(),className:"h-7 text-sm",disabled:r}),e.jsx(g,{variant:"ghost",size:"icon",className:"h-6 w-6 shrink-0",onMouseDown:m=>m.preventDefault(),onClick:w,disabled:r,"aria-label":"Save",children:e.jsx(Ie,{className:"h-3 w-3 text-success"})}),e.jsx(g,{variant:"ghost",size:"icon",className:"h-6 w-6 shrink-0",onClick:l,disabled:r,children:e.jsx(Ye,{className:"h-3 w-3 text-destructive"})})]}):e.jsxs("button",{onClick:()=>j(!0),className:"flex items-center gap-1 group text-left",children:[e.jsx("span",{className:L(!t&&"italic text-muted-foreground/70","text-sm"),children:t||u}),e.jsx(ae,{className:"h-3 w-3 opacity-0 group-hover:opacity-100 transition-opacity text-muted-foreground"})]})}function Yt({client:t,onUpdate:u,onCreateSOW:a,isCreatingSOW:n,onRefresh:x}){const{toast:j}=ce(),[c,d]=o.useState(!1),r=async()=>{d(!0);try{const{data:l,error:m}=await _.functions.invoke("calculate-health-scores",{body:{tenant_id:t.tenant_id,client_id:t.id}});if(m)throw m;const N=l?.results?.[0]?.score;j({title:"Health score updated",description:N!==void 0?`New score: ${N}/100`:void 0}),x?.()}catch(l){j({title:"Error recalculating health score",description:l instanceof Error?l.message:void 0,variant:"destructive"})}finally{d(!1)}},{practices:h}=Qs(),f=async(l,m)=>{try{await u({[l]:m||null})}catch(N){throw j({title:"Error updating field",variant:"destructive"}),N}},w=h.find(l=>l.id===t.practice_id);return e.jsxs("div",{className:"flex items-start gap-4",children:[e.jsxs(Qe,{className:"h-16 w-16 shrink-0",children:[e.jsx(at,{src:t.logo_url||""}),e.jsx(Je,{className:"bg-primary/10 text-primary text-xl",children:t.name.substring(0,2).toUpperCase()})]}),e.jsxs("div",{className:"flex-1 min-w-0 space-y-1.5",children:[e.jsx(us,{value:t.name,placeholder:"Company Name",onSave:l=>f("name",l)}),e.jsxs("div",{className:"flex items-center gap-3 text-muted-foreground flex-wrap",children:[e.jsxs(fe,{children:[e.jsx(ge,{asChild:!0,children:e.jsxs("button",{className:"flex items-center gap-1 group text-sm hover:text-foreground transition-colors",children:[e.jsx("span",{className:L(!t.industry&&"italic text-muted-foreground/70"),children:t.industry||"Set industry"}),e.jsx(ae,{className:"h-3 w-3 opacity-0 group-hover:opacity-100 transition-opacity"})]})}),e.jsx(Ne,{children:Kt.map(l=>e.jsx(ee,{onClick:()=>f("industry",l),className:L(t.industry===l&&"bg-accent"),children:l},l))})]}),t.website?e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsxs("a",{href:t.website,target:"_blank",rel:"noopener noreferrer",className:"flex items-center gap-1 hover:text-primary text-sm",children:[e.jsx(gt,{className:"h-3 w-3"}),"Website"]}),e.jsx("button",{onClick:async()=>{const l=prompt("Website URL:",t.website||"");l!==null&&await f("website",l)},className:"opacity-0 hover:opacity-100 focus:opacity-100",children:e.jsx(ae,{className:"h-3 w-3"})})]}):e.jsx(us,{value:"",placeholder:"Add website",type:"url",onSave:l=>f("website",l)}),e.jsxs(fe,{children:[e.jsx(ge,{asChild:!0,children:e.jsx("button",{className:"group",children:e.jsxs(M,{variant:t.status==="active"?"default":"secondary",className:"cursor-pointer",children:[pe(t.status||"active"),e.jsx(ae,{className:"h-2.5 w-2.5 ml-1 opacity-0 group-hover:opacity-100 transition-opacity"})]})})}),e.jsx(Ne,{children:Wt.map(l=>e.jsx(ee,{onClick:()=>f("status",l),className:L(t.status===l&&"bg-accent"),children:pe(l)},l))})]}),h.length>0&&e.jsxs(fe,{children:[e.jsx(ge,{asChild:!0,children:e.jsxs("button",{className:"group flex items-center gap-1 text-sm hover:text-foreground transition-colors",children:[w?e.jsxs("span",{className:"flex items-center gap-1.5",children:[w.color&&e.jsx("span",{className:"h-2 w-2 rounded-full",style:{backgroundColor:w.color}}),w.name]}
17):e.jsx("span",{className:"italic text-muted-foreground/70",children:"Set practice"}),e.jsx(ae,{className:"h-3 w-3 opacity-0 group-hover:opacity-100 transition-opacity"})]})}),e.jsxs(Ne,{children:[e.jsx(ee,{onClick:()=>f("practice_id",null),children:e.jsx("span",{className:"text-muted-foreground",children:"None"})}),h.map(l=>e.jsx(ee,{onClick:()=>f("practice_id",l.id),className:L(t.practice_id===l.id&&"bg-accent"),children:e.jsxs("span",{className:"flex items-center gap-2",children:[l.color&&e.jsx("span",{className:"h-2 w-2 rounded-full",style:{backgroundColor:l.color}}),l.name]})},l.id))]})]}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(Gt,{score:t.relationship_health_score,lastCalculatedAt:t.last_health_calculated_at,size:"sm"}),e.jsx(g,{variant:"ghost",size:"icon",className:"h-6 w-6",onClick:r,disabled:c,title:"Recalculate health score","aria-label":"Recalculate health score",children:e.jsx(gs,{className:L("h-3 w-3",c&&"animate-spin")})})]})]})]}),e.jsx("div",{className:"flex gap-2 shrink-0",children:e.jsxs(g,{variant:"outline",onClick:a,disabled:n,children:[e.jsx(ne,{className:"h-4 w-4 mr-2"}),n?"Creating...":"New SOW / Change Order"]})})]})}function we({value:t,placeholder:u,icon:a,onSave:n,type:x="text",label:j}){const[c,d]=o.useState(!1),[r,h]=o.useState(t),[f,w]=o.useState(!1),l=o.useRef(null);o.useEffect(()=>{c&&(l.current?.focus(),l.current?.select())},[c]),o.useEffect(()=>{h(t)},[t]);const m=async()=>{if(r===t){d(!1);return}w(!0);try{await n(r),d(!1)}finally{w(!1)}},N=()=>{h(t),d(!1)};return c?e.jsxs("div",{className:"flex items-center gap-2",children:[a&&e.jsx(a,{className:"h-4 w-4 text-muted-foreground shrink-0 mt-0.5"}),e.jsx(W,{ref:l,type:x,value:r,onChange:y=>h(y.target.value),onKeyDown:y=>{y.key==="Enter"&&m(),y.key==="Escape"&&N()},onBlur:()=>!f&&N(),className:"h-7 text-sm",disabled:f}),e.jsx(g,{variant:"ghost",size:"icon",className:"h-6 w-6 shrink-0",onMouseDown:y=>y.preventDefault(),onClick:m,"aria-label":"Save",children:e.jsx(Ie,{className:"h-3 w-3 text-success"})}),e.jsx(g,{variant:"ghost",size:"icon",className:"h-6 w-6 shrink-0",onClick:N,children:e.jsx(Ye,{className:"h-3 w-3 text-destructive"})})]}):e.jsxs("button",{onClick:()=>d(!0),className:"flex items-start gap-2 text-left group w-full hover:bg-muted/50 rounded-md p-1 -m-1 transition-colors",children:[a&&e.jsx(a,{className:"h-4 w-4 text-muted-foreground shrink-0 mt-0.5"}),e.jsxs("div",{className:"flex-1 min-w-0",children:[j&&e.jsx("p",{className:"text-xs text-muted-foreground",children:j}),e.jsx("span",{className:L("text-sm",!t&&"italic text-muted-foreground/70"),children:t||u})]}),e.jsx(ae,{className:"h-3 w-3 text-muted-foreground opacity-0 group-hover:opacity-100 transition-opacity shrink-0 mt-0.5"})]})}function Qt({value:t,placeholder:u,label:a,onSave:n}){const[x,j]=o.useState(!1),[c,d]=o.useState(t),[r,h]=o.useState(!1),f=o.useRef(null);o.useEffect(()=>{x&&f.current?.focus()},[x]),o.useEffect(()=>{d(t)},[t]);const w=async()=>{if(c===t){j(!1);return}h(!0);try{await n(c),j(!1)}finally{h(!1)}},l=()=>{d(t),j(!1)};return x?e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{className:"text-sm font-medium",children:a}),e.jsx(Te,{ref:f,value:c,onChange:m=>d(m.target.value),onKeyDown:m=>{m.key==="Escape"&&l(),m.key==="Enter"&&(m.metaKey||m.ctrlKey)&&w()},rows:3,disabled:r,className:"text-sm"}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsxs(g,{variant:"ghost",size:"sm",className:"h-7",onMouseDown:m=>m.preventDefault(),onClick:w,disabled:r,children:[e.jsx(Ie,{className:"h-3 w-3 mr-1"})," Save"]}),e.jsxs(g,{variant:"ghost",size:"sm",className:"h-7",onClick:l,children:[e.jsx(Ye,{className:"h-3 w-3 mr-1"})," Cancel"]}),e.jsx("span",{className:"text-xs text-muted-foreground ml-auto",children:"⌘+Enter to save"})]})]}):e.jsxs("button",{onClick:()=>j(!0),className:"text-left group w-full hover:bg-muted/50 rounded-md p-1 -m-1 transition-colors",children:[e.jsx("p",{className:"text-sm font-medium mb-1",children:a}),e.jsx("span",{className:L("text-sm",t?"text-muted-foreground":"italic text-muted-foreground/70"),children:t||u}),e.jsx(ae,{className:"h-3 w-3 text-muted-foreground opacity-0 group-hover:opacity-100 transition-opacity inline-block ml-2"})]})}function Jt({client:t,onUpdate:u}){const{toast:a}=ce(),n=async(x,j)=>{try{await u({[x]:j||null})}catch(c){throw a({title:"Error updating field",variant:"destructive"}),c}};return e.jsxs(C,{children:[e.jsx(T,{children:e.jsx(R,{className:"text-lg",children:"Client Information"})}),e.jsxs(S,{className:"space-y-4",children:[e.jsx(we,{value:t.address||"",placeholder:"Add address",icon:Nt,onSave:x=>n("address",x)}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("p",{className:"text-sm font-medium",children:"Primary Contact"}),e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx(Qe,{className:"h-8 w-8 shrink-0",children:e.jsx(Je,{children:(t.primary_contact_name||"?").substring(0,2)})}),e.jsxs("div",{className:"flex-1 min-w-0 space-y-1",children:[e.jsx(we,{value:t.primary_contact_name||"",placeholder:"Add contact name",icon:vt,onSave:x=>n("primary_contact_name",x)}),e.jsx(we,{value:t.primary_contact_email||"",placeholder:"Add contact email",icon:vs,type:"email",onSave:x=>n("primary_contact_email",x)}),e.jsx(we,{value:t.primary_contact_phone||"",placeholder:"Add phone number",icon:ys,type:"tel",onSave:x=>n("primary_contact_phone",x)})]})]})]}),e.jsx(Qt,{value:t.notes||"",placeholder:"Add notes...",label:"Notes",onSave:x=>n("notes",x)})]})]})}const Xt={up:le,down:ts,flat:ss},Zt={up:"text-success",down:"text-destructive",flat:"text-muted-foreground"};function Le({label:t,score:u,icon:a}){const n=u>=70?"bg-success":u>=40?"bg-warning":"bg-destructive";return e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs("div",{className:"flex items-center justify-between text-xs",children:[e.jsxs("span",{className:"flex items-center gap-1.5 text-muted-foreground",children:[e.jsx(a,{className:"h-3.5 w-3.5"}),t]}),e.jsx("span",{className:"font-medium",children:u})]}),e.jsx(ws,{value:u,className:"h-2",indicatorClassName:n})]})}function ea({clientId:t}){const{currentTenant:u}=Fe(),{data:a,isLoading:n}=Ze({queryKey:["client-intelligence",t,u?.id],queryFn:async()=>{const{data:c,error:d}=await _.from("client_intelligence").select("*").eq("client_id",t).eq("tenant_id",u.id).single();if(d&&d.code!=="PGRST116")throw d;return c},enabled:!!t&&!!u?.id});if(n)return e.jsx(C,{children:e.jsx(S,{className:"py-8 text-center text-sm text-muted-foreground",children:"Loading intelligence..."})});if(!a)return e.jsx(C,{children:e.jsx(S,{className:"py-8 text-center text-sm text-muted-foreground",children:"No intelligence data yet. Health scores are computed weekly."})});const x=Xt[a.health_trend]||ss,j=Zt[a.health_trend]||"text-muted-foreground";return e.jsxs("div",{className:"space-y-4",children:[e.jsxs(C,{children:[e.jsx(T,{className:"pb-3",children:e.jsxs(R,{className:"text-sm font-medium flex items-center gap-2",children:[e.jsx(Ge,{className:"h-4 w-4"}),"Client Health Score",e.jsxs(M,{variant:"outline",className:"ml-auto",children:["Last updated: ",new Date(a.last_computed_at).toLocaleDateString()]})]})}),e.jsxs(S,{children:[e.jsxs("div",{className:"flex items-center gap-4 mb-6",children:[e.jsx("div",{className:L("text-4xl font-bold",a.health_score>=70?"text-success":a.health_score>=40?"text-warning":"text-destructive"),children:a.health_score}),e.jsxs("div",{className:L("flex items-center gap-1",j),children:[e.jsx(x,{className:"h-4 w-4"}),e.jsx("span",{className:"text-sm capitalize",children:a.health_trend})]})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsx(Le,{label:"Financial",score:a.financial_score,icon:ve}),e.jsx(Le,{label:"Delivery",score:a.delivery_score,icon:yt}),e.jsx(Le,{label:"Engagement",score:a.engagement_score,icon:Ge})]})]})]}),e.jsxs(C,{children:[e.jsx(T,{className:"pb-3",children:e.jsxs(R,{className:"text-sm font-medium flex items-center gap-2",children:[e.jsx(ve,{className:"h-4 w-4"}),"Financial Summary"]})}),e.jsx(S,{children:e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Revenue YTD"}),e.jsxs("p",{className:"text-lg font-semibold",children:["$",Number(a.revenue_ytd).toLocaleStr
17ing()]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Revenue Previous Year"}),e.jsxs("p",{className:"text-lg font-semibold",children:["$",Number(a.revenue_previous_year).toLocaleString()]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Growth Rate"}),e.jsxs("p",{className:L("text-lg font-semibold",a.growth_rate>0?"text-success":a.growth_rate<0?"text-destructive":""),children:[a.growth_rate>0?"+":"",a.growth_rate,"%"]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Avg Project Margin"}),e.jsxs("p",{className:"text-lg font-semibold",children:[a.avg_project_margin,"%"]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Avg Payment Days"}),e.jsxs("p",{className:"text-lg font-semibold",children:[a.avg_payment_days," days"]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Active / Total Projects"}),e.jsxs("p",{className:"text-lg font-semibold",children:[a.active_projects_count," / ",a.total_projects_count]})]})]})})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[a.risk_factors.length>0&&e.jsxs(C,{children:[e.jsx(T,{className:"pb-2",children:e.jsxs(R,{className:"text-sm font-medium flex items-center gap-2 text-destructive",children:[e.jsx(me,{className:"h-4 w-4"}),"Risk Factors"]})}),e.jsx(S,{children:e.jsx("ul",{className:"space-y-1.5",children:a.risk_factors.map((c,d)=>e.jsxs("li",{className:"text-xs flex items-start gap-2",children:[e.jsx("span",{className:"h-1.5 w-1.5 rounded-full bg-destructive mt-1.5 shrink-0"}),c]},d))})})]}),a.opportunities.length>0&&e.jsxs(C,{children:[e.jsx(T,{className:"pb-2",children:e.jsxs(R,{className:"text-sm font-medium flex items-center gap-2 text-success",children:[e.jsx(fs,{className:"h-4 w-4"}),"Opportunities"]})}),e.jsx(S,{children:e.jsx("ul",{className:"space-y-1.5",children:a.opportunities.map((c,d)=>e.jsxs("li",{className:"text-xs flex items-start gap-2",children:[e.jsx("span",{className:"h-1.5 w-1.5 rounded-full bg-success mt-1.5 shrink-0"}),c]},d))})})]})]}),a.ai_summary&&e.jsxs(C,{children:[e.jsx(T,{className:"pb-2",children:e.jsx(R,{className:"text-sm font-medium",children:"AI Summary"})}),e.jsx(S,{children:e.jsx("p",{className:"text-sm text-muted-foreground whitespace-pre-wrap",children:a.ai_summary})})]})]})}function sa({clientId:t,tenantId:u}){const[a,n]=o.useState(null),[x,j]=o.useState(!0);o.useEffect(()=>{let r=!1;return(async()=>await c(()=>r))(),()=>{r=!0}},[t,u]);const c=async r=>{try{const f=(await _.from("projects").select("id, name, budget, budget_used, budget_hours").eq("tenant_id",u).eq("client_id",t).limit(50)).data||[],w=f.map(v=>v.id);if(w.length===0){r()||(n({totalHours:0,thisMonthHours:0,lastMonthHours:0,avgWeeklyHours:0,burnRate:0,totalBudgetHours:0,budgetUsedPct:0,byProject:[]}),j(!1));return}const l=await _.from("time_entries").select("project_id, hours, date").in("project_id",w).limit(1e4),m=new Set(w),N=(l.data||[]).filter(v=>v.project_id&&m.has(v.project_id)),y=new Date,V=new Date(y.getFullYear(),y.getMonth(),1).toISOString().split("T")[0],z=new Date(y.getFullYear(),y.getMonth()-1,1).toISOString().split("T")[0],P=new Date(y.getFullYear(),y.getMonth(),0).toISOString().split("T")[0],X=new Date(Date.now()-28*864e5).toISOString().split("T")[0];let Y=0,$=0,G=0,b=0;const E=y.toISOString().split("T")[0],F=new Map;for(const v of N){const U=v.hours||0;Y+=U,F.set(v.project_id,(F.get(v.project_id)||0)+U),v.date&&v.date>=V&&v.date<=E&&($+=U),v.date&&v.date>=z&&v.date<=P&&(G+=U),v.date&&v.date>=X&&(b+=U)}const p=b/4,D=f.reduce((v,U)=>v+(U.budget_hours||0),0),k=D>0?Y/D*100:0,B=f.length>0?f.reduce((v,U)=>v+(U.budget||0),0)/Math.max(Y,1):0,I=p*B,O=f.map(v=>({name:v.name,hours:F.get(v.id)||0,budget:v.budget_hours||null})).filter(v=>v.hours>0).sort((v,U)=>U.hours-v.hours).slice(0,5);r()||n({totalHours:Y,thisMonthHours:$,lastMonthHours:G,avgWeeklyHours:p,burnRate:I,totalBudgetHours:D,budgetUsedPct:k,byProject:O})}catch(h){console.error("Error loading client time data:",h)}finally{r()||j(!1)}};if(x)return e.jsxs(C,{children:[e.jsx(T,{className:"pb-2",children:e.jsxs(R,{className:"text-base font-semibold flex items-center gap-2",children:[e.jsx(ds,{className:"h-4 w-4"})," Time Tracking"]})}),e.jsxs(S,{className:"space-y-3",children:[e.jsx(je,{className:"h-16 w-full"}),e.jsx(je,{className:"h-20 w-full"})]})]});if(!a)return null;const d=a.lastMonthHours>0?(a.thisMonthHours-a.lastMonthHours)/a.lastMonthHours*100:0;return e.jsxs(C,{children:[e.jsx(T,{className:"pb-2",children:e.jsxs(R,{className:"text-base font-semibold flex items-center gap-2",children:[e.jsx(ds,{className:"h-4 w-4"}),"Time Tracking",a.budgetUsedPct>90&&e.jsx(M,{variant:"destructive",className:"text-caption",children:"Over budget"})]})}),e.jsxs(S,{className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-3 gap-3 text-center",children:[e.jsxs("div",{children:[e.jsx(H,{size:"md",children:a.totalHours.toFixed(0)}),e.jsx("p",{className:"text-caption text-muted-foreground",children:"Total Hours"})]}),e.jsxs("div",{children:[e.jsx(H,{size:"md",children:a.thisMonthHours.toFixed(0)}),e.jsxs("p",{className:"text-caption text-muted-foreground flex items-center justify-center gap-1",children:["This Month",d!==0&&(d>0?e.jsx(le,{className:"h-3 w-3 text-success"}):e.jsx(ts,{className:"h-3 w-3 text-destructive"}))]})]}),e.jsxs("div",{children:[e.jsx(H,{size:"md",children:a.avgWeeklyHours.toFixed(1)}),e.jsx("p",{className:"text-caption text-muted-foreground",children:"Avg/Week"})]})]}),a.totalBudgetHours>0&&e.jsxs("div",{className:"space-y-1",children:[e.jsxs("div",{className:"flex justify-between text-xs",children:[e.jsxs("span",{children:["Budget: ",a.totalHours.toFixed(0),"/",a.totalBudgetHours.toFixed(0)," hrs"]}),e.jsxs("span",{className:a.budgetUsedPct>90?"text-destructive font-medium":"",children:[a.budgetUsedPct.toFixed(0),"%"]})]}),e.jsx(ws,{value:Math.min(a.budgetUsedPct,100),className:"h-2"})]}),a.burnRate>0&&e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(ve,{className:"h-3 w-3 text-muted-foreground"}),e.jsx("span",{className:"text-muted-foreground",children:"Burn rate:"}),e.jsxs("span",{className:"font-medium",children:[Js(a.burnRate),"/week"]})]}),a.byProject.length>0&&e.jsxs("div",{className:"space-y-2",children:[e.jsxs("p",{className:"text-xs font-medium text-muted-foreground flex items-center gap-1",children:[e.jsx(wt,{className:"h-3 w-3"})," Hours by Project"]}),a.byProject.map(r=>e.jsxs("div",{className:"flex items-center justify-between text-sm",children:[e.jsx("span",{className:"truncate max-w-[60%]",children:r.name}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("span",{className:"font-mono text-xs",children:[r.hours.toFixed(1),"h"]}),r.budget&&r.budget>0&&e.jsxs("span",{className:"text-caption text-muted-foreground",children:["/",r.budget.toFixed(0),"h"]})]})]},r.name))]})]})]})}const ps={invoice:e.jsx(Tt,{className:"h-4 w-4"}),contract:e.jsx(Rt,{className:"h-4 w-4"}),proposal:e.jsx(ne,{className:"h-4 w-4"}),deliverable:e.jsx(Et,{className:"h-4 w-4"}),other:e.jsx(ne,{className:"h-4 w-4"})},js={invoice:"Invoice",contract:"Contract",proposal:"Proposal",deliverable:"Deliverable",correspondence:"Correspondence",other:"Other"};function kr(){const{id:t}=Os(),u=qs(),{toast:a}=ce(),{currentTenant:n}=Fe(),{data:x,isLoading:j}
17=st(t),[c,d]=o.useState(!0),[r,h]=o.useState(null),[f,w]=o.useState([]),[l,m]=o.useState([]),[N,y]=o.useState([]),[V,z]=o.useState([]),[P,X]=o.useState(null),[Y,$]=o.useState(null),[G,b]=o.useState(!1),[E,F]=o.useState(!1),[p,D]=o.useState(!1),[k,B]=o.useState({name:"",description:"",document_type:"other",invoice_number:"",invoice_amount:"",invoice_date:""}),[I,O]=o.useState({name:"",title:"",email:"",phone:"",role:"contact",notes:""}),[v,U]=o.useState(!1),[oe,as]=o.useState(null),[_s,rs]=o.useState(!1);o.useEffect(()=>{t&&te()},[t]);const ns=async s=>{if(r)try{const{error:i}=await _.from("clients").update(s).eq("id",r.id);if(i)throw i;h(J=>J?{...J,...s}:null),a({title:"Client updated"})}catch(i){throw a({title:"Error updating client",description:i.message,variant:"destructive"}),i}},te=async()=>{if(t){d(!0);try{const{data:s,error:i}=await _.from("clients").select("*").eq("id",t).single();if(i)throw i;h(s);const J=new Date;J.setDate(1);const Me=J.toISOString().split("T")[0],[ye,Is,Ms,As,Bs,Hs]=await Promise.all([_.from("projects").select("*").eq("client_id",t).order("created_at",{ascending:!1}),_.from("proposals").select("*").eq("client_id",t).order("created_at",{ascending:!1}),_.from("client_documents").select("*").eq("client_id",t).order("created_at",{ascending:!1}),_.from("client_contacts").select("*").eq("client_id",t).order("is_primary",{ascending:!1}),_.from("company_intelligence").select("*").eq("entity_id",t).eq("entity_type","client").maybeSingle(),_.from("time_entries").select("hours, is_billable, date").eq("client_id",t).limit(1e4)]);w(ye.data||[]),m(Is.data||[]),y(Ms.data||[]),z(As.data||[]),X(Bs.data);const Ae=Hs.data||[],Ls=Ae.reduce((Z,xe)=>Z+(xe.hours||0),0),zs=Ae.filter(Z=>Z.is_billable).reduce((Z,xe)=>Z+(xe.hours||0),0),Us=Ae.filter(Z=>Z.date>=Me).reduce((Z,xe)=>Z+(xe.hours||0),0);$({totalHours:Ls,thisMonthHours:Us,billableHours:zs})}catch(s){console.error("Error loading client:",s),a({title:"Error loading client",variant:"destructive"})}finally{d(!1)}}},Cs=s=>{const i=s.target.files?.[0];i&&(as(i),B(J=>({...J,name:i.name})))},Ss=async()=>{if(!(!oe||!t||!k.name)){U(!0);try{const s=oe.name.split(".").pop(),i=`client-documents/${t}/${Date.now()}.${s}`,{error:J}=await _.storage.from("documents").upload(i,oe);if(J)throw J;const{data:{publicUrl:Me}}=_.storage.from("documents").getPublicUrl(i),{error:ye}=await _.from("client_documents").insert({client_id:t,tenant_id:n?.id,name:k.name,description:k.description||null,document_type:k.document_type,file_url:Me,file_size:oe.size,mime_type:oe.type,invoice_number:k.invoice_number||null,invoice_date:k.invoice_date||null,invoice_amount:k.invoice_amount?parseFloat(k.invoice_amount):null});if(ye)throw ye;a({title:"Document uploaded successfully"}),b(!1),as(null),B({name:"",description:"",document_type:"other",invoice_number:"",invoice_amount:"",invoice_date:""}),te()}catch(s){console.error("Error uploading document:",s),a({title:"Error uploading document",variant:"destructive"})}finally{U(!1)}}},ks=async()=>{if(!(!t||!I.name))try{const{error:s}=await _.from("client_contacts").insert({client_id:t,tenant_id:n?.id,name:I.name,title:I.title||null,email:I.email||null,phone:I.phone||null,role:I.role,notes:I.notes||null});if(s)throw s;a({title:"Contact added successfully"}),F(!1),O({name:"",title:"",email:"",phone:"",role:"contact",notes:""}),te()}catch(s){console.error("Error adding contact:",s),a({title:"Error adding contact",variant:"destructive"})}},Ps=async s=>{try{const{error:i}=await _.from("client_documents").update({shared_in_portal:!s.shared_in_portal}).eq("id",s.id);if(i)throw i;a({title:s.shared_in_portal?"Removed from client portal":"Shared to client portal"}),te()}catch{a({title:"Error updating document",variant:"destructive"})}},Ds=async s=>{try{const{error:i}=await _.from("client_documents").delete().eq("id",s);if(i)throw i;a({title:"Document deleted"}),te()}catch{a({title:"Error deleting document",variant:"destructive"})}},Ts=async s=>{try{const{error:i}=await _.from("client_contacts").delete().eq("id",s);if(i)throw i;a({title:"Contact deleted"}),te()}catch{a({title:"Error deleting contact",variant:"destructive"})}},Rs=async()=>
17{if(!(!t||!n?.id)){rs(!0);try{const{data:s,error:i}=await _.from("proposals").insert({tenant_id:n.id,client_id:t,title:`${r?.name||"New"} SOW`,status:"draft",proposal_status:"draft"}).select().single();if(i)throw i;a({title:"SOW created",description:"Opening SOW builder..."}),u(`/proposals/${s.id}`)}catch(s){console.error("Error creating SOW:",s),a({title:"Error creating SOW",variant:"destructive"})}finally{rs(!1)}}};if(c)return e.jsxs("div",{className:"container py-6 space-y-6",children:[e.jsx(je,{className:"h-10 w-48"}),e.jsx(je,{className:"h-64 w-full"}),e.jsx(je,{className:"h-96 w-full"})]});if(!r)return e.jsxs("div",{className:"container py-6 text-center",children:[e.jsx("p",{className:"text-muted-foreground",children:"Client not found"}),e.jsx(g,{variant:"outline",className:"mt-4",onClick:()=>u("/clients"),children:"Back to Clients"})]});const Es=N.filter(s=>s.document_type==="invoice"),Fs=N.filter(s=>s.document_type==="contract");Es.reduce((s,i)=>s+(i.invoice_amount||0),0);const Q=t?x?.get(t):void 0;return e.jsxs("div",{className:"container py-6 space-y-6",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx(g,{variant:"ghost",size:"icon",onClick:()=>u("/clients"),"aria-label":"Back",children:e.jsx(St,{className:"h-5 w-5"})}),e.jsx(Yt,{client:r,onUpdate:ns,onCreateSOW:Rs,isCreatingSOW:_s,onRefresh:te})]}),e.jsxs("div",{className:"grid gap-4 md:grid-cols-2 lg:grid-cols-4",children:[e.jsxs(C,{children:[e.jsx(T,{className:"pb-2",children:e.jsxs(R,{className:"text-sm font-medium text-muted-foreground flex items-center gap-2",children:[e.jsx(ve,{className:"h-4 w-4"}),"Revenue YTD"]})}),e.jsxs(S,{children:[e.jsxs(H,{size:"lg",children:["$",((Q?.revenueYTD||0)/1e3).toFixed(0),"K"]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["$",((Q?.totalInvoiced||0)/1e3).toFixed(0),"K invoiced"]})]})]}),e.jsxs(C,{children:[e.jsx(T,{className:"pb-2",children:e.jsxs(R,{className:"text-sm font-medium text-muted-foreground flex items-center gap-2",children:[e.jsx(Ns,{className:"h-4 w-4"}),"Outstanding A/R"]})}),e.jsxs(S,{children:[e.jsxs(H,{size:"lg",children:["$",((Q?.outstandingAR||0)/1e3).toFixed(0),"K"]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["$",((Q?.overdueAR||0)/1e3).toFixed(0),"K overdue"]})]})]}),e.jsxs(C,{children:[e.jsx(T,{className:"pb-2",children:e.jsxs(R,{className:"text-sm font-medium text-muted-foreground flex items-center gap-2",children:[e.jsx(kt,{className:"h-4 w-4"}),"Gross Margin"]})}),e.jsxs(S,{children:[e.jsxs(H,{size:"lg",children:[(Q?.grossMarginPercent||0).toFixed(0),"%"]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["$",((Q?.grossProfit||0)/1e3).toFixed(0),"K profit"]})]})]}),e.jsxs(C,{children:[e.jsx(T,{className:"pb-2",children:e.jsxs(R,{className:"text-sm font-medium text-muted-foreground flex items-center gap-2",children:[e.jsx(ms,{className:"h-4 w-4"}),"Projects"]})}),e.jsxs(S,{children:[e.jsx(H,{size:"lg",children:f.length}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[f.filter(s=>s.status==="active").length," active"]})]})]})]}),e.jsxs("div",{className:"grid gap-4 md:grid-cols-4",children:[e.jsxs(C,{children:[e.jsx(T,{className:"pb-2",children:e.jsxs(R,{className:"text-sm font-medium text-muted-foreground flex items-center gap-2",children:[e.jsx(ne,{className:"h-4 w-4"}),"Proposals"]})}),e.jsxs(S,{children:[e.jsx(H,{size:"lg",children:l.length}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["$",l.reduce((s,i)=>s+(i.total_value||0),0).toLocaleString()," total value"]})]})]}),e.jsxs(C,{children:[e.jsx(T,{className:"pb-2",children:e.jsxs(R,{className:"text-sm font-medium text-muted-foreground flex items-center gap-2",children:[e.jsx(le,{className:"h-4 w-4"}),"Realization"]})}),e.jsxs(S,{children:[e.jsxs(H,{size:"lg",children:[(Q?.realizationRate||100).toFixed(0),"%"]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[(Q?.billedHours||0).toFixed(0),"h billed / ",(Q?.billableHours||0).toFixed(0),"h worked"]})]})]}),e.jsxs(C,{children:[e.jsx(T,{className:"pb-2",children:e.jsxs(R,{className:"text-sm font-medium text-muted-foreground flex items-center gap-2",children:[e.jsx(ve,{className:"h-4 w-4"}),"Labor Cost"]})}),e.jsxs(S,{children:[e.jsxs(H,{size:"lg",children:["$",((Q?.laborCost||0)/1e3).toFixed(0),"K"]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"YTD cost basis"})]})]}),e.jsxs(C,{children:[e.jsx(T,{className:"pb-2",children:e.jsxs(R,{className:"text-sm font-medium text-muted-foreground flex items-center gap-2",children:[e.jsx(Ge,{className:"h-4 w-4"}),"Health Score"]})}),e.jsxs(S,{children:[e.jsxs(H,{size:"lg",children:[r.health_score||0,"/100"]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[Fs.length," active contracts"]})]})]})]}),e.jsxs(Xs,{defaultValue:"overview",className:"space-y-4",children:[e.jsxs(Zs,{children:[e.jsx(he,{value:"overview",children:"Overview"}),e.jsxs(he,{value:"projects",children:["Projects (",f.length,")"]}),e.jsxs(he,{value:"documents",children:["Documents (",N.length,")"]}),e.jsxs(he,{value:"contacts",children:["Contacts (",V.length,")"]}),e.jsx(he,{value:"intel",children:"Intelligence"})]}),e.jsxs(ue,{value:"overview",className:"space-y-4",children:[e.jsxs("div",{className:"grid gap-4 md:grid-cols-2",children:[e.jsx(Jt,{client:r,onUpdate:ns}),e.jsxs(C,{children:[e.jsx(T,{children:e.jsx(R,{className:"text-lg",children:"Recent Projects"})}),e.jsxs(S,{children:[f.slice(0,5).map(s=>e.jsxs(be,{to:`/projects/${s.id}`,className:"flex items-center gap-3 py-2 hover:bg-muted/50 rounded-md px-2 -mx-2",children:[e.jsx(ms,{className:"h-4 w-4 text-muted-foreground"}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-sm font-medium truncate",children:s.name}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[s.status," • $",((s.budget||0)/1e3).toFixed(0),"K"]})]}),e.jsx(M,{variant:"outline",className:"text-xs",children:pe(s.health||"on_track")})]},s.id)),f.length===0&&e.jsx("p",{className:"text-sm text-muted-foreground text-center py-4",children:"No projects yet"})]})]})]}),n?.id&&e.jsx(sa,{clientId:t,tenantId:n.id}),e.jsx(Mt,{clientId:t})]}),e.jsx(ue,{value:"projects",className:"space-y-4",children:e.jsxs(C,{children:[e.jsxs(T,{className:"flex flex-row items-center justify-between",children:[e.jsx(R,{children:"Projects"}),e.jsxs(g,{size:"sm",onClick:()=>u(`/projects?client_id=${t}`),children:[e.jsx(Re,{className:"h-4 w-4 mr-1"}),"New Project"]})]}),e.jsx(S,{children:e.jsxs(is,{children:[e.jsx(ls,{children:e.jsxs(de,{children:[e.jsx(K,{children:"Project"}),e.jsx(K,{children:"Status"}),e.jsx(K,{children:"Health"}),e.jsx(K,{className:"text-right",children:"Budget"}),e.jsx(K,{children:"Dates"}),e.jsx(K,{})]})}),e.jsxs(cs,{children:[f.map(s=>e.jsxs(de,{children:[e.jsxs(q,{children:[e.jsx(be,{to:`/projects/${s.id}`,className:"font-medium hover:underline",children:s.name}),s.description&&e.jsx("p",{className:"text-xs text-muted-foreground truncate max-w-xs",children:s.description})]}),e.jsx(q,{children:e.jsx(M,{variant:"outline",children:pe(s.status)})}),e.jsx(q,{children:e.jsx(M,{variant:s.health==="at_risk"?"destructive":"secondary",children:pe(s.health||"on_track")})}),e.jsxs(q,{className:"text-right",children:["$",((s.budget||0)/1e3).toFixed(0),"K"]}),e.jsxs(q,{className:"text-sm text-muted-foreground",children:[s.start_date&&_e(new Date(s.start_date),"MMM d")," -",s.end_date&&_e(new Date(s.end_date),"MMM d, yyyy")]}),e.jsx(q,{children:e.jsx(g,{variant:"ghost",size:"icon",asChild:!0,"aria-label":"Copy link",children:e.jsx(be,{to:`/projects/${s.id}`,children:e.jsx(Pt,{className:"h-4 w-4"})})})})]},s.id)),f.length===0&&e.jsx(de,{children:e.jsx(q,{colSpan:6,className:"text-center py-8 text-muted-foreground",children:"No projects found"})})]})]})})]})}),e.jsx(ue,{value:"documents",className:"space-y-4",children:e.jsxs(C,{children:[e.jsxs(T,{className:"flex flex-row items-center justify-between",children:[e.jsxs("div",{children:[e.jsx(R,{children:"Documents"}),e.jsx(re,{children:"Contracts, invoices, and other files"})]}),e.jsxs(g,{size:"sm",onClick:()=>b(!0),children:[e.jsx(Ee,{className:"h-4 w-4 mr-1"}),"Upload"]})]}),e.jsx(S,{children:e.jsxs(is,{children:[e.jsx(ls,{children:e.jsxs(de,{children:[e.jsx(K,{children:"Name"}),e.jsx(K,{children:"Type"}),e.jsx(K,{children:"Details"}),e.jsx(K,{children:"Date"}),e.jsx(K,{})]})}),e.jsxs(cs,{children:[N.map(s=>e.jsxs(de,{children:[e.jsxs(q,{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[ps[s.document_type]||ps.other,e.jsx("span",{className:"font-medium",children:s.name}
17),s.shared_in_portal&&e.jsx(M,{variant:"secondary",className:"text-xs",children:"Shared in portal"})]}),s.description&&e.jsx("p",{className:"text-xs text-muted-foreground ml-6",children:s.description})]}),e.jsx(q,{children:e.jsx(M,{variant:"outline",children:js[s.document_type]||s.document_type})}),e.jsxs(q,{className:"text-sm text-muted-foreground",children:[s.document_type==="invoice"&&s.invoice_amount&&e.jsxs("span",{children:["$",s.invoice_amount.toLocaleString()]}),s.invoice_number&&e.jsxs("span",{className:"ml-2",children:["#",s.invoice_number]})]}),e.jsx(q,{className:"text-sm text-muted-foreground",children:_e(new Date(s.created_at),"MMM d, yyyy")}),e.jsx(q,{children:e.jsxs(fe,{children:[e.jsx(ge,{asChild:!0,children:e.jsx(g,{variant:"ghost",size:"icon","aria-label":"More options",children:e.jsx(xs,{className:"h-4 w-4"})})}),e.jsxs(Ne,{align:"end",children:[e.jsx(ee,{asChild:!0,children:e.jsxs("a",{href:s.file_url,target:"_blank",rel:"noopener noreferrer",children:[e.jsx(et,{className:"h-4 w-4 mr-2"}),"View"]})}),e.jsx(ee,{asChild:!0,children:e.jsxs("a",{href:s.file_url,download:!0,children:[e.jsx(es,{className:"h-4 w-4 mr-2"}),"Download"]})}),e.jsxs(ee,{onClick:()=>Ps(s),children:[e.jsx(bs,{className:"h-4 w-4 mr-2"}),s.shared_in_portal?"Unshare from portal":"Share in portal"]}),e.jsxs(ee,{className:"text-destructive",onClick:()=>Ds(s.id),children:[e.jsx(We,{className:"h-4 w-4 mr-2"}),"Delete"]})]})]})})]},s.id)),N.length===0&&e.jsx(de,{children:e.jsx(q,{colSpan:5,className:"text-center py-8 text-muted-foreground",children:"No documents uploaded yet"})})]})]})})]})}),e.jsx(ue,{value:"contacts",className:"space-y-4",children:e.jsxs(C,{children:[e.jsxs(T,{className:"flex flex-row items-center justify-between",children:[e.jsxs("div",{children:[e.jsx(R,{children:"Contacts"}),e.jsx(re,{children:"Key stakeholders and contacts"})]}),e.jsxs(g,{size:"sm",onClick:()=>F(!0),children:[e.jsx(Re,{className:"h-4 w-4 mr-1"}),"Add Contact"]})]}),e.jsx(S,{children:e.jsxs("div",{className:"grid gap-4 md:grid-cols-2 lg:grid-cols-3",children:[V.map(s=>e.jsx(C,{className:"relative",children:e.jsxs(S,{className:"pt-6",children:[e.jsxs(fe,{children:[e.jsx(ge,{asChild:!0,children:e.jsx(g,{variant:"ghost",size:"icon",className:"absolute top-2 right-2","aria-label":"More options",children:e.jsx(xs,{className:"h-4 w-4"})})}),e.jsx(Ne,{align:"end",children:e.jsxs(ee,{className:"text-destructive",onClick:()=>Ts(s.id),children:[e.jsx(We,{className:"h-4 w-4 mr-2"}),"Delete"]})})]}),e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx(Qe,{children:e.jsx(Je,{children:s.name.substring(0,2)})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("p",{className:"font-medium",children:s.name}),s.is_primary&&e.jsx(M,{variant:"secondary",className:"text-xs",children:"Primary"})]}),s.title&&e.jsx("p",{className:"text-sm text-muted-foreground",children:s.title}),s.role&&e.jsx(M,{variant:"outline",className:"text-xs mt-1",children:s.role}),e.jsxs("div",{className:"mt-2 space-y-1 text-sm",children:[s.email&&e.jsxs("a",{href:`mailto:${s.email}`,className:"flex items-center gap-1 text-muted-foreground hover:text-primary",children:[e.jsx(vs,{className:"h-3 w-3"}),s.email]}),s.phone&&e.jsxs("a",{href:`tel:${s.phone}`,className:"flex items-center gap-1 text-muted-foreground hover:text-primary",children:[e.jsx(ys,{className:"h-3 w-3"}),s.phone]})]})]})]})]})},s.id)),V.length===0&&e.jsx("div",{className:"col-span-full text-center py-8 text-muted-foreground",children:"No contacts added yet"})]})})]})}),e.jsxs(ue,{value:"intel",className:"space-y-4",children:[e.jsxs("div",{className:"flex justify-end gap-2",children:[r&&e.jsx(Ot,{clientId:r.id,clientName:r.name}),e.jsxs(g,{variant:"outline",size:"sm",onClick:()=>D(!0),children:[e.jsx(Dt,{className:"h-4 w-4 mr-2"}),"Import from Granola"]})]}),r&&e.jsx(zt,{clientId:r.id,clientName:r.name,website:r.website,industry:r.industry}),r&&n?.id&&e.jsx(Ut,{clientId:r.id,tenantId:n.id}),r&&e.jsx(ea,{clientId:r.id}),r&&e.jsx(Lt,{clientId:r.id,clientName:r.name}),e.jsxs(C,{children:[e.jsxs(T,{children:[e.jsxs(R,{className:"flex items-center gap-2",children:[e.jsx(le,{className:"h-5 w-5"}),"Company Intelligence"]}),e.jsx(re,{children:P?"Scraped company data and insights":"No intelligence data available yet"})]}),e.jsx(S,{children:P?e.jsxs("div",{className:"space-y-6",children:[P.description&&e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium mb-1",children:"Description"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:P.description})]}),e.jsxs("div",{className:"grid gap-4 md:grid-cols-3",children:[P.estimated_revenue&&e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Est. Revenue"}),e.jsx("p",{className:"text-lg font-semibold",children:P.estimated_revenue})]}),P.estimated_headcount&&e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Est. Headcount"}),e.jsx("p",{className:"text-lg font-semibold",children:P.estimated_headcount})]}),P.industry&&e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Industry"}),e.jsx("p",{className:"text-lg font-semibold",children:tt(P.industry)})]})]}),P.strengths&&P.strengths.length>0&&e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium mb-2",children:"Strengths"}),e.jsx("div",{className:"flex flex-wrap gap-2",children:P.strengths.map((s,i)=>e.jsx(M,{variant:"secondary",children:String(s)},i))})]}),P.services&&P.services.length>0&&e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium mb-2",children:"Services"}),e.jsx("div",{className:"flex flex-wrap gap-2",children:P.services.map((s,i)=>e.jsx(M,{variant:"outline",children:String(s)},i))})]})]}):e.jsxs("div",{className:"text-center py-8",children:[e.jsx(Ke,{className:"h-12 w-12 text-muted-foreground
17/30 mx-auto mb-4"}),e.jsx("p",{className:"text-muted-foreground",children:"No intelligence data available"}),e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:"Visit the Intelligence page to scrape company data"})]})})]})]})]}),e.jsx(Ce,{open:G,onOpenChange:b,children:e.jsxs(Se,{children:[e.jsxs(ke,{children:[e.jsx(Pe,{children:"Upload Document"}),e.jsx(De,{children:"Add a document to this client's files"})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{children:"File"}),e.jsx(W,{type:"file",onChange:Cs})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{children:"Name"}),e.jsx(W,{value:k.name,onChange:s=>B(i=>({...i,name:s.target.value})),placeholder:"Document name"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{children:"Type"}),e.jsxs(Ue,{value:k.document_type,onValueChange:s=>B(i=>({...i,document_type:s})),children:[e.jsx(Oe,{children:e.jsx(qe,{})}),e.jsx(Ve,{children:Object.entries(js).map(([s,i])=>e.jsx(se,{value:s,children:i},s))})]})]}),k.document_type==="invoice"&&e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{children:"Invoice Number"}),e.jsx(W,{value:k.invoice_number,onChange:s=>B(i=>({...i,invoice_number:s.target.value})),placeholder:"INV-001"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{children:"Amount"}),e.jsx(W,{type:"number",value:k.invoice_amount,onChange:s=>B(i=>({...i,invoice_amount:s.target.value})),placeholder:"10000"})]}),e.jsxs("div",{className:"space-y-2 col-span-2",children:[e.jsx(A,{children:"Invoice Date"}),e.jsx(W,{type:"date",value:k.invoice_date,onChange:s=>B(i=>({...i,invoice_date:s.target.value}))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{children:"Description (optional)"}),e.jsx(Te,{value:k.description,onChange:s=>B(i=>({...i,description:s.target.value})),placeholder:"Brief description..."})]})]}),e.jsxs(ze,{children:[e.jsx(g,{variant:"outline",onClick:()=>b(!1),children:"Cancel"}),e.jsx(g,{onClick:Ss,disabled:v||!oe,children:v?"Uploading...":"Upload"})]})]})}),e.jsx(Ce,{open:E,onOpenChange:F,children:e.jsxs(Se,{children:[e.jsxs(ke,{children:[e.jsx(Pe,{children:"Add Contact"}),e.jsx(De,{children:"Add a new contact for this client"})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{children:"Name *"}),e.jsx(W,{value:I.name,onChange:s=>O(i=>({...i,name:s.target.value})),placeholder:"Full name"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{children:"Title"}),e.jsx(W,{value:I.title,onChange:s=>O(i=>({...i,title:s.target.value})),placeholder:"Job title"})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{children:"Email"}),e.jsx(W,{type:"email",value:I.email,onChange:s=>O(i=>({...i,email:s.target.value})),placeholder:"[email protected]"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{children:"Phone"}),e.jsx(W,{value:I.phone,onChange:s=>O(i=>({...i,phone:s.target.value})),placeholder:"+1 555-0100"})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{children:"Role"}),e.jsxs(Ue,{value:I.role,onValueChange:s=>O(i=>({...i,role:s})),children:[e.jsx(Oe,{children:e.jsx(qe,{})}),e.jsxs(Ve,{children:[e.jsx(se,{value:"decision_maker",children:"Decision Maker"}),e.jsx(se,{value:"champion",children:"Champion"}),e.jsx(se,{value:"technical",children:"Technical"}),e.jsx(se,{value:"finance",children:"Finance"}),e.jsx(se,{value:"executive",children:"Executive"}),e.jsx(se,{value:"contact",children:"Contact"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(A,{children:"Notes"}),e.jsx(Te,{value:I.notes,onChange:s=>O(i=>({...i,notes:s.target.value})),placeholder:"Any notes about this contact..."})]})]}),e.jsxs(ze,{children:[e.jsx(g,{variant:"outline",onClick:()=>F(!1),children:"Cancel"}),e.jsx(g,{onClick:ks,disabled:!I.name,children:"Add Contact"})]})]})}),e.jsx(Ct,{open:p,onOpenChange:D,linkedEntity:"client",linkedId:t,linkedName:r?.name})]})}export{kr 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.