"use client";

import { signIn, useSession } from "next-auth/react";
import { useEffect } from "react";

export default function SignIn() {
  const { data: session } = useSession();

  useEffect(() => {
    // Trigger Keycloak sign-in
    signIn("keycloak", { callbackUrl: "/" });
  }, []);

  useEffect(() => {
    if (session?.user && !session.user.nextcloudInitialized) {
      // Initialize Nextcloud
      fetch('/api/nextcloud/init', {
        method: 'POST'
      }).then(response => {
        if (!response.ok) {
          console.error('Failed to initialize Nextcloud');
        }
      }).catch(error => {
        console.error('Error initializing Nextcloud:', error);
      });
    }
  }, [session]);

  return (
    <div 
      className="min-h-screen flex items-center justify-center"
      style={{
        backgroundImage: "url('/signin.jpg')",
        backgroundSize: 'cover',
        backgroundPosition: 'center',
        backgroundRepeat: 'no-repeat'
      }}
    >
      <div className="w-full max-w-md space-y-8">
        <div>
          <h2 className="mt-6 text-center text-3xl font-bold tracking-tight text-white">
            Redirecting to login...
          </h2>
        </div>
      </div>
    </div>
  );
}
