1import{$ as H,U as n,O as e,B as C,S as _,n as U,o as O,l as T,m as E,d as V,e as Z,h as q,i as z,I as g,p as m,Z as r}from"./index-IBHyG7M_.js";import{L as u}from"./label-DPfgYCkJ.js";import{T as J,d as G,e as j,c as f,a as K,b as h}from"./table-CG-0Fq2Z.js";import{B as Q}from"./badge-CXQjseq-.js";import{P as W}from"./plus-CSRmdP_D.js";import{T as X}from"./trash-2-DdyChubd.js";import{S as Y}from"./shield-DUfxfOyh.js";const l={super_admin:"Super Admin",admin:"Admin",content_manager:"Content Manager",support_agent:"Support Agent",marketing_manager:"Marketing Manager"},ee={super_admin:"destructive",admin:"default",content_manager:"secondary",support_agent:"outline",marketing_manager:"outline"};function oe(){const{isAdmin:k,roles:w}=H(),[v,L]=n.useState([]),[P,x]=n.useState(!1),[i,y]=n.useState(""),[o,N]=n.useState(""),[S,R]=n.useState(""),[d,$]=n.useState("content_manager"),[b,p]=n.useState(!1),[B,M]=n.useState(!1),c=async()=>{const s=await m.users.list();L(Array.isArray(s)?s:[])};n.useEffect(()=>{c(),M(w.includes("super_admin"))},[w]);const D=async(s,a)=>{try{await m.users.addRole(s,a),r.success("Role added"),c()}catch(t){const A=(t==null?void 0:t.message)||"Failed to add role";A.toLowerCase().includes("already")?r.error("User already has this role"):r.error(A)}},I=async s=>{if(confirm("Remove this role?"))try{await m.users.removeRole(s),r.success("Role removed"),c()}catch(a){r.error((a==null?void 0:a.message)||"Failed to remove role")}},F=async()=>{if(!i||!o){r.error("Email and password are required");return}if(!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(i)){r.error("Please enter a valid email address");return}if(!/^(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/.test(o)){r.error("Password must be at least 8 characters with uppercase letter, number, and special character (@$!%*?&)");return}if(d==="super_admin"&&!B){r.error("Only super admins can create other super admin users");return}p(!0);try{await m.users.create({email:i,password:o,full_name:S,role:d})}catch(t){r.error((t==null?void 0:t.message)||"Failed to create user"),p(!1);return}r.success(`User created with ${l[d]} role.`),x(!1),y(""),N(""),R(""),p(!1),c()};return k?e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between mb-6",children:[e.jsx("h1",{className:"text-2xl font-display font-bold text-foreground",children:"User Management"}),e.jsxs(C,{onClick:()=>x(!0),children:[e.jsx(W,{className:"w-4 h-4 mr-2"})," Add User"]})]}),e.jsx("div",{className:"rounded-lg border border-border overflow-hidden",children:e.jsxs(J,{children:[e.jsx(G,{children:e.jsxs(j,{children:[e.jsx(f,{children:"Name"}),e.jsx(f,{children:"Roles"}),e.jsx(f,{className:"w-48",children:"Add Role"})]})}),e.jsxs(K,{children:[v.map(s=>e.jsxs(j,{children:[e.jsx(h,{className:"font-medium",children:s.display_name||"Unknown"}),e.jsx(h,{children:e.jsx("div",{className:"flex flex-wrap gap-1",children:s.roles.map(a=>e.jsxs(Q,{variant:ee[a.role],className:"gap-1 cursor-pointer group",onClick:()=>I(a.id),children:[l[a.role],e.jsx(X,{className:"w-3 h-3 opacity-0 group-hover:opacity-100 transition-opacity"})]},a.id))})}),e.jsx(h,{children:e.jsxs(_,{onValueChange:a=>D(s.user_id,a),children:[e.jsx(U,{className:"h-8 text-xs",children:e.jsx(O,{placeholder:"Add role..."})}),e.jsx(T,{children:Object.keys(l).filter(a=>!s.roles.some(t=>t.role===a)).map(a=>e.jsx(E,{value:a,children:l[a]},a))})]})})]},s.user_id)),v.length===0&&e.jsx(j,{children:e.jsx(h,{colSpan:3,className:"text-center text-muted-foreground py-8",children:"No staff users found."})})]})]})}),e.jsx(V,{open:P,onOpenChange:x,children:e.jsxs(Z,{children:[e.jsx(q,{children:e.jsxs(z,{className:"flex items-center gap-2",children:[e.jsx(Y,{className:"w-5 h-5"})," Add Staff User"]})}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx(u,{children:"Full Name"}),e.jsx(g,{value:S,onChange:s=>R(s.target.value),placeholder:"John Doe"})]}),e.jsxs("div",{children:[e.jsx(u,{children:"Email"}),e.jsx(g,{type:"email",value:i,onChange:s=>y(s.target.value),placeholder:"[email protected]"})]}),e.jsxs("div",{children:[e.jsx(u,{children:"Password"}),e.jsx(g,{type:"password",value:o,onChange:s=>N(s.target.value),placeholder:"Min 6 characters"})]}),e.jsxs("div",{children:[e.jsx(u,{children:"Role"}),e.jsxs(_,{value:d,onValueChange:s=>$(s),children:[e.jsx(U,{children:e.jsx(O,{})}),e.jsx(T,{children:Object.keys(l).map(s=>e.jsx(E,{value:s,children:l[s]},s))})]})]}),e.jsx(C,{onClick:F,disabled:b,className:"w-full bg-gradient-electric text-white",children:b?"Creating...":"Create User"})]})]})})]}):e.jsx("p",{className:"text-muted-foreground",children:"Only admins can manage users."})}export{oe 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.