import{e as T,u as H,f as N,g as M,r as s,d as C,j as e,L as P,b as m,S as D,C as F,P as U,M as _}from"./index-Cn0iwDxV.js";import G from"./LocationPage-CJsPwtKT.js";import{C as V}from"./chevron-left-CS5gTWBq.js";import{S as k}from"./star-DNzCVNep.js";const O=[["circle",{cx:"12",cy:"12",r:"10",key:"1mglay"}],["path",{d:"M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20",key:"13o1zl"}],["path",{d:"M2 12h20",key:"9i4pu4"}]],q=T("globe",O);function J(i){const o=i.trim().split(/\s+/);return o.length<=1?o[0]??"":`${o[0]} ${o[o.length-1][0]}.`}function p(i){return i.split(/[_\s-]+/).filter(Boolean).map(o=>o.charAt(0).toUpperCase()+o.slice(1).toLowerCase()).join(" ")}function K(i){return new Date(i).toLocaleDateString("en-GB",{day:"numeric",month:"short",year:"numeric"})}function S({rating:i}){return e.jsx("div",{style:{display:"flex",alignItems:"center",gap:3},children:[1,2,3,4,5].map(o=>e.jsx(k,{size:15,fill:o<=Math.round(i)?"var(--ochre-500)":"none",color:o<=Math.round(i)?"var(--ochre-500)":"var(--border)"},o))})}function ee(){const{id:i}=H(),o=!!(i&&N(i)),{user:c,token:R}=M(),[r,h]=s.useState(null),[z,W]=s.useState(!0),[x,B]=s.useState(""),[a,I]=s.useState(0),[u,v]=s.useState(0),[d,$]=s.useState(""),[l,E]=s.useState(""),[g,f]=s.useState(!1),[y,b]=s.useState(""),[j,L]=s.useState(!1);s.useEffect(()=>{if(!i||o)return;let t=!1;return C.get(`/contractors/${i}`).then(n=>{t||h(n)}).catch(n=>{t||B(n instanceof Error?n.message:"Failed to load contractor profile.")}).finally(()=>{t||W(!1)}),()=>{t=!0}},[i,o]);const w=s.useMemo(()=>!r||r.reviews.length===0?0:r.reviews.reduce((n,A)=>n+A.rating,0)/r.reviews.length,[r]);return o?e.jsx(G,{categorySlugProp:"contractors",countySlugProp:i}):z?e.jsxs("main",{style:{backgroundColor:"var(--cream)",minHeight:"80vh",display:"flex",alignItems:"center",justifyContent:"center"},children:[e.jsx(P,{size:34,color:"var(--green-800)",style:{animation:"spin 1s linear infinite"}}),e.jsx("style",{children:"@keyframes spin { to { transform: rotate(360deg); } }"})]}):x||!r?e.jsx("main",{style:{backgroundColor:"var(--cream)",minHeight:"80vh",display:"flex",alignItems:"center",justifyContent:"center"},children:e.jsxs("div",{style:{textAlign:"center",padding:"40px 24px"},children:[e.jsx("p",{style:{color:"#dc2626",fontWeight:600,marginBottom:14},children:x||"Contractor not found."}),e.jsx(m,{to:"/contractors",style:{color:"var(--green-800)",fontWeight:700},children:"Back to Contractors"})]})}):e.jsxs("main",{style:{backgroundColor:"var(--cream)",minHeight:"80vh",padding:"30px 24px 56px"},children:[e.jsx(D,{title:`${r.businessName} - Agricultural Contractor`,description:`${r.businessName} - ${r.services.map(t=>p(t)).join(", ")} contractor covering ${r.coverageCounties.join(", ")}. View reviews and contact details on FarmExchange.`,url:`/contractors/${r.id}`,image:r.coverImageUrl??void 0,jsonLd:{"@context":"https://schema.org","@type":"LocalBusiness",name:r.businessName,description:r.bio??`Agricultural contractor offering ${r.services.map(t=>p(t)).join(", ")}`,url:`https://www.farmexchange.co.uk/contractors/${r.id}`,...r.coverImageUrl?{image:r.coverImageUrl}:{},...r.phone?{telephone:r.phone}:{},...r.website?{sameAs:r.website}:{},areaServed:r.coverageCounties.map(t=>({"@type":"AdministrativeArea",name:t})),...r.reviews.length>0?{aggregateRating:{"@type":"AggregateRating",ratingValue:(r.reviews.reduce((t,n)=>t+n.rating,0)/r.reviews.length).toFixed(1),reviewCount:r.reviews.length}}:{}}}),e.jsxs("div",{style:{maxWidth:1040,margin:"0 auto"},children:[e.jsxs(m,{to:"/contractors",style:{display:"inline-flex",alignItems:"center",gap:6,color:"var(--green-700)",fontWeight:700,marginBottom:16},children:[e.jsx(V,{size:16})," Back to Contractors"]}),r.coverImageUrl?e.jsx("div",{style:{marginBottom:18,borderRadius:14,overflow:"hidden",border:"1.5px solid var(--border)"},children:e.jsx("img",{src:r.coverImageUrl,alt:`${r.businessName} cover`,style:{width:"100%",maxHeight:280,objectFit:"cover",display:"block"}})}):e.jsx("div",{style:{marginBottom:18,borderRadius:14,height:120,background:"linear-gradient(135deg, var(--green-900) 0%, var(--green-700) 100%)"}}),e.jsxs("section",{style:{backgroundColor:"white",borderRadius:14,border:"1.5px solid var(--border)",padding:"24px",marginBottom:18},children:[e.jsxs("div",{style:{display:"flex",justifyContent:"space-between",alignItems:"flex-start",gap:14,flexWrap:"wrap"},children:[e.jsxs("div",{children:[e.jsxs("div",{style:{display:"flex",alignItems:"center",gap:8,marginBottom:6},children:[e.jsx("h1",{style:{fontSize:"clamp(1.35rem, 2.7vw, 1.9rem)",fontWeight:900,color:"var(--ink)"},children:r.businessName}),r.verified&&e.jsx(F,{size:18,color:"var(--green-700)",fill:"var(--green-100)"})]}),e.jsxs("div",{style:{display:"flex",alignItems:"center",gap:8},children:[e.jsx(S,{rating:w}),e.jsx("span",{style:{fontSize:"0.86rem",color:"var(--ink-muted)"},children:r.reviews.length>0?`${w.toFixed(1)} (${r.reviews.length} reviews)`:"No reviews yet"})]})]}),e.jsxs("div",{style:{display:"flex",gap:8,flexWrap:"wrap"},children:[r.phone&&e.jsxs("a",{href:`tel:${r.phone.replace(/\s+/g,"")}`,style:{display:"inline-flex",alignItems:"center",gap:7,backgroundColor:"var(--green-800)",color:"white",fontWeight:700,borderRadius:8,minHeight:44,padding:"0 14px"},children:[e.jsx(U,{size:14})," Call"]}),r.website&&e.jsxs("a",{href:r.website,target:"_blank",rel:"noreferrer",style:{display:"inline-flex",alignItems:"center",gap:7,backgroundColor:"white",color:"var(--green-800)",fontWeight:700,borderRadius:8,minHeight:44,padding:"0 14px",border:"1.5px solid var(--green-800)"},children:[e.jsx(q,{size:14})," Website"]})]})]}),e.jsxs("div",{style:{display:"flex",gap:16,flexWrap:"wrap",marginTop:14,fontSize:"0.84rem",color:"var(--ink-muted)"},children:[e.jsxs("span",{style:{display:"inline-flex",alignItems:"center",gap:5},children:[e.jsx(_,{size:13})," ",r.coverageCounties.join(", ")||"Coverage not specified"]}),r.yearEstablished&&e.jsxs("span",{children:["Established ",r.yearEstablished]})]}),e.jsx("p",{style:{marginTop:16,color:"var(--ink-light)",lineHeight:1.75},children:r.bio??"No profile bio added yet."}),e.jsx("div",{style:{marginTop:16,display:"flex",gap:8,flexWrap:"wrap"},children:r.services.map(t=>e.jsx("span",{style:{backgroundColor:"var(--green-100)",color:"var(--green-800)",fontSize:"0.76rem",fontWeight:700,padding:"4px 10px",borderRadius:20},children:p(t)},t))})]}),e.jsxs("section",{style:{backgroundColor:"white",borderRadius:14,border:"1.5px solid var(--border)",padding:"24px"},children:[e.jsx("h2",{style:{fontSize:"1.03rem",fontWeight:800,marginBottom:14,color:"var(--ink)"},children:"Reviews"}),r.reviews.length===0?e.jsx("p",{style:{color:"var(--ink-muted)"},children:"No reviews yet."}):e.jsx("div",{style:{display:"flex",flexDirection:"column",gap:12},children:r.reviews.map(t=>e.jsxs("article",{style:{border:"1px solid var(--border)",borderRadius:10,padding:"14px 14px 12px",backgroundColor:"var(--cream)"},children:[e.jsxs("div",{style:{display:"flex",justifyContent:"space-between",alignItems:"center",marginBottom:8,flexWrap:"wrap",gap:8},children:[e.jsxs("div",{style:{display:"flex",alignItems:"center",gap:8},children:[e.jsx(S,{rating:t.rating}),e.jsx("span",{style:{fontWeight:700,color:"var(--ink)"},children:J(t.author.name)})]}),e.jsx("span",{style:{fontSize:"0.78rem",color:"var(--ink-muted)"},children:K(t.createdAt)})]}),t.title&&e.jsx("h3",{style:{fontSize:"0.93rem",marginBottom:4},children:t.title}),e.jsx("p",{style:{color:"var(--ink-light)",lineHeight:1.65,fontSize:"0.9rem"},children:t.comment})]},t.id))})]}),r&&c&&c.id!==r.user.id&&!r.reviews.some(t=>t.author.id===c.id)&&!j&&e.jsxs("section",{style:{backgroundColor:"white",borderRadius:14,border:"1.5px solid var(--border)",padding:"24px",marginTop:18},children:[e.jsx("h2",{style:{fontSize:"1.03rem",fontWeight:800,marginBottom:14,color:"var(--ink)"},children:"Leave a Review"}),e.jsxs("div",{style:{marginBottom:16},children:[e.jsx("div",{style:{fontSize:"0.85rem",fontWeight:600,color:"var(--ink)",marginBottom:6},children:"Rating *"}),e.jsx("div",{style:{display:"flex",gap:4},children:[1,2,3,4,5].map(t=>e.jsx("button",{type:"button",onMouseEnter:()=>v(t),onMouseLeave:()=>v(0),onClick:()=>I(t),style:{background:"none",border:"none",cursor:"pointer",padding:2},"aria-label":`${t} star${t>1?"s":""}`,children:e.jsx(k,{size:24,fill:t<=(u||a)?"var(--ochre-500)":"none",color:t<=(u||a)?"var(--ochre-500)":"var(--border)"})},t))})]}),e.jsxs("div",{style:{marginBottom:14},children:[e.jsx("div",{style:{fontSize:"0.85rem",fontWeight:600,color:"var(--ink)",marginBottom:6},children:"Title (optional)"}),e.jsx("input",{type:"text",value:d,onChange:t=>$(t.target.value),placeholder:"e.g. Excellent silage service",maxLength:120,style:{width:"100%",border:"1.5px solid var(--border)",borderRadius:8,padding:"10px 14px",fontSize:"0.9rem",outline:"none"}})]}),e.jsxs("div",{style:{marginBottom:16},children:[e.jsx("div",{style:{fontSize:"0.85rem",fontWeight:600,color:"var(--ink)",marginBottom:6},children:"Comment *"}),e.jsx("textarea",{value:l,onChange:t=>E(t.target.value),placeholder:"Describe your experience working with this contractor...",maxLength:2e3,rows:4,style:{width:"100%",border:"1.5px solid var(--border)",borderRadius:8,padding:"10px 14px",fontSize:"0.9rem",outline:"none",resize:"vertical"}}),e.jsxs("div",{style:{fontSize:"0.75rem",color:"var(--ink-muted)",textAlign:"right",marginTop:4},children:[l.length,"/2000"]})]}),y&&e.jsx("p",{style:{color:"#dc2626",fontSize:"0.85rem",fontWeight:600,marginBottom:12},children:y}),e.jsx("button",{disabled:g||a===0||l.trim().length<10,onClick:async()=>{f(!0),b("");try{const t=await C.post(`/contractors/${i}/reviews`,{rating:a,...d.trim()?{title:d.trim()}:{},comment:l.trim()},R);h(n=>n&&{...n,reviews:[t,...n.reviews]}),L(!0)}catch(t){b(t instanceof Error?t.message:"Failed to submit review")}finally{f(!1)}},style:{backgroundColor:a>0&&l.trim().length>=10?"var(--green-800)":"var(--border)",color:"white",fontWeight:700,fontSize:"0.9rem",border:"none",borderRadius:8,padding:"12px 24px",cursor:a>0?"pointer":"not-allowed",opacity:g?.6:1},children:g?"Submitting…":"Submit Review"})]}),j&&e.jsx("section",{style:{backgroundColor:"#eef5e6",borderRadius:14,border:"1.5px solid #b5d99c",padding:"24px",marginTop:18,textAlign:"center"},children:e.jsx("p",{style:{color:"#2D5016",fontWeight:700,fontSize:"0.95rem"},children:"Thanks for your review!"})}),r&&!c&&e.jsx("section",{style:{backgroundColor:"white",borderRadius:14,border:"1.5px solid var(--border)",padding:"24px",marginTop:18,textAlign:"center"},children:e.jsxs("p",{style:{color:"var(--ink-muted)",fontSize:"0.9rem"},children:[e.jsx(m,{to:"/sign-in",style:{color:"var(--green-800)",fontWeight:700},children:"Sign in"})," to leave a review for this contractor."]})})]})]})}export{ee as default};