import { createId } from "@paralleldrive/cuid2";

export function generateSessionToken() {
  return createId();
}
export const fromDate = (time: number, date = Date.now()) => {
  return new Date(date + time * 1000);
};

// CredentialsProvider({
//       name: "Credentials",
//       credentials: {
//         email: { label: "Email", type: "email" },
//         password: { label: "Password", type: "password" },
//       },
//       authorize: async (credentials) => {
//         let user: Session["user"] | null = null;

//         if (!credentials?.email || !credentials?.password) return null;

//         if (
//           typeof credentials.password !== "string" ||
//           typeof credentials.email !== "string"
//         ) {
//           console.log("WARN: Password or Email is not a string.");
//           return null;
//         }
//         try {
//           // Add your own database logic here
//           const response = await db.query.users.findFirst({
//             where: eq(users.email, String(credentials.email)),
//           });

//           // No user found
//           if (!response || !response.password) {
//             if (!response?.password) return null;
//           }
//           user = response;
//           // Check password - using timing-safe comparison via bcrypt
//           const isValidPassword = await argon.verify(
//             String(response.password),
//             String(credentials?.password),
//           );
//           if (!isValidPassword) return null;
//           console.log("User authenticated successfully:", user.id);

//           return {
//             name: user.name,
//             email: user.email,
//             image: user.image,
//             id: user.id,
//             role: user.role,
//           } as User;
//         } catch (e) {
//           console.log("WARN: Error while validating credentials.");
//           return null;
//         }
//       },
//     }),

// callback
//  async signIn({ user, account, profile, email, credentials }) {
//       console.log("👉 SignIn callback triggered", user?.id);
//       console.log("👉 SignIn callback credentials", credentials);
//       if (credentials && user.id) await createSession(user.id!);
//       // Return true to allow sign-in
//       return true;
//     },

// server action
// export async function login(values: z.infer<typeof loginSchema>) {
//   return await signIn("credentials", values);
// }
// export async function register(values: z.infer<typeof registerSchema>) {
//   try {
//     const { success } = await api.auth.register({
//       user: {
//         email: values.email,
//         name: values.name,
//       },
//       password: values.password,
//     });
//     await signIn("credentials", {
//       email: values.email,
//       password: values.password,
//     });
//     return success;
//   } catch (e) {
//     return false;
//   }
// }

// export async function createSession(userId: string) {
//   if (!adapter.createSession) throw new Error("Adapter not initialized");
//   const sessionToken = generateSessionToken();
//   const sessionExpiry = fromDate(authConfig.session.maxAge);
//   console.log("👉 createSession", sessionToken);
//   const session = await adapter.createSession({
//     sessionToken: sessionToken,
//     userId: userId,
//     expires: sessionExpiry,
//   });
//   console.log("👉 createSession session", session);
//   const cookieStore = await cookies();
//   cookieStore.set("authjs.session-token", sessionToken, {
//     expires: sessionExpiry,
//   });
// }

// register page
// "use client";
// import { cn } from "@/lib/utils";
// import { Button } from "@/components/ui/button";
// import { Card, CardContent } from "@/components/ui/card";
// import { Input } from "@/components/ui/input";
// import { zodResolver } from "@hookform/resolvers/zod";
// import { useForm } from "react-hook-form";
// import { z } from "zod";

// import {
//   Form,
//   FormControl,
//   FormField,
//   FormItem,
//   FormLabel,
//   FormMessage,
// } from "@/components/ui/form";

// import { registerSchema } from "@/lib/validation/zod/user";
// import { appConfig, appRoutes } from "@/config";
// import Link from "next/link";
// import { login, register } from "@/server/actions/auth";
// import { toast } from "sonner";
// import { AuthProviderList, LeagalFooter } from ".";

// export function RegisterForm({
//   className,
//   ...props
// }: React.ComponentProps<"div">) {
//   const form = useForm<z.infer<typeof registerSchema>>({
//     resolver: zodResolver(registerSchema),
//     defaultValues: {
//       name: "",
//       email: "",
//       password: "",
//       confirmPassword: "",
//     },
//   });

//   // 2. Define a submit handler.
//   async function onSubmit(values: z.infer<typeof registerSchema>) {
//     const success = await register(values);
//     if (!success) toast.error("Registrierung fehlgeschlagen");
//     form.reset();
//   }
//   return (
//     <div className={cn("flex flex-col gap-6", className)} {...props}>
//       <Card className="overflow-hidden">
//         <CardContent className="grid p-0 md:grid-cols-2">
//           <Form {...form}>
//             <form onSubmit={form.handleSubmit(onSubmit)} className="p-6 md:p-8">
//               <div className="flex flex-col gap-6">
//                 <div className="flex flex-col items-center text-center">
//                   <h1 className="text-2xl font-bold">Wilkommen</h1>
//                   <p className="text-balance text-muted-foreground">
//                     Erstelle dein {appConfig.name} Konto
//                   </p>
//                 </div>

//                 <AuthProviderList />

//                 <div className="relative text-center text-sm after:absolute after:inset-0 after:top-1/2 after:z-0 after:flex after:items-center after:border-t after:border-border">
//                   <span className="relative z-10 bg-background px-2 text-muted-foreground">
//                     Oder mit
//                   </span>
//                 </div>

//                 <div className="space-y-4">
//                   <FormField
//                     control={form.control}
//                     name="name"
//                     render={({ field }) => (
//                       <FormItem>
//                         <FormLabel>Name</FormLabel>
//                         <FormControl>
//                           <Input
//                             placeholder="Mustermax"
//                             tabIndex={1}
//                             {...field}
//                           />
//                         </FormControl>
//                         <FormMessage />
//                       </FormItem>
//                     )}
//                   />
//                   <FormField
//                     control={form.control}
//                     name="email"
//                     render={({ field }) => (
//                       <FormItem>
//                         <FormLabel>Email</FormLabel>
//                         <FormControl>
//                           <Input
//                             placeholder="email@beispiel.com"
//                             tabIndex={2}
//                             {...field}
//                           />
//                         </FormControl>
//                         <FormMessage />
//                       </FormItem>
//                     )}
//                   />
//                   <FormField
//                     control={form.control}
//                     name="password"
//                     render={({ field }) => (
//                       <FormItem>
//                         <FormLabel>Passwort</FormLabel>

//                         <FormControl>
//                           <Input
//                             type="password"
//                             tabIndex={3}
//                             placeholder="******"
//                             {...field}
//                           />
//                         </FormControl>
//                         <FormMessage />
//                       </FormItem>
//                     )}
//                   />
//                   <FormField
//                     control={form.control}
//                     name="confirmPassword"
//                     render={({ field }) => (
//                       <FormItem>
//                         <FormLabel>Passwort Wiederholen</FormLabel>
//                         <FormControl>
//                           <Input
//                             type="password"
//                             tabIndex={4}
//                             placeholder="******"
//                             {...field}
//                           />
//                         </FormControl>
//                         <FormMessage />
//                       </FormItem>
//                     )}
//                   />
//                 </div>

//                 <Button type="submit">Login</Button>

//                 <div className="text-center text-sm">
//                   Du hast bereits ein Konto?{" "}
//                   <Link
//                     href={appRoutes.signin}
//                     className="underline underline-offset-4"
//                   >
//                     Anmelden
//                   </Link>
//                 </div>
//               </div>
//             </form>
//           </Form>

//           <div className="relative hidden bg-muted md:block">
//             <img
//               src="/placeholder.svg"
//               alt="Image"
//               className="absolute inset-0 h-full w-full object-cover dark:brightness-[0.2] dark:grayscale"
//             />
//           </div>
//         </CardContent>
//       </Card>
//       <LeagalFooter />
//     </div>
//   );
// }

// Login page

// export function LoginForm({
//   className,
//   ...props
// }: React.ComponentProps<"div">) {
//   const form = useForm<z.infer<typeof loginSchema>>({
//     resolver: zodResolver(loginSchema),
//     defaultValues: {
//       email: "",
//       password: "",
//     },
//   });

//   // 2. Define a submit handler.
//   async function onSubmit(values: z.infer<typeof loginSchema>) {
//     const success = await login(values);
//     // if (!success) toast.error("Login fehlgeschlagen");
//     // form.reset();
//   }
//   return (
//     <div className={cn("flex flex-col gap-6", className)} {...props}>
//       <Card className="overflow-hidden">
//         <CardContent className="grid p-0 md:grid-cols-2">
//           <Form {...form}>
//             <form onSubmit={form.handleSubmit(onSubmit)} className="p-6 md:p-8">
//               <div className="flex flex-col gap-6">
//                 <div className="flex flex-col items-center text-center">
//                   <h1 className="text-2xl font-bold">Wilkommen Zurück</h1>
//                   <p className="text-balance text-muted-foreground">
//                     Melde dich in deinem {appConfig.name} Konto an
//                   </p>
//                 </div>
//                 <AuthProviderList />
//                 <div className="relative text-center text-sm after:absolute after:inset-0 after:top-1/2 after:z-0 after:flex after:items-center after:border-t after:border-border">
//                   <span className="relative z-10 bg-background px-2 text-muted-foreground">
//                     Oder mit
//                   </span>
//                 </div>
//                 <div className="space-y-4">
//                   <FormField
//                     control={form.control}
//                     name="email"
//                     render={({ field }) => (
//                       <FormItem>
//                         <FormLabel>Email</FormLabel>
//                         <FormControl>
//                           <Input
//                             placeholder="email@beispiel.com"
//                             tabIndex={1}
//                             {...field}
//                           />
//                         </FormControl>
//                         <FormMessage />
//                       </FormItem>
//                     )}
//                   />
//                   <FormField
//                     control={form.control}
//                     name="password"
//                     render={({ field }) => (
//                       <FormItem>
//                         <div className="flex items-center">
//                           <FormLabel>Passwort</FormLabel>

//                           <a
//                             href="#"
//                             className="ml-auto text-sm underline-offset-2 hover:underline"
//                           >
//                             passwort vergessen?
//                           </a>
//                         </div>
//                         <FormControl>
//                           <Input
//                             type="password"
//                             tabIndex={2}
//                             placeholder="******"
//                             {...field}
//                           />
//                         </FormControl>
//                         <FormMessage />
//                       </FormItem>
//                     )}
//                   />
//                 </div>

//                 <Button type="submit">Login</Button>

//                 <div className="text-center text-sm">
//                   Du hast noch kein Konto?{" "}
//                   <Link
//                     href={appRoutes.signup}
//                     className="underline underline-offset-4"
//                   >
//                     Registrieren
//                   </Link>
//                 </div>
//               </div>
//             </form>
//           </Form>

//           <div className="relative hidden bg-muted md:block">
//             <img
//               src="/placeholder.svg"
//               alt="Image"
//               className="absolute inset-0 h-full w-full object-cover dark:brightness-[0.2] dark:grayscale"
//             />
//           </div>
//         </CardContent>
//       </Card>
//       <LeagalFooter />
//     </div>
//   );
// }

// export function AuthProviderList() {
//   return (
//     <div className="grid grid-cols-3 gap-4">
//       <Button
//         variant="outline"
//         className="w-full"
//         onClick={() => loginOAuth("discord")}
//       >
//         <Icons.discord />
//         <span className="sr-only">Login with Discord</span>
//       </Button>
//       <Button variant="outline" className="w-full">
//         <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
//           <path
//             d="M12.48 10.92v3.28h7.84c-.24 1.84-.853 3.187-1.787 4.133-1.147 1.147-2.933 2.4-6.053 2.4-4.827 0-8.6-3.893-8.6-8.72s3.773-8.72 8.6-8.72c2.6 0 4.507 1.027 5.907 2.347l2.307-2.307C18.747 1.44 16.133 0 12.48 0 5.867 0 .307 5.387.307 12s5.56 12 12.173 12c3.573 0 6.267-1.173 8.373-3.36 2.16-2.16 2.84-5.213 2.84-7.667 0-.76-.053-1.467-.173-2.053H12.48z"
//             fill="currentColor"
//           />
//         </svg>
//         <span className="sr-only">Login with Google</span>
//       </Button>
//       <Button variant="outline" className="w-full">
//         <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
//           <path
//             d="M6.915 4.03c-1.968 0-3.683 1.28-4.871 3.113C.704 9.208 0 11.883 0 14.449c0 .706.07 1.369.21 1.973a6.624 6.624 0 0 0 .265.86 5.297 5.297 0 0 0 .371.761c.696 1.159 1.818 1.927 3.593 1.927 1.497 0 2.633-.671 3.965-2.444.76-1.012 1.144-1.626 2.663-4.32l.756-1.339.186-.325c.061.1.121.196.183.3l2.152 3.595c.724 1.21 1.665 2.556 2.47 3.314 1.046.987 1.992 1.22 3.06 1.22 1.075 0 1.876-.355 2.455-.843a3.743 3.743 0 0 0 .81-.973c.542-.939.861-2.127.861-3.745 0-2.72-.681-5.357-2.084-7.45-1.282-1.912-2.957-2.93-4.716-2.93-1.047 0-2.088.467-3.053 1.308-.652.57-1.257 1.29-1.82 2.05-.69-.875-1.335-1.547-1.958-2.056-1.182-.966-2.315-1.303-3.454-1.303zm10.16 2.053c1.147 0 2.188.758 2.992 1.999 1.132 1.748 1.647 4.195 1.647 6.4 0 1.548-.368 2.9-1.839 2.9-.58 0-1.027-.23-1.664-1.004-.496-.601-1.343-1.878-2.832-4.358l-.617-1.028a44.908 44.908 0 0 0-1.255-1.98c.07-.109.141-.224.211-.327 1.12-1.667 2.118-2.602 3.358-2.602zm-10.201.553c1.265 0 2.058.791 2.675 1.446.307.327.737.871 1.234 1.579l-1.02 1.566c-.757 1.163-1.882 3.017-2.837 4.338-1.191 1.649-1.81 1.817-2.486 1.817-.524 0-1.038-.237-1.383-.794-.263-.426-.464-1.13-.464-2.046 0-2.221.63-4.535 1.66-6.088.454-.687.964-1.226 1.533-1.533a2.264 2.264 0 0 1 1.088-.285z"
//             fill="currentColor"
//           />
//         </svg>
//         <span className="sr-only">Login with Meta</span>
//       </Button>
//     </div>
//   );
// }

// export function LeagalFooter() {
//   return (
//     <div className="text-balance text-center text-xs text-muted-foreground [&_a]:underline [&_a]:underline-offset-4 hover:[&_a]:text-primary">
//       By clicking continue, you agree to our <a href="#">Terms of Service</a>{" "}
//       and <a href="#">Privacy Policy</a>.
//     </div>
//   );
// }
