"use client";
import {
Box,
Flex,
Heading,
Text,
Card,
VStack,
HStack,
Separator,
Spinner,
Input,
Button,
IconButton,
} from "@chakra-ui/react";
import { useTranslations } from "next-intl";
import { useColorModeValue } from "@/components/ui/color-mode";
import { SlideUp } from "@/components/motion";
import { Avatar } from "@/components/ui/data-display/avatar";
import { useSession } from "next-auth/react";
import { useUserBettingStats } from "@/lib/api/coupons/use-hooks";
import type { UserBettingStatsDto } from "@/lib/api/coupons/types";
import {
LuMail,
LuUser,
LuCalendar,
LuShield,
LuTrendingUp,
LuTarget,
LuTicket,
LuPen,
LuCheck,
LuX,
LuLock,
} from "react-icons/lu";
import { useState } from "react";
import { useUpdateProfile, useChangePassword } from "@/lib/api/users/use-hooks";
import { Field } from "@/components/ui/forms/field";
import { useForm } from "react-hook-form";
import * as yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
import { PasswordInput } from "@/components/ui/forms/password-input";
import { useRouter } from "next/navigation";
interface InfoRowProps {
icon: React.ReactNode;
label: string;
value: string;
}
function InfoRow({ icon, label, value }: InfoRowProps) {
return (
{icon}
{label}
{value}
);
}
const profileSchema = yup.object({
firstName: yup.string().required(),
lastName: yup.string().required(),
});
type ProfileForm = yup.InferType;
const passwordSchema = yup.object({
currentPassword: yup.string().required(),
newPassword: yup.string().min(8).required(),
confirmPassword: yup
.string()
.oneOf([yup.ref("newPassword")], "Passwords must match")
.required(),
});
type PasswordForm = yup.InferType;
export default function ProfileContent() {
const t = useTranslations("profile");
const tCommon = useTranslations("common");
const { data: session, update: updateSession } = useSession();
const { data: statsData, isLoading: statsLoading } = useUserBettingStats();
const cardBg = useColorModeValue("white", "gray.800");
const borderColor = useColorModeValue("gray.100", "gray.700");
const user = session?.user;
const stats = statsData?.data as UserBettingStatsDto | undefined;
// Edit profile state
const [isEditing, setIsEditing] = useState(false);
const updateProfile = useUpdateProfile();
const {
handleSubmit: handleProfileSubmit,
register: profileRegister,
formState: { errors: profileErrors },
reset: resetProfile,
} = useForm({
resolver: yupResolver(profileSchema),
mode: "onChange",
defaultValues: {
firstName: user?.name?.split(" ")[0] || "",
lastName: user?.name?.split(" ").slice(1).join(" ") || "",
},
});
const onProfileSubmit = async (data: ProfileForm) => {
await updateProfile.mutateAsync(data);
await updateSession();
setIsEditing(false);
};
// Change password state
const [showPasswordForm, setShowPasswordForm] = useState(false);
const changePassword = useChangePassword();
const router = useRouter();
const {
handleSubmit: handlePasswordSubmit,
register: passwordRegister,
formState: { errors: passwordErrors },
reset: resetPassword,
} = useForm({
resolver: yupResolver(passwordSchema),
mode: "onChange",
});
const onPasswordSubmit = async (data: PasswordForm) => {
await changePassword.mutateAsync({
currentPassword: data.currentPassword,
newPassword: data.newPassword,
});
resetPassword();
setShowPasswordForm(false);
};
return (
{t("title")}
{/* Profile Card */}
{user?.name || "—"}
{user?.email || "—"}
{/* Account Info */}
{t("personal-info")}
{!isEditing ? (
setIsEditing(true)}
>
) : null}
{isEditing ? (
) : (
<>
}
label={t("full-name")}
value={user?.name || "—"}
/>
}
label={t("email")}
value={user?.email || "—"}
/>
}
label={t("role")}
value={
(user as Record)?.roles
? String((user as Record).roles)
: "User"
}
/>
}
label={t("member-since")}
value="—"
/>
>
)}
{/* Change Password */}
{t("change-password")}
{!showPasswordForm ? (
) : null}
{showPasswordForm ? (
) : (
{t("change-password-desc")}
)}
{/* Betting Stats */}
{t("betting-stats")}
{statsLoading ? (
) : (
<>
}
label={t("total-coupons")}
value={String(stats?.totalCoupons ?? "—")}
/>
}
label={t("win-rate")}
value={
stats?.winRate != null
? `${Math.round(stats.winRate)}%`
: "—"
}
/>
}
label={t("total-profit")}
value={stats?.wonBets != null ? String(stats.wonBets) : "—"}
/>
>
)}
);
}