1import{x as Ve,k as Ue,i as Oe,r,j as e,L as le}from"./vendor-react-Bj9BTYWp.js";import{u as Ye,a as Qe,i as Ke,s as d,t as h,Y as f,B as We}from"./index-H_QQySEB.js";import{u as Xe}from"./use-smart-back-CKy-V0s1.js";import{I as de}from"./input-CFlk99Ir.js";import{T as Je}from"./textarea-DgCNsHcW.js";import{S as b}from"./skeleton-BXb5DO2C.js";import{a as He}from"./use-app-data-DrQ1z2OZ.js";import{L as U,c as Ze,j as et,V as tt,al as st,G as at,M as me,g as ue,Y as xe,a2 as rt,F as he,J as D,h as nt}from"./vendor-icons-CuDMhxxe.js";import"./vendor-supabase-Ch7gXvyu.js";function pe(s){if(!s)return"";const m=Date.now()-new Date(s).getTime(),i=Math.floor(m/6e4);if(i<1)return"just now";if(i<60)return`${i} minutes ago`;const x=Math.floor(i/60);if(x<24)return`${x} hours ago`;const u=Math.floor(x/24);return u===1?"yesterday":u<7?`${u} days ago`:u<30?`${Math.floor(u/7)} weeks ago`:`${Math.floor(u/30)} months ago`}function fe(s){if(!s)return"";const m=Date.now()-new Date(s).getTime();if(m<0)return"just now";const i=Math.floor(m/6e4);if(i<1)return"just now";if(i<60)return`${i}m ago`;const x=Math.floor(i/60);if(x<24)return`${x}h ago`;const u=Math.floor(x/24);if(u<30)return`${u}d ago`;const g=Math.floor(u/30);return g<12?`${g}mo ago`:`${Math.floor(g/12)}y ago`}const it={firecrawl:"Brand website",hunter:"Email database",database:"Records"},ot=/^(info|contact|hello|help|support|team|admin|office|sales|orders|shop|store|billing|careers|jobs|legal|privacy|service|customerservice|customer-service|enquiries|inquiries|feedback|hi|hey|general|reception|press|media|marketing|partner|partners|partnerships?|sponsors?|creators?|influencer|collabs?|pr)@/i;function ct(s){return ot.test(s)}const bt=()=>{const{brandId:s}=Ve(),{user:m}=Ye(),{activeProfileId:i}=Qe(),x=Ue(),u=Xe("/matches"),g=Ke(),k=Oe(),{data:ge}=He(),[n,be]=r.useState(null),[O,Y]=r.useState([]),[y,je]=r.useState(null),[T,Q]=r.useState(null),[ve,ye]=r.useState(null),[Ne,P]=r.useState(!1),[_e,we]=r.useState(!1),[ke,K]=r.useState("Checking brand website..."),[p,C]=r.useState(""),[S,I]=r.useState(""),[w,q]=r.useState(""),[z,W]=r.useState("professional"),[F,X]=r.useState("initial"),[Ce,B]=r.useState(!1),[J,H]=r.useState(!1),[Z,ee]=r.useState(null),[te,A]=r.useState(!1),[$,Se]=r.useState(0),R=3,[ze,se]=r.useState(!1),[M,$e]=r.useState(null),[Me,ae]=r.useState(!0),E=ge?.channel;r.useEffect(()=>{if(!s||!m)return;let t=!1;return A(!1),(async()=>{const{data:l}=await d.from("brands").select("id, name, logo_url, website, category, description, total_sponsorships, total_channels, median_channel_size, last_sponsorship_at").eq("id",s).single();if(t||!l){t||ae(!1);return}be(l),ae(!1);const{data:a}=await d.from("pipeline_entries").select("id, status, pitched_at").eq("profile_id",i).eq("brand_id",s).maybeSingle();if(t)return;if(a)Q(a.id),(a.status==="pitched"||a.status==="negotiating")&&X("followup");else{const{data:_}=await d.from("pipeline_entries").insert({user_id:m.id,profile_id:i,brand_id:s,status:"saved"}).select("id").single();if(t)return;_&&(Q(_.id),k.invalidateQueries({queryKey:["pipeline"]}),k.invalidateQueries({queryKey:["dashboard"]}))}const o=d.from("pitch_drafts").select("*").eq("profile_id",i).eq("brand_id",s).maybeSingle(),j=d.from("matches").select("score").eq("profile_id",i).eq("brand_id",s).maybeSingle(),N=d.from("user_channels").select("channel_id, channels(avg_views)").eq("profile_id",i).eq("is_primary",!0).limit(1),Fe=d.from("matches").select("brand_id, score, brands(name)").eq("profile_id",i).neq("brand_id",s).order("score",{ascending:!1}).limit(5),Be=d.from("pipeline_entries").select("brand_id").eq("profile_id",i).in("status",["pitched","negotiating","closed"]),[{data:v},{data:oe},{data:ce},{data:Ae},{data:Re}]=await Promise.all([o,j,N,Fe,Be]);if(t)return;v&&v.body&&(I(v.subject||""),q(v.body||""),W(v.tone||"professional"),X(v.pitch_type||"initial"),ee(v.updated_at),A(!0)),oe&&je(oe.score),ce?.[0]&&ye(ce[0].channels?.avg_views||null);const Ge=new Set((Re||[]).map(_=>_.brand_id)),L=(Ae||[]).find(_=>!Ge.has(_.brand_id));L&&$e({brand_id:L.brand_id,brand_name:L.brands?.name||"Unknown",score:L.score})})(),()=>{t=!0}},[s,m]);const Ee=r.useCallback(async()=>
1{if(!s||!n)return;P(!0),we(!0),K("Checking brand website...");const{data:t}=await d.from("brand_contacts").select("*").eq("brand_id",s).order("confidence",{ascending:!1});if(t&&t.length>0){const c=t.reduce((a,o)=>new Date(a.created_at)>new Date(o.created_at)?a:o);if(Date.now()-new Date(c.created_at).getTime()<30*24*60*60*1e3){Y(t),p||C(t.find(a=>a.contact_type==="partnership")?.email||t[0]?.email||""),P(!1);return}}K("Searching email databases...");try{const{data:c,error:l}=await d.functions.invoke("enrich-brand-contacts",{body:{brand_id:s}});if(l){let a=null;try{a=await l?.context?.json?.()}catch{}const o=l?.context?.status;a?.error==="daily_limit"||o===429?h({title:"Daily contact-discovery limit reached",description:"Try again tomorrow.",variant:"destructive"}):h({title:"Couldn't fetch contacts right now",description:"Try again in a minute.",variant:"destructive"})}else if(c?.contacts){Y(c.contacts);const a=c.contacts.find(o=>o.contact_type==="partnership")||c.contacts[0];p||C(a?.email||"")}}catch(c){console.error("Contact enrichment error:",c),h({title:"Couldn't fetch contacts right now",description:"Try again in a minute.",variant:"destructive"})}P(!1)},[s,n,p]),G=r.useCallback(async(t,c)=>{if(!s)return;if($>=R){h({title:"Generation limit reached",description:"You've used all 3 generations for this pitch. Edit the text manually.",variant:"destructive"});return}const l=t||z,a=c||F;B(!0);try{const{data:o,error:j}=await d.functions.invoke("generate-pitch",{body:{brand_id:s,profile_id:i,tone:l,pitch_type:a}});if(console.log("[generatePitch] response:",{data:o,error:j}),j){console.error("[generatePitch] Edge function error:",j),h({title:"Couldn't generate the pitch right now",description:"Try again in a minute.",variant:"destructive"}),B(!1);return}o?(I(o.subject||""),q(o.body||""),o.to_email&&!p&&C(o.to_email),Se(N=>N+1),A(!0)):h({title:"Couldn't generate the pitch right now",description:"Try again in a minute.",variant:"destructive"})}catch(o){console.error("Pitch generation error:",o),h({title:"Couldn't generate the pitch right now",description:"Try again in a minute.",variant:"destructive"})}B(!1)},[s,z,F,p,$]),Le=t=>{W(t),te&&G(t)},De=()=>{const t=`To: ${p} 2Subject: ${S} 3 4${w}`;navigator.clipboard.writeText(t),h({title:"Copied!",description:"Full email copied to clipboard"})},Te=()=>{const t=`https://mail.google.com/mail/?view=cm&to=${encodeURIComponent(p)}&su=${encodeURIComponent(S)}&body=${encodeURIComponent(w)}`;window.open(t,"_blank")},Pe=async()=>{if(!T)return;const{error:t}=await d.from("pipeline_entries").update({status:"pitched",pitched_at:new Date().toISOString(),contact_email_used:p||null}).eq("id",T);t||(se(!0),k.invalidateQueries({queryKey:["pipeline"]}),k.invalidateQueries({queryKey:["dashboard"]}),m&&d.from("user_onboarding").upsert({user_id:m.id,profile_id:i,step_name:"draft_pitch",completed_at:new Date().toISOString()},{onConflict:"profile_id,step_name"}))},Ie=async()=>{if(!m||!s)return;H(!0);const{error:t}=await d.from("pitch_drafts").upsert({user_id:m.id,profile_id:i,brand_id:s,pipeline_entry_id:T,subject:S,body:w,tone:z,pitch_type:F},{onConflict:"profile_id,brand_id"});H(!1),t?h({title:"Error",description:t.message,variant:"destructive"}):(ee(new Date().toISOString()),h({title:"Draft saved",description:"You can come back to this later"}))},V=(()=>{const t=ve||(n?.median_channel_size?n.median_channel_size*.05:null);if(!t||t<100)return null;const c=Math.max(50,Math.round(t/1e3*12)),l=Math.max(100,Math.round(t/1e3*25));return{low:c,high:l}})(),re=(()=>{const t=n?.name||"";return`https://www.linkedin.com/search/results/companies/?keywords=${encodeURIComponent(t)}`})(),qe=y!==null?e.jsxs("span",{className:"text-[13px] font-extrabold rounded-full px-2.5 py-0.5",style:{background:y>=80?"hsl(var(--brand))":y>=60?"#FFB020":"#E5E5E5",color:y>=80?"hsl(var(--foreground))":y>=60?"#5a3200":"#444"},children:[y,"% match"]}):null;E?.name,E?.thumbnail_url;const ne=E?.subscriber_count||0,ie=E?.avg_views||0;return ne>0&&ie>0&&(ie/ne*100).toFixed(1),Me?e.jsx("div",{className:"min-h-screen bg-background",children:e.jsx("main",{className:`${g} min-h-screen flex items-center justify-center`,children:e.jsx(U,{size:32,className:"animate-spin text-primary"})})}):n?ze?e.jsx("div",{className:"min-h-screen bg-background",children:e.jsx("main",{className:`${g} min-h-screen flex items-center justify-center`,children:e.jsxs("div",{className:"text-center max-w-md mx-auto px-4",children:[e.jsx("div",{className:"w-16 h-16 rounded-full mx-auto flex items-center justify-center mb-4",style:{background:"hsl(var(--brand))"},children:e.jsx(Ze,{size:28,className:"text-primary-foreground"})}),e.jsxs("h2",{className:"text-2xl font-bold text-foreground",children:["Pitch sent to ",n.name," â"]}),e.jsx("p",{className:"text-sm text-muted-foreground mt-2",children:"Follow up in 5-7 days if no response"}),e.jsxs("div",{className:"mt-6 space-y-3",children:[M&&e.jsxs(f,{variant:"default",size:"lg",className:"w-full",onClick:()=>{se(!1),x(`/outreach/${M.brand_id}`,{replace:!0})},children:["Next: Reach out to ",M.brand_name," (",M.score,"% match) ",e.jsx(et,{size:16})]}),e.jsx(f,{variant:"secondary",className:"w-full",onClick:()=>x("/pipeline"),children:"View pipeline"}),e.jsx(f,{variant:"ghost",className:"w-full",onClick:()=>x("/matches"),children:"â Back to matches"})]})]})})}):e.jsx("div",{className:"min-h-screen app-bg",children:e.jsxs("main",{className:`${g} min-h-screen pb-28 md:pb-24`,children:[e.jsxs("div",{className:"max-w-7xl mx-auto w-full px-4 md:px-6 xl:px-8 py-5 md:py-7",children:[e.jsxs("button",{onClick:u,className:"flex items-center gap-1.5 text-sm text-muted-foreground hover:text-foreground mb-4 bg-transparent border-none cursor-pointer p-0",children:[e.jsx(tt,{size:14})," Back"]}),e.jsxs("div",{className:"rounded-2xl border border-border p-5 flex flex-col sm:flex-row sm:items-center gap-4",style:{background:"hsl(var(--card))"},children:[e.jsx(We,{name:n.name,logoUrl:n.logo_url,size:52}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx("h1",{className:"text-xl font-bold text-foreground",children:n.name}),qe,e.jsxs("span",{className:"flex items-center gap-1 text-[11px] rounded-full px-2 py-0.5 bg-secondary text-secondary-foreground",children:[e.jsx(st,{size:10})," Saved to pipeline"]})]}),e.jsxs("div",{className:"flex items-center gap-3 mt-1 text-xs text-muted-foreground flex-wrap",children:[n.total_sponsorships&&e.jsxs("span",{children:[n.total_sponsorships," creators sponsored"]}),n.last_sponsorship_at&&e.jsxs("span",{children:["· Active ",pe(n.last_sponsorship_at)]}),n.total_channels&&n.total_channels>1&&e.jsx("span",{children:"· Targets your size"})]}),V&&e.jsxs("div",{className:"mt-1.5 text-xs font-semibold text-accent",children:["Est. deal range: $",V.low.toLocaleStr
4ing()," â $",V.high.toLocaleString()]})]}),e.jsxs(le,{to:`/brands/${n.id}`,className:"text-xs font-medium text-accent hover:underline flex items-center gap-1 shrink-0",children:["View full profile ",e.jsx(at,{size:10})]})]}),e.jsxs("div",{className:"mt-6 grid grid-cols-1 lg:grid-cols-5 gap-6",children:[e.jsxs("div",{className:"lg:col-span-2 space-y-5",children:[e.jsxs("div",{className:"rounded-2xl border border-border p-5",style:{background:"hsl(var(--card))"},children:[e.jsx("h2",{className:"text-sm font-bold text-foreground mb-4",children:"Who to contact"}),_e?Ne?e.jsxs("div",{className:"py-8 text-center",children:[e.jsx(U,{size:20,className:"animate-spin mx-auto text-primary mb-3"}),e.jsx("p",{className:"text-sm font-medium text-foreground",children:"Finding the right contact..."}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:ke})]}):O.length>0?e.jsxs("div",{className:"space-y-3",children:[O.map((t,c)=>{const l=!!(t.name&&t.name.trim());let a=null;if(t.verification_status==="verified"){const N=fe(t.verified_at||null);a={text:N?`â Verified · ${N}`:"â Verified",palette:"verified"}}else t.verification_status==="unverified"&&l?a={text:"Likely",palette:"amber"}:t.source==="firecrawl"&&ct(t.email)&&(a={text:"From brand site",palette:"neutral"});const o=a?.palette==="verified"?{background:"#DCFCE7",color:"#166534"}:a?.palette==="amber"?{background:"#FEF3C7",color:"#92400E"}:a?.palette==="neutral"?{background:"#F3F4F6",color:"#374151"}:void 0,j=it[t.source]||t.source;return e.jsxs("div",{className:`rounded-xl p-3 border ${c===0?"border-primary/30 bg-secondary/30":"border-border"}`,children:[l&&e.jsxs("div",{className:"mb-1.5",children:[e.jsx("div",{className:"text-sm font-semibold text-foreground truncate",children:t.name}),t.title&&e.jsx("div",{className:"text-[11px] text-muted-foreground truncate",children:t.title})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("a",{href:`mailto:${t.email}`,className:`${l?"text-[12px]":"text-sm font-medium"} text-foreground hover:text-accent truncate flex-1`,children:t.email}),e.jsx("button",{onClick:()=>{navigator.clipboard.writeText(t.email),h({title:"Copied!"})},className:"p-1 rounded hover:bg-muted bg-transparent border-none cursor-pointer shrink-0","aria-label":"Copy email",children:e.jsx(ue,{size:12,className:"text-muted-foreground"})}),a&&e.jsx("span",{className:"inline-flex items-center text-[9px] px-1.5 py-0.5 rounded-full font-medium shrink-0",style:o,children:a.text})]}),e.jsxs("span",{className:"text-[10px] text-muted-foreground mt-1 block",children:[j,t.created_at?` · added ${fe(t.created_at)}`:""]})]},t.id||c)}),e.jsxs("a",{href:re,target:"_blank",rel:"noopener noreferrer",className:"flex items-center gap-1.5 text-xs text-blue-600 hover:underline mt-2",children:[e.jsx(xe,{size:12})," Find on LinkedIn â"]})]}):e.jsxs("div",{className:"py-4",children:[e.jsxs("p",{className:"text-sm font-semibold text-foreground",children:["No verified contact for ",n.name," yet"]}),e.jsx("p",{className:"text-sm text-muted-foreground mt-1 mb-3",children:"We re-check automatically as new data comes in â in the meantime, LinkedIn is usually the fastest route to the right person."}),e.jsxs("a",{href:re,target:"_blank",rel:"noopener noreferrer",className:"flex items-center gap-2 rounded-xl p-3 border border-blue-200 bg-blue-50 text-blue-700 text-sm font-medium hover:bg-blue-100 transition-colors",children:[e.jsx(xe,{size:16})," Find marketing contacts on LinkedIn â"]}),n.website&&e.jsxs("a",{href:n.website.startsWith("http")?n.website:`https://${n.website}`,target:"_blank",rel:"noopener noreferrer",className:"flex items-center gap-1.5 text-xs text-muted-foreground hover:text-foreground mt-3",children:[e.jsx(rt,{size:12})," Visit brand website â"]})]}):e.jsxs("div",{className:"py-6 text-center",children:[e.jsx("p",{className:"text-xs text-muted-foreground mb-3",children:"Find the best email contact for this brand"}),e.jsxs(f,{variant:"default",size:"sm",className:"gap-1.5",onClick:Ee,children:[e.jsx(me,{size:14})," View contact"]})]})]}),e.jsxs("div",{className:"rounded-2xl border border-border p-5",style:{background:"hsl(var(--card))"},children:[e.jsxs("div",{className:"flex items-center gap-2 mb-3",children:[e.jsx(he,{size:14,className:"text-muted-foreground"}),e.jsx("h2",{className:"text-sm font-bold text-foreground",children:"Your media kit"})]}),e.jsx("p",{className:"text-xs text-muted-foreground mb-4",children:"View, customize, and download your full media kit to attach with your pitch."}),e.jsx(f,{variant:"secondary",size:"sm",className:"w-full text-xs gap-1.5",asChild:!0,children:e.jsxs(le,{to:"/media-kit",children:[e.jsx(he,{size:12})," Open full media kit â"]})}),e.jsx("p",{className:"text-[10px] text-muted-foreground mt-3",children:"ð¡ Tip: Attach your media kit when the brand responds to your pitch"})]})]}),e.jsx("div",{className:"lg:col-span-3",children:e.jsxs("div",{className:"rounded-2xl border border-border p-5",style:{background:"hsl(var(--card))"},children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("h2",{className:"text-sm font-bold text-foreground",children:"Your pitch email"}),Z&&e.jsxs("span",{className:"text-[10px] text-muted-foreground",children:["Draft saved · Last edited ",pe(Z)]})]}),e.jsx("div",{className:"flex items-center gap-1",children:["professional","casual","brief"].map(t=>e.jsx("button",{onClick:()=>Le(t),className:`text-[11px] rounded-full px-3 py-1 cursor-pointer transition-colors border ${z===t?"bg-secondary text-accent border-primary/30 font-semibold":"bg-transparent text-muted-foreground border-border hover:bg-muted"}`,children:t.charAt(0).toUpperCase()+t.slice(1)},t))})]}),Ce?e.jsxs("div",{className:"space-y-4 py-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-[11px] font-medium text-muted-foreground",children:"Subject"}),e.jsx(b,{className:"h-10 w-full mt-1 rounded-md"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-[11px] font-medium text-muted-foreground",children:"Body"}),e.jsxs("div",{className:"mt-1 space-y-2",children:[e.jsx(b,{className:"h-4 w-3/4 rounded"}),e.jsx(b,{className:"h-4 w-full rounded"}),e.jsx(b,{className:"h-4 w-5/6 rounded"}),e.jsx(b,{className:"h-4 w-full rounded"}),e.jsx(b,{className:"h-4 w-2/3 rounded"}),e.jsx(b,{className:"h-4 w-full rounded"}),e.jsx(b,{className:"h-4 w-4/5 rounded"})]})]}),e.jsxs("p",{className:"text-xs text-muted-foreground text-center flex items-center justify-center gap-1.5",children:[e.jsx(D,{size:12,className:"text-primary"})," Generating your personalized pitch..."]})]}):!te&&!w?e.jsxs("div",{className:"py-12 text-center",children:[e.jsx(D,{size:24,className:"mx-auto text-primary mb-3"}),e.jsxs("p",{className:"text-sm text-muted-foreground mb-4",children:["Generate a personalized pitch for ",n.name]}),e.jsxs(f,{onClick:()=>G(),className:"gap-1.5",children:[e.jsx(D,{size:14})," Generate pitch"]})]}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"mb-3",children:[e.jsx("label",{className:"text-[11px] font-medium text-muted-foreground",children:"To"}),e.jsx(de,{value:p,onChange:t=>C(t.target.value),placeholder:"Paste email address or find one on LinkedIn",className:"mt-1 text-sm"}),!p&&e.jsx("p",{className:"text-[10px] text-amber-600 mt-1",children:"No email found â paste one or use LinkedIn"})]}),e.jsxs("div",{className:"mb-3",children:[e.jsx("label",{className:"text-[11px] font-medium text-muted-foreground",children:"Subject"}),e.jsx(de,{value:S,onChange:t=>I(t.target.value),className:"mt-1 text-sm"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-[11px] font-medium text-muted-foreground",children:"Body"}),e.jsx(Je,{value:w,onChange:t=>q(t.target.value),className:"mt-1 text-sm min-h-[280px] font-mono leading-relaxed"})]}),$<R?e.jsxs("button",{onClick:()=>G(),className:"flex items-center gap-1.5 text-xs text-accent hover:underline mt-3 bg-transparent border-none cursor-pointer p-0",children:[e.jsx(D,{size:12})," Regenerate pitch (",R-$," left)"]}):e.jsx("p",{className:"text-[10px] text-muted-foreground mt-3",children:"No regenerations left â edit the text manually"})]})]})})]})]}),e.jsx("div",{className:"fixed bottom-0 left-0 right-0 z-40 border-t border-border",style:{background:"hsl(var(--background) / 0.95)",backdropFilter:"blur(8px)"},children:e.jsxs("div",{className:`${g} max-w-[1200px] mx-auto px-4 py-3 flex items-center gap-3 flex-wrap justify-between`,children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsxs(f,{onClick:De,className:"gap-1.5",children:[e.jsx(ue,{size:14})," Copy to clipboard"]}),e.jsxs(f,{variant:"secondary",onClick:Te,className:"gap-1.5",children:[e.jsx(me,{size:14})," Open in Gmail â"]})]}),e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx(f,{variant:"ghost",size:"sm",onClick:Ie,disabled:J,className:"text-xs",children:J?e.jsx(U,{size:12,className:"animate-spin"}):"Save draft"}),e.jsxs(f,{variant:"cta",onClick:Pe,className:"gap-1.5",children:[e.jsx(nt,{size:14})," Mark as pitched"]})]})]})})]})}):e.jsx("div",{className:"min-h-screen bg-background",children:e.jsx("main",{className:`${g} min-h-screen flex items-center justify-center`,children:e.jsx("p",{className:"text-muted-foreground",children:"Brand n
4ot found"})})})};export{bt 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.