"use client";
import { useEffect } from "react";
import { authClient } from "../lib/auth-client";
function useEnsureUserMetadata() {
  var _a, _b;
  const session = authClient.useSession();
  useEffect(() => {
    const ensureMetadata = async () => {
      var _a2, _b2;
      if (!((_b2 = (_a2 = session.data) == null ? void 0 : _a2.user) == null ? void 0 : _b2.id)) return;
      try {
        const response = await fetch("/api/user/profile?includeMeta=true", {
          credentials: "include"
        });
        if (response.ok) {
          const userData = await response.json();
          if (!userData.meta || Object.keys(userData.meta).length === 0) {
            console.log("Creating default metadata for user:", session.data.user.id);
            await fetch("/api/internal/user-metadata", {
              method: "POST",
              headers: {
                "Content-Type": "application/json"
              },
              body: JSON.stringify({
                userId: session.data.user.id,
                metadata: {
                  uiPreferences: {
                    theme: "light",
                    sidebarCollapsed: false
                  },
                  securityPreferences: {
                    loginAlertsEnabled: true
                  },
                  notificationsPreferences: {
                    pushEnabled: true,
                    loginAlertsEmail: true,
                    loginAlertsPush: true,
                    passwordChangesEmail: true,
                    passwordChangesPush: true,
                    suspiciousActivityEmail: true,
                    suspiciousActivityPush: true,
                    mentionsEmail: true,
                    mentionsPush: true,
                    projectUpdatesEmail: true,
                    projectUpdatesPush: false,
                    teamInvitesEmail: true,
                    teamInvitesPush: true,
                    newsletterEmail: false,
                    newsletterPush: false,
                    promotionsEmail: false,
                    promotionsPush: false,
                    featureAnnouncementsEmail: true,
                    featureAnnouncementsPush: false
                  }
                }
              })
            });
            console.log("Default metadata created successfully");
          }
        }
      } catch (error) {
        console.error("Error ensuring user metadata:", error);
      }
    };
    ensureMetadata();
  }, [(_b = (_a = session.data) == null ? void 0 : _a.user) == null ? void 0 : _b.id]);
}
export {
  useEnsureUserMetadata
};
