1import{r as k,j as e,s as E,B as T,h as F}from"./index-Dqt4EOsp.js";import{C as A,b as M,c as O,a as $}from"./card-Ce6tErGP.js";import{T as L,a as D,b as g,c as d,d as S,e as c}from"./table-BF5WGBMC.js";import{c as B}from"./dateFormat-FKFAlDHJ.js";function N(a,n,x,p=!0){return F({queryKey:["admin-merchant-overview",a,n,x],enabled:p,staleTime:6e4,retry:1,queryFn:async({signal:j})=>{const u=await fetch(`/api/admin/merchants/${encodeURIComponent(a)}/overview/${n}?days=${x}`,{credentials:"include",signal:j});if(!u.ok)throw new Error("Unable to load this section");return u.json()}})}const _=a=>a?B(a):"Not recorded",f=(a,n="PKR")=>`${n} ${Number(a).toLocaleString(void 0,{maximumFractionDigits:2})}`,R=a=>a>=1048576?`${(a/1048576).toFixed(2)} MB`:`${(a/1024).toFixed(1)} KB`;function w({title:a,loading:n,error:x,retry:p,children:j}){return e.jsxs(A,{children:[e.jsx(M,{children:e.jsx(O,{children:a})}),e.jsx($,{children:n?e.jsx("p",{role:"status",className:"text-sm text-muted-foreground",children:"Loadingâ¦"}):x?e.jsxs("div",{role:"alert",className:"flex items-center justify-between gap-3",children:[e.jsx("p",{className:"text-sm",children:"This section is unavailable."}),e.jsx(T,{size:"sm",variant:"outline",onClick:p,children:"Retry"})]}):j})]})}function i({label:a,value:n}){return e.jsxs("div",{className:"min-w-0 rounded-xl border p-4",children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:a}),e.jsx("p",{className:"mt-2 text-lg font-semibold break-words tabular-nums",children:n})]})}function W({merchantId:a}){const[n,x]=k.useState(30),[p,j]=k.useState(!1),u=N(a,"connections",30),v=N(a,"usage",n),o=N(a,"dispatch",n),h=N(a,"storage",30,p),t=u.data,r=v.data,C=[["own_account","Own account · self-dispatch"],["master_self_dispatch","Master account · self-dispatch"],["master_warehouse","Master account · warehouse dispatch"]];return e.jsxs("div",{className:"space-y-4","data-testid":"merchant-operational-overview",children:[e.jsx(w,{title:"Connections",loading:u.isLoading,error:u.isError,retry:()=>u.refetch(),children:t&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid gap-3 md:grid-cols-3",children:[e.jsx(i,{label:"WhatsApp",value:t.merchant.wa_disconnected?"Disconnected":t.merchant.wa_configured&&t.merchant.wa_phone_number_id?t.merchant.wa_phone_registered?"Configured · phone registered":"Configured · registration unverified":"Not configured"}),e.jsx(i,{label:"Meta",value:t.meta.length?t.meta.map((s,m)=>e.jsxs("span",{className:"block text-sm",children:[s.context_key,": ",s.token_expires_at&&new Date(s.token_expires_at)<new Date?"Token expired":s.status," · ",s.health_status,e.jsxs("span",{className:"block text-xs font-normal text-muted-foreground",children:["Checked ",_(s.last_health_check_at)]})]},m)):t.merchant.facebook_oauth_connected||t.merchant.meta_configured?"Legacy configuration · unverified":"Not configured"}),e.jsx(i,{label:"RoboCall",value:t.robocall.enabled?t.robocall.platform_configured?"Enabled · platform configured":t.robocall.legacy_configured?"Enabled · legacy configured":"Enabled · credentials missing":"Access disabled"})]}),e.jsx("p",{className:"mt-3 text-xs text-muted-foreground",children:"Stored connection status, not a live provider test. Credentials are never displayed."}),e.jsx("h3",{className:"mt-5 mb-2",children:"Courier accounts"}),t.couriers.length?e.jsx("ul",{className:"divide-y",children:t.couriers.map((s,m)=>e.jsxs("li",{className:"flex flex-wrap justify-between gap-2 py-2 text-sm",children:[e.jsxs("span",{className:"capitalize",children:[s.courier_name," ",e.jsxs("span",{className:"text-muted-foreground",children:["· ",s.account_type]})]}),e.jsx(E,{variant:"outline",children:s.status})]},m))}):e.jsx("p",{className:"text-sm text-muted-foreground",children:"No merchant courier accounts or master self-dispatch registrations recorded."})]})}),e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-3",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Dispatch and consumption for the selected period."}),e.jsxs("label",{className:"flex items-center gap-2 text-sm",children:["Period ",e.jsxs("select",{"aria-label":"Merchant reporting period",className:"rounded-lg border bg-card p-2",value:n,onChange:s=>x(Number(s.target.value)),children:[e.jsx("option",{value:7,children:"Last 7 days"}),e.jsx("option",{value:30,children:"Last 30 days"}),e.jsx("option",{value:90,children:"Last 90 days"})]})]})]}),e.jsx(w,{title:"Dispatch & COD",loading:o.isLoading,error:o.isError,retry:()=>o.refetch(),children:o.data&&e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"grid gap-3 lg:grid-cols-3",children:C.map(([s,m])=>{const b=o.data.rows.filter(l=>l.source===s);return e.jsxs("div",{className:"rounded-xl border p-4",children:[e.jsx("h3",{children:m}),e.jsxs("p",{className:"my-2 text-xl font-semibold tabular-nums",children:[b.reduce((l,y)=>
1l+y.shipments,0).toLocaleString()," shipments"]}),e.jsxs("p",{className:"text-sm",children:["Total COD ",e.jsx("strong",{children:f(b.reduce((l,y)=>l+Number(y.cod),0))})]}),e.jsx("ul",{className:"mt-3 space-y-2",children:b.map(l=>e.jsxs("li",{className:"text-xs text-muted-foreground capitalize",children:[l.courier," · ",l.shipments," shipments · ",f(l.cod)]},l.courier))})]},s)})}),e.jsx("h3",{className:"mt-5 mb-2",children:"Recent dispatch lots"}),e.jsx("p",{className:"mb-3 text-xs text-muted-foreground",children:"Latest 25 loadsheet groups in this period. Shipments without a loadsheet are grouped as Unbatched."}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs(L,{children:[e.jsx(D,{children:e.jsxs(g,{children:[e.jsx(d,{children:"Lot / loadsheet"}),e.jsx(d,{children:"Dispatch group"}),e.jsx(d,{children:"Courier"}),e.jsx(d,{children:"Shipments"}),e.jsx(d,{children:"Total COD"})]})}),e.jsx(S,{children:o.data.lots.length?o.data.lots.map((s,m)=>e.jsxs(g,{children:[e.jsx(c,{children:s.batch||"Unbatched"}),e.jsx(c,{children:C.find(([b])=>b===s.source)?.[1]||s.source}),e.jsx(c,{className:"capitalize",children:s.courier}),e.jsx(c,{children:s.shipments}),e.jsx(c,{className:"whitespace-nowrap",children:f(s.cod)})]},m)):e.jsx(g,{children:e.jsx(c,{colSpan:5,children:"No dispatch lots in this period."})})})]})}),e.jsx("p",{className:"mt-3 text-xs text-muted-foreground",children:"Dispatch groups, split by courier. COD is booked value, not money received or merchant payout. Master totals use the canonical courier ledger; own-account totals use tracked, non-cancelled self-dispatch bookings. Reporting dates use the ledger dispatch date or own-account booking date."}),o.data.unclassified>0&&e.jsxs("p",{className:"mt-2 text-sm text-amber-700 dark:text-amber-300",children:[o.data.unclassified," bookings have unknown account ownership and are excluded from these groups."]})]})}),e.jsx(w,{title:"RoboCall & WhatsApp consumption",loading:v.isLoading,error:v.isError,retry:()=>v.refetch(),children:r&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid gap-3 sm:grid-cols-2 lg:grid-cols-4",children:[e.jsx(i,{label:"Calls sent successfully",value:r.calls.sent.toLocaleString()}),e.jsx(i,{label:"Calls answered",value:r.calls.answered.toLocaleString()}),e.jsx(i,{label:"Answered duration",value:`${(Number(r.calls.answered_seconds)/60).toFixed(1)} min`}),e.jsx(i,{label:"Recorded RoboCall charges",value:r.charges.length?r.charges.map(s=>e.jsx("span",{className:"block",children:f(s.amount,s.currency)},s.currency)):"No recorded charges"})]}),e.jsxs("div",{className:"mt-3 grid gap-3 sm:grid-cols-2 lg:grid-cols-4",children:[e.jsx(i,{label:"WhatsApp sent",value:r.whatsapp.sent.toLocaleString()}),e.jsx(i,{label:"Delivered / read",value:r.whatsapp.delivered.toLocaleString()}),e.jsx(i,{label:"Received",value:r.whatsapp.received.toLocaleString()}),e.jsx(i,{label:"Failed sends",value:r.whatsapp.failed.toLocaleString()})]}),e.jsx("p",{className:"mt-3 text-xs text-muted-foreground",children:"WhatsApp counts cover messages recorded in 1SOL; delivered/read is a subset of sent. Provider charges are not available. RoboCall charges come from the billing ledger by answer date; call counts use initiation date. A missing charge is not treated as free."}),e.jsxs("p",{className:"mt-3 text-sm text-muted-foreground",children:[r.calls.failed_sends," failed sends · ",r.calls.unverified," pending / unverified. Successful means a provider call ID was recorded, regardless of whether the recipient answered."]}),e.jsx("h3",{className:"mt-5 mb-2",children:"Recent successfully sent calls"}),e.jsx("p",{className:"mb-3 text-xs text-muted-foreground",children:"Latest 10 calls accepted by the provider in this period. Answering and order confirmation are separate outcomes."}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs(L,{children:[e.jsx(D,{children:e.jsxs(g,{children:[e.jsx(d,{children:"Order"}),e.jsx(d,{children:"Sent request"}),e.jsx(d,{children:"Provider ID / outcome"}),e.jsx(d,{children:"Duration"}),e.jsx(d,{children:"Recorded charge"})]})}),e.jsx(S,{children:r.recentCalls.length?r.recentCalls.map(s=>e.jsxs(g,{children:[e.jsx(c,{children:s.order_number||"â"}),e.jsx(c,{className:"whitespace-nowrap",children:_(s.created_at)}),e.jsxs(c,{children:[s.call_id,e.jsx("span",{className:"block text-xs text-muted-foreground",children:s.outcome})]}),e.jsx(c,{children:s.duration_seconds==null?"Not recorded":`${s.duration_seconds}s`}),e.jsx(c,{children:s.charge_amount==null?"Not recorded":f(s.charge_amount,s.currency||"PKR")})]},s.id)):e.jsx(g,{children:e.jsx(c,{colSpan:5,children:"No successfully sent calls in this period."})})})]})})]})}),e.jsx(w,{title:"Database consumption",loading:h.isFetching,error:h.isError,retry:()=>h.refetch(),children:p?h.data&&e.jsxs(e.Fragment,{children:[e.jsxs("p",{className:"text-2xl font-semibold",children:["â ",R(h.data.estimatedBytes)]}),e.jsx("p",{className:"my-3 text-xs text-muted-foreground",children:h.data.scope}),e.jsx("ul",{className:"divide-y",children:h.data.tables.map(s=>e.jsxs("li",{className:"flex justify-between gap-3 py-2 text-sm",children:[e.jsx("span",{children:s.label}),e.jsxs("span",{children:["â ",R(s.estimatedBytes)]})]},s.label))}),e.jsxs("p",{className:"mt-3 text-xs text-muted-foreground",children:["Estimated ",_(h.data.measuredAt)]})]}):e.jsxs(e.Fragment,{children:[e.jsx("p",{className:"text-sm text-muted-foreground mb-3",children:"Estimate this merchantâs row data in six core tables without scanning their entire history. Shared database disk space cannot be attributed exactly."}),e.jsx(T,{variant:"outline",size:"sm",onClick:()=>j(!0),children:"Estimate database usage"})]})})]})}export{W as MerchantOverview};
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.