import{c as N,a as r,f as P,t as gt}from"../chunks/bmn8XlW7.js";import{o as _e}from"../chunks/CJbqisBi.js";import{f as $,p as ge,s as E,b as xe,a as ye,d as u,e as n,c as s,g as t,h as D,r as l,n as q,t as it}from"../chunks/DpkolLiq.js";import{s as tt,e as we}from"../chunks/pfhwnV1O.js";import{l as ct,s as ut,i as h}from"../chunks/TXeUdaq1.js";import{I as vt,s as ft,h as dt,a as Lt,e as be}from"../chunks/DxBGAPM1.js";import{g as Pe}from"../chunks/C2ETuzsT.js";import{r as Ce}from"../chunks/CiPTfhJW.js";import{I as ke,B as b,C as Tt}from"../chunks/SxayRmdi.js";import{C as ze,T as Bt}from"../chunks/CkcI5Cyl.js";import{B as Ft}from"../chunks/DJJfkUlF.js";import{L as kt}from"../chunks/C8B7Gptd.js";import"../chunks/EMQoTJgY.js";function De(k,m){const _=ct(m,["children","$$slots","$$events","$$legacy"]);/**
 * @license lucide-svelte v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const y=[["path",{d:"M2.586 17.414A2 2 0 0 0 2 18.828V21a1 1 0 0 0 1 1h3a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1h1a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1h.172a2 2 0 0 0 1.414-.586l.814-.814a6.5 6.5 0 1 0-4-4z"}],["circle",{cx:"16.5",cy:"7.5",r:".5",fill:"currentColor"}]];vt(k,ut({name:"key-round"},()=>_,{get iconNode(){return y},children:(f,C)=>{var i=N(),v=$(i);ft(v,m,"default",{}),r(f,i)},$$slots:{default:!0}}))}function It(k,m){const _=ct(m,["children","$$slots","$$events","$$legacy"]);/**
 * @license lucide-svelte v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const y=[["path",{d:"m2 2 20 20"}],["path",{d:"M5 5a1 1 0 0 0-1 1v7c0 5 3.5 7.5 7.67 8.94a1 1 0 0 0 .67.01c2.35-.82 4.48-1.97 5.9-3.71"}],["path",{d:"M9.309 3.652A12.252 12.252 0 0 0 11.24 2.28a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1v7a9.784 9.784 0 0 1-.08 1.264"}]];vt(k,ut({name:"shield-off"},()=>_,{get iconNode(){return y},children:(f,C)=>{var i=N(),v=$(i);ft(v,m,"default",{}),r(f,i)},$$slots:{default:!0}}))}function Ne(k,m){const _=ct(m,["children","$$slots","$$events","$$legacy"]);/**
 * @license lucide-svelte v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const y=[["path",{d:"M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"}]];vt(k,ut({name:"shield"},()=>_,{get iconNode(){return y},children:(f,C)=>{var i=N(),v=$(i);ft(v,m,"default",{}),r(f,i)},$$slots:{default:!0}}))}function jt(k,m){const _=ct(m,["children","$$slots","$$events","$$legacy"]);/**
 * @license lucide-svelte v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const y=[["path",{d:"M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"}],["circle",{cx:"9",cy:"7",r:"4"}],["polyline",{points:"16 11 18 13 22 9"}]];vt(k,ut({name:"user-check"},()=>_,{get iconNode(){return y},children:(f,C)=>{var i=N(),v=$(i);ft(v,m,"default",{}),r(f,i)},$$slots:{default:!0}}))}function Vt(k,m){const _=ct(m,["children","$$slots","$$events","$$legacy"]);/**
 * @license lucide-svelte v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const y=[["path",{d:"M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"}],["circle",{cx:"9",cy:"7",r:"4"}],["line",{x1:"17",x2:"22",y1:"8",y2:"13"}],["line",{x1:"22",x2:"17",y1:"8",y2:"13"}]];vt(k,ut({name:"user-x"},()=>_,{get iconNode(){return y},children:(f,C)=>{var i=N(),v=$(i);ft(v,m,"default",{}),r(f,i)},$$slots:{default:!0}}))}var Me=P('<div class="text-center py-12 text-muted-foreground"><!> Loading users...</div>'),Ue=P('<div class="text-center py-12"><p class="text-red-500 dark:text-red-400 mb-4"> </p> <!></div>'),Ae=P('<p class="text-muted-foreground">No users found.</p>'),Ee=P('<span class="text-xs text-muted-foreground">(you)</span>'),Se=P('<span class="text-muted-foreground">User</span>'),Re=P("<!> Approve",1),Le=P("<!> <!> <!> <!>",1),Te=P('<tr class="hover:bg-muted/30 transition-colors"><td class="py-3 px-4"><div class="flex items-center gap-2"> <!></div></td><td class="py-3 px-4"><!></td><td class="py-3 px-4"><!></td><td class="py-3 px-4 text-muted-foreground"> </td><td class="py-3 px-4"><div class="flex items-center justify-end gap-2"><!></div></td></tr>'),Be=P('<div class="overflow-x-auto"><table class="w-full text-sm"><thead class="bg-muted/50 border-b"><tr><th class="text-left py-3 px-4 font-medium">Email</th><th class="text-left py-3 px-4 font-medium">Status</th><th class="text-left py-3 px-4 font-medium">Role</th><th class="text-left py-3 px-4 font-medium">Created</th><th class="text-right py-3 px-4 font-medium">Actions</th></tr></thead><tbody class="divide-y"></tbody></table></div>'),Fe=P('<p class="text-sm text-red-500"> </p>'),Ie=P("<!> Reset Password",1),je=P('<div class="fixed inset-0 z-50 bg-black/50 flex items-center justify-center"><div class="bg-background border rounded-lg shadow-lg p-6 w-full max-w-md"><h2 class="text-lg font-semibold mb-4">Reset Password</h2> <p class="text-sm text-muted-foreground mb-4">Set a new password for <strong> </strong></p> <form><div class="space-y-4"><div><label for="new-password" class="block text-sm font-medium mb-1">New Password</label> <!></div> <!> <div class="flex justify-end gap-2"><!> <!></div></div></form></div></div>'),Ve=P('<div class="space-y-6"><div class="flex items-center justify-between"><h1 class="text-2xl font-bold">User Management</h1></div> <!></div> <!> <!>',1);function sa(k,m){ge(m,!0);let _=E(xe([])),y=E(!0),f=E(null),C=E(null),i=E(null),v=E(""),K=E(null),et=E(!1),xt=E(!1),H=E(null);_e(()=>{if(!Lt.isAdmin){Pe(Ce("/"));return}at()});async function at(){s(y,!0),s(f,null);try{const e=await dt.listUsers();s(_,e.sort((o,p)=>o.status==="pending"&&p.status!=="pending"?-1:p.status==="pending"&&o.status!=="pending"?1:new Date(p.created_at).getTime()-new Date(o.created_at).getTime()),!0)}catch(e){s(f,e instanceof Error?e.message:"Failed to load users",!0)}finally{s(y,!1)}}async function yt(e,o){s(C,e,!0);try{await dt.updateUser(e,{status:o}),await at()}catch(p){s(f,p instanceof Error?p.message:"Failed to update user",!0)}finally{s(C,null)}}async function zt(e,o){s(C,e,!0);try{await dt.updateUser(e,{is_admin:o}),await at()}catch(p){s(f,p instanceof Error?p.message:"Failed to update user",!0)}finally{s(C,null)}}function Ht(e){s(H,e,!0),s(xt,!0)}async function Ot(){if(t(H)){s(C,t(H).id,!0);try{await dt.deleteUser(t(H).id),await at()}catch(e){s(f,e instanceof Error?e.message:"Failed to delete user",!0)}finally{s(C,null),s(H,null)}}}function qt(e){s(i,{userId:e.id,email:e.email},!0),s(v,""),s(K,null)}async function Kt(){if(t(i)){if(t(v).length<8){s(K,"Password must be at least 8 characters");return}s(et,!0),s(K,null);try{await dt.resetPassword(t(i).userId,t(v)),s(i,null),s(v,"")}catch(e){s(K,e instanceof Error?e.message:"Failed to reset password",!0)}finally{s(et,!1)}}}function Gt(e){switch(e){case"pending":return"warning";case"approved":return"success";case"disabled":return"destructive"}}const Jt=D(()=>t(_).filter(e=>e.is_admin).length);function S(e){return e.is_admin&&t(Jt)===1}function L(e){var o;return((o=Lt.user)==null?void 0:o.id)===e.id}function Qt(e){return new Date(e).toLocaleDateString(void 0,{year:"numeric",month:"short",day:"numeric"})}var Dt=Ve(),wt=$(Dt),Wt=u(n(wt),2);{var Xt=e=>{var o=Me(),p=n(o);kt(p,{class:"w-6 h-6 animate-spin mx-auto mb-2"}),q(),l(o),r(e,o)},Yt=e=>{var o=N(),p=$(o);{var st=T=>{var M=Ue(),B=n(M),O=n(B,!0);l(B);var rt=u(B,2);b(rt,{onclick:()=>at(),children:(z,G)=>{q();var J=gt("Retry");r(z,J)},$$slots:{default:!0}}),l(M),it(()=>tt(O,t(f))),r(T,M)},mt=T=>{var M=N(),B=$(M);{var O=z=>{Tt(z,{class:"p-12 text-center",children:(G,J)=>{var F=Ae();r(G,F)},$$slots:{default:!0}})},rt=z=>{Tt(z,{class:"overflow-hidden",children:(G,J)=>{var F=Be(),pt=n(F),g=u(n(pt));be(g,21,()=>t(_),U=>U.id,(U,a)=>{const ht=D(()=>t(C)===t(a).id);var ot=Te(),lt=n(ot),Q=n(lt),Mt=n(Q),ee=u(Mt);{var ae=c=>{var A=Ee();r(c,A)};h(ee,c=>{L(t(a))&&c(ae)})}l(Q),l(lt);var bt=u(lt),se=n(bt);{let c=D(()=>Gt(t(a).status));Ft(se,{get variant(){return t(c)},children:(A,$t)=>{q();var W=gt();it(()=>tt(W,t(a).status)),r(A,W)},$$slots:{default:!0}})}l(bt);var Pt=u(bt),re=n(Pt);{var oe=c=>{Ft(c,{variant:"default",children:(A,$t)=>{q();var W=gt("Admin");r(A,W)},$$slots:{default:!0}})},le=c=>{var A=Se();r(c,A)};h(re,c=>{t(a).is_admin?c(oe):c(le,!1)})}l(Pt);var Ct=u(Pt),ne=n(Ct,!0);l(Ct);var Ut=u(Ct),At=n(Ut),ie=n(At);{var de=c=>{kt(c,{class:"w-4 h-4 animate-spin"})},ce=c=>{var A=Le(),$t=$(A);{var W=d=>{b(d,{size:"sm",variant:"outline",onclick:()=>yt(t(a).id,"approved"),title:"Approve user",children:(x,I)=>{var j=Re(),X=$(j);jt(X,{class:"w-4 h-4 mr-1"}),q(),r(x,j)},$$slots:{default:!0}})},ue=d=>{var x=N(),I=$(x);{var j=w=>{var R=N(),Y=$(R);{var nt=V=>{{let _t=D(()=>L(t(a))&&S(t(a))),Rt=D(()=>L(t(a))&&S(t(a))?"Cannot disable last admin":"Disable user");b(V,{size:"sm",variant:"ghost",onclick:()=>yt(t(a).id,"disabled"),get disabled(){return t(_t)},get title(){return t(Rt)},children:($e,He)=>{Vt($e,{class:"w-4 h-4"})},$$slots:{default:!0}})}},Z=V=>{b(V,{size:"sm",variant:"ghost",disabled:!0,title:"Cannot disable last admin",children:(_t,Rt)=>{Vt(_t,{class:"w-4 h-4 opacity-50"})},$$slots:{default:!0}})};h(Y,V=>{!L(t(a))||!S(t(a))?V(nt):V(Z,!1)})}r(w,R)},X=w=>{var R=N(),Y=$(R);{var nt=Z=>{b(Z,{size:"sm",variant:"ghost",onclick:()=>yt(t(a).id,"approved"),title:"Enable user",children:(V,_t)=>{jt(V,{class:"w-4 h-4"})},$$slots:{default:!0}})};h(Y,Z=>{t(a).status==="disabled"&&Z(nt)},!0)}r(w,R)};h(I,w=>{t(a).status==="approved"?w(j):w(X,!1)},!0)}r(d,x)};h($t,d=>{t(a).status==="pending"?d(W):d(ue,!1)})}var Et=u($t,2);{var ve=d=>{b(d,{size:"sm",variant:"ghost",onclick:()=>zt(t(a).id,!0),title:"Make admin",children:(x,I)=>{Ne(x,{class:"w-4 h-4"})},$$slots:{default:!0}})},fe=d=>{var x=N(),I=$(x);{var j=w=>{{let R=D(()=>S(t(a))),Y=D(()=>S(t(a))?"Cannot remove last admin":"Remove admin");b(w,{size:"sm",variant:"ghost",onclick:()=>zt(t(a).id,!1),get disabled(){return t(R)},get title(){return t(Y)},children:(nt,Z)=>{It(nt,{class:"w-4 h-4"})},$$slots:{default:!0}})}},X=w=>{b(w,{size:"sm",variant:"ghost",disabled:!0,title:"Cannot remove last admin",children:(R,Y)=>{It(R,{class:"w-4 h-4 opacity-50"})},$$slots:{default:!0}})};h(I,w=>{!L(t(a))||!S(t(a))?w(j):w(X,!1)},!0)}r(d,x)};h(Et,d=>{t(a).is_admin?d(fe,!1):d(ve)})}var St=u(Et,2);b(St,{size:"sm",variant:"ghost",onclick:()=>qt(t(a)),title:"Reset password",children:(d,x)=>{De(d,{class:"w-4 h-4"})},$$slots:{default:!0}});var me=u(St,2);{var pe=d=>{{let x=D(()=>L(t(a))&&S(t(a))),I=D(()=>L(t(a))&&S(t(a))?"Cannot delete last admin":"Delete user");b(d,{size:"sm",variant:"ghost",onclick:()=>Ht(t(a)),get disabled(){return t(x)},get title(){return t(I)},children:(j,X)=>{Bt(j,{class:"w-4 h-4 text-destructive"})},$$slots:{default:!0}})}},he=d=>{b(d,{size:"sm",variant:"ghost",disabled:!0,title:"Cannot delete last admin",children:(x,I)=>{Bt(x,{class:"w-4 h-4 opacity-50"})},$$slots:{default:!0}})};h(me,d=>{!L(t(a))||!S(t(a))?d(pe):d(he,!1)})}r(c,A)};h(ie,c=>{t(ht)?c(de):c(ce,!1)})}l(At),l(Ut),l(ot),it(c=>{tt(Mt,`${t(a).email??""} `),tt(ne,c)},[()=>Qt(t(a).created_at)]),r(U,ot)}),l(g),l(pt),l(F),r(G,F)},$$slots:{default:!0}})};h(B,z=>{t(_).length===0?z(O):z(rt,!1)},!0)}r(T,M)};h(p,T=>{t(f)?T(st):T(mt,!1)},!0)}r(e,o)};h(Wt,e=>{t(y)?e(Xt):e(Yt,!1)})}l(wt);var Nt=u(wt,2);{var Zt=e=>{var o=je(),p=n(o),st=u(n(p),2),mt=u(n(st)),T=n(mt,!0);l(mt),l(st);var M=u(st,2),B=n(M),O=n(B),rt=u(n(O),2);ke(rt,{id:"new-password",type:"password",placeholder:"Enter new password (min 8 characters)",required:!0,get value(){return t(v)},set value(g){s(v,g,!0)}}),l(O);var z=u(O,2);{var G=g=>{var U=Fe(),a=n(U,!0);l(U),it(()=>tt(a,t(K))),r(g,U)};h(z,g=>{t(K)&&g(G)})}var J=u(z,2),F=n(J);b(F,{variant:"outline",onclick:()=>{s(i,null),s(v,"")},get disabled(){return t(et)},children:(g,U)=>{q();var a=gt("Cancel");r(g,a)},$$slots:{default:!0}});var pt=u(F,2);{let g=D(()=>t(et)||t(v).length<8);b(pt,{type:"submit",get disabled(){return t(g)},children:(U,a)=>{var ht=Ie(),ot=$(ht);{var lt=Q=>{kt(Q,{class:"w-4 h-4 animate-spin mr-2"})};h(ot,Q=>{t(et)&&Q(lt)})}q(),r(U,ht)},$$slots:{default:!0}})}l(J),l(B),l(M),l(p),l(o),it(()=>tt(T,t(i).email)),we("submit",M,g=>{g.preventDefault(),Kt()}),r(e,o)};h(Nt,e=>{t(i)&&e(Zt)})}var te=u(Nt,2);{let e=D(()=>{var o;return`Are you sure you want to delete ${(o=t(H))==null?void 0:o.email}? This action cannot be undone.`});ze(te,{title:"Delete User",get description(){return t(e)},confirmLabel:"Delete",variant:"destructive",onConfirm:Ot,onCancel:()=>s(H,null),get open(){return t(xt)},set open(o){s(xt,o,!0)}})}r(k,Dt),ye()}export{sa as component};
