'use client';

import { useState } from 'react';
import { Link, useRouter } from '@/i18n/routing';
import { authClient } from '@/lib/auth-client';
import { useTranslations } from 'next-intl';
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select";

export default function SignUp() {
  const t = useTranslations('Auth');
  const [formData, setFormData] = useState({
    name: '',
    email: '',
    password: '',
    confirmPassword: '',
    role: 'CALON_PENDIDIK' as 'CALON_PENDIDIK' | 'UMUM',
  });
  const [isLoading, setIsLoading] = useState(false);
  const router = useRouter();

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();

    if (formData.password !== formData.confirmPassword) {
      alert(t('passwordMismatch'));
      return;
    }

    setIsLoading(true);

    await authClient.signUp.email({
      email: formData.email,
      password: formData.password,
      name: formData.name,
      // role: formData.role, // TODO: Handle role in metadata or post-signup
    }, {
      onSuccess: () => {
        router.push('/dashboard');
      },
      onError: (ctx) => {
        alert(ctx.error.message);
        setIsLoading(false);
      }
    });
  };

  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    setFormData({
      ...formData,
      [e.target.name]: e.target.value,
    });
  };

  return (
    <div className="min-h-screen flex items-center justify-center px-4 py-12">
      <div className="relative z-10 w-full max-w-md">
        <div className="bg-white/10 backdrop-blur-lg rounded-2xl p-8 border border-white/20">
          {/* Logo */}
          <div className="text-center mb-8">
            <h1 className="text-3xl font-bold text-white mb-2">
              kreati<span className="text-gold-400">Vortex</span>
            </h1>
            <p className="text-gray-300">{t('signUpTitle')}</p>
          </div>

          {/* Form */}
          <form onSubmit={handleSubmit} className="space-y-6">
            <div className="grid gap-2">
              <Label htmlFor="name" className="text-gray-300">
                {t('nameLabel')}
              </Label>
              <Input
                id="name"
                name="name"
                type="text"
                value={formData.name}
                onChange={handleChange}
                className="bg-white/10 border-white/20 text-white placeholder:text-gray-400 focus-visible:ring-gold-400"
                placeholder="John Doe"
                required
              />
            </div>

            <div className="grid gap-2">
              <Label htmlFor="email" className="text-gray-300">
                {t('emailLabel')}
              </Label>
              <Input
                id="email"
                name="email"
                type="email"
                value={formData.email}
                onChange={handleChange}
                className="bg-white/10 border-white/20 text-white placeholder:text-gray-400 focus-visible:ring-gold-400"
                placeholder="nama@email.com"
                required
              />
            </div>

            <div className="grid gap-2">
              <Label htmlFor="role" className="text-gray-300">
                {t('roleLabel')}
              </Label>
              <Select
                value={formData.role}
                onValueChange={(value) => setFormData({ ...formData, role: value as any })}
              >
                <SelectTrigger className="bg-white/10 border-white/20 text-white focus:ring-gold-400">
                  <SelectValue placeholder={t('roleLabel')} />
                </SelectTrigger>
                <SelectContent className="bg-navy-800 border-white/20 text-white">
                  <SelectItem value="CALON_PENDIDIK">{t('roleEducator')}</SelectItem>
                  <SelectItem value="UMUM">{t('roleGeneral')}</SelectItem>
                </SelectContent>
              </Select>
            </div>

            <div className="grid gap-2">
              <Label htmlFor="password" className="text-gray-300">
                {t('passwordLabel')}
              </Label>
              <Input
                id="password"
                name="password"
                type="password"
                value={formData.password}
                onChange={handleChange}
                className="bg-white/10 border-white/20 text-white placeholder:text-gray-400 focus-visible:ring-gold-400"
                placeholder="••••••••"
                required
              />
            </div>

            <div className="grid gap-2">
              <Label htmlFor="confirmPassword" className="text-gray-300">
                {t('confirmPasswordLabel')}
              </Label>
              <Input
                id="confirmPassword"
                name="confirmPassword"
                type="password"
                value={formData.confirmPassword}
                onChange={handleChange}
                className="bg-white/10 border-white/20 text-white placeholder:text-gray-400 focus-visible:ring-gold-400"
                placeholder="••••••••"
                required
              />
            </div>

            <button
              type="submit"
              disabled={isLoading}
              className="w-full py-3 px-4 bg-gold-500 hover:bg-gold-400 text-navy-900 font-semibold rounded-lg transition-colors duration-200 disabled:opacity-50 disabled:cursor-not-allowed"
            >
              {isLoading ? t('registering') : t('signUpButton')}
            </button>
          </form>

          {/* Sign in link */}
          <div className="mt-8 text-center">
            <p className="text-gray-300">
              {t('haveAccount')}{' '}
              <Link href="/auth/signin" className="text-gold-400 hover:text-gold-300 font-medium">
                {t('signInNow')}
              </Link>
            </p>
          </div>
        </div>
      </div>
    </div>
  );
}