import{j as e}from"./ui-vendor-DUfjpZ4p.js";import{r as h}from"./react-vendor-DlVMRrtE.js";import{a9 as D,aa as L,ab as Y,ac as E,at as r,aL as k,aM as A,aN as F,aO as $,aP as R,I as l,B as N,t as u,s as x,aS as S,aT as w}from"./index-BKwmLAPt.js";const q=d=>`KYC/V2/26-${d.split(/\s+/).filter(m=>m.length>0).map(m=>m[0].toUpperCase()).join("")||"XX"}`,G=({open:d,onOpenChange:o,onSuccess:m,onSelectDebtRecovery:p})=>{const[y,g]=h.useState(!1),[T,j]=h.useState("KYC Report"),[a,_]=h.useState({company_name:"",registration_number:"",contact_person:"",contact_number:"",city:"",area:""}),i=(t,c)=>_(s=>({...s,[t]:c})),I=t=>{if(t==="Debt Recovery"){p==null||p();return}j(t)},K=async t=>{if(t.preventDefault(),!a.company_name.trim()){u({title:"Company name is required",variant:"destructive"});return}g(!0);const{data:{user:c}}=await x.auth.getUser();if(!c){u({title:"You must be logged in",variant:"destructive"}),g(!1);return}const s=q(a.company_name),{error:v}=await x.from("kyc_assignments").insert({assignment_ref:s,company_name:a.company_name.trim(),registration_number:a.registration_number.trim()||null,contact_person:a.contact_person.trim()||null,contact_number:a.contact_number.trim()||null,city:a.city.trim()||null,area:a.area.trim()||null,request_type:"KYC Report",country:"Ghana",status:"Pending",submitted_by:c.id});if(g(!1),v)u({title:"Error",description:v.message,variant:"destructive"});else{u({title:"Request submitted",description:`Reference: ${s}`});const{data:n}=await x.from("profiles").select("full_name, email, company").eq("id",c.id).maybeSingle(),C=(n==null?void 0:n.full_name)||"",b=(n==null?void 0:n.email)||c.email||"";S({templateName:"request-received-client",recipientEmail:b,idempotencyKey:`kyc-client-${s}`,templateData:{clientName:C,companyName:a.company_name,country:"Ghana",checkType:"KYC Report",referenceId:s}});for(const f of w)S({templateName:"request-received-admin",recipientEmail:f,idempotencyKey:`kyc-admin-${s}-${f}`,templateData:{clientName:C,clientEmail:b,clientCompany:(n==null?void 0:n.company)||"",companyName:a.company_name,country:"Ghana",checkType:"KYC Report",referenceId:s,sourceModule:"Star Steel KYC"}});_({company_name:"",registration_number:"",contact_person:"",contact_number:"",city:"",area:""}),j("KYC Report"),o(!1),m()}};return e.jsx(e.Fragment,{children:e.jsx(D,{open:d,onOpenChange:o,children:e.jsxs(L,{className:"max-w-lg",children:[e.jsx(Y,{children:e.jsx(E,{children:"Submit Investigation Request"})}),e.jsxs("form",{onSubmit:K,className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx(r,{children:"Request Type *"}),e.jsxs(k,{value:T,onValueChange:I,children:[e.jsx(A,{children:e.jsx(F,{})}),e.jsxs($,{children:[e.jsx(R,{value:"KYC Report",children:"KYC / Due Diligence Report"}),e.jsx(R,{value:"Debt Recovery",children:"Debt Recovery"})]})]})]}),e.jsxs("div",{children:[e.jsx(r,{children:"Customer / Company Name *"}),e.jsx(l,{placeholder:"e.g. Accra Building Company",value:a.company_name,onChange:t=>i("company_name",t.target.value),maxLength:200}),a.company_name.trim()&&e.jsxs("p",{className:"text-xs text-muted-foreground mt-1",children:["Reference: ",q(a.company_name)]})]}),e.jsxs("div",{children:[e.jsx(r,{children:"Business Registration Number"}),e.jsx(l,{placeholder:"Registration number",value:a.registration_number,onChange:t=>i("registration_number",t.target.value),maxLength:100})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx(r,{children:"Contact Person"}),e.jsx(l,{placeholder:"Full name",value:a.contact_person,onChange:t=>i("contact_person",t.target.value),maxLength:100})]}),e.jsxs("div",{children:[e.jsx(r,{children:"Contact Number"}),e.jsx(l,{placeholder:"Phone number",value:a.contact_number,onChange:t=>i("contact_number",t.target.value),maxLength:30})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx(r,{children:"City"}),e.jsx(l,{placeholder:"City",value:a.city,onChange:t=>i("city",t.target.value),maxLength:100})]}),e.jsxs("div",{children:[e.jsx(r,{children:"Area"}),e.jsx(l,{placeholder:"Area / District",value:a.area,onChange:t=>i("area",t.target.value),maxLength:100})]})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[e.jsx(N,{type:"button",variant:"outline",onClick:()=>o(!1),children:"Cancel"}),e.jsx(N,{type:"submit",disabled:y,children:y?"Submitting...":"Submit Request"})]})]})]})})})};export{G as C};
