import React, { useState } from 'react';

type Errors = {
  email?: string;
  password?: string;
  form?: string;
};

const Login: React.FC = () => {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const [errors, setErrors] = useState<Errors>({});

  const validateEmail = (value: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value.trim());

  const validate = (): Errors => {
    const next: Errors = {};
    const trimmedEmail = email.trim();

    if (!trimmedEmail) next.email = 'Email is required.';
    else if (!validateEmail(trimmedEmail)) next.email = 'Enter a valid email.';

    if (!password) next.password = 'Password is required.';
    else if (password.length < 6) next.password = 'Minimum 6 characters.';

    return next;
  };

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

    const nextErrors = validate();
    setErrors(nextErrors);

    if (Object.keys(nextErrors).length > 0) return;

    console.log('Login:', { email: email.trim(), password });
  };

  const handleForgotPassword = () => {
    console.log('Redirect to forgot password page');
  };

  const onEmailChange = (v: string) => {
    setEmail(v);
    if (errors.email) setErrors((prev) => ({ ...prev, email: undefined, form: undefined }));
  };

  const onPasswordChange = (v: string) => {
    setPassword(v);
    if (errors.password) setErrors((prev) => ({ ...prev, password: undefined, form: undefined }));
  };

  return (
    <div className="login">
      <div className="login__img__wrapper">
        <img className="login__img" src="https://cdn.pixabay.com/photo/2022/04/08/06/36/rock-7118843_1280.jpg" alt="Login" />
      </div>

      <div className="login__container">
        <div className="login__card">
          <div className="login__logo">
            <svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 362 105" fill="none">
              <path d="M162.919 93.5458V7.96968H136.383V0H197.82V7.96968H171.285V93.5458H162.919Z" fill="black" />
              <path
                d="M210.005 13.849C208.175 13.849 206.606 13.2393 205.299 12.0199C204.079 10.7133 203.469 9.18909 203.469 7.44708C203.469 5.61798 204.079 4.09372 205.299 2.87432C206.606 1.56781 208.175 0.914552 210.005 0.914552C211.748 0.914552 213.229 1.56781 214.449 2.87432C215.757 4.09372 216.41 5.61798 216.41 7.44708C216.41 9.18909 215.757 10.7133 214.449 12.0199C213.229 13.2393 211.748 13.849 210.005 13.849ZM205.822 93.5458V29.527H214.188V93.5458H205.822Z"
                fill="black"
              />
              <path
                d="M280.911 82.3099H280.388C279.342 84.0519 278.078 85.7503 276.597 87.4052C275.115 88.973 273.373 90.3666 271.368 91.5861C269.364 92.8055 267.098 93.7635 264.571 94.4604C262.044 95.2443 259.255 95.6362 256.205 95.6362C252.109 95.6362 248.231 94.8088 244.571 93.1539C240.911 91.4118 237.686 89.0601 234.898 86.0987C232.196 83.0502 230.017 79.4355 228.362 75.2547C226.793 71.0739 226.009 66.5011 226.009 61.5364C226.009 56.5717 226.793 51.9989 228.362 47.8181C230.017 43.6373 232.196 40.0662 234.898 37.1048C237.686 34.0563 240.911 31.7045 244.571 30.0496C248.231 28.3076 252.109 27.4366 256.205 27.4366C262.305 27.4366 267.359 28.7431 271.368 31.3561C275.377 33.9691 278.383 37.0612 280.388 40.6323H280.911L280.388 31.7481V0H288.754V93.5458H280.911V82.3099ZM257.643 88.0585C260.693 88.0585 263.612 87.4923 266.401 86.36C269.277 85.1406 271.76 83.3986 273.852 81.134C276.031 78.7823 277.73 75.9951 278.95 72.7724C280.257 69.4625 280.911 65.7172 280.911 61.5364C280.911 57.3556 280.257 53.6103 278.95 50.3005C277.73 46.9907 276.031 44.2034 273.852 41.9388C271.76 39.6742 269.277 37.9758 266.401 36.8435C263.612 35.6241 260.693 35.0144 257.643 35.0144C254.593 35.0144 251.63 35.6241 248.754 36.8435C245.965 38.0629 243.481 39.8484 241.303 42.2001C239.211 44.4647 237.512 47.252 236.205 50.5618C234.985 53.7845 234.375 57.4427 234.375 61.5364C234.375 65.6301 234.985 69.3319 236.205 72.6417C237.512 75.9515 239.211 78.7387 241.303 81.0033C243.481 83.268 245.965 85.01 248.754 86.2294C251.63 87.4488 254.593 88.0585 257.643 88.0585Z"
                fill="black"
              />
              <path
                d="M359.793 78.3903C358.66 80.7421 357.223 82.9631 355.48 85.0535C353.737 87.1439 351.689 88.973 349.336 90.5408C347.07 92.1087 344.456 93.3281 341.493 94.1991C338.53 95.1572 335.262 95.6362 331.689 95.6362C327.07 95.6362 322.8 94.8088 318.878 93.1539C314.957 91.4118 311.558 89.0166 308.682 85.9681C305.894 82.9196 303.715 79.3484 302.146 75.2547C300.578 71.0739 299.793 66.5011 299.793 61.5364C299.793 56.9201 300.491 52.5651 301.885 48.4714C303.366 44.2905 305.458 40.6759 308.159 37.6274C310.861 34.4917 314.129 32.0094 317.963 30.1803C321.798 28.3512 326.111 27.4366 330.905 27.4366C335.523 27.4366 339.663 28.2641 343.323 29.919C347.07 31.4868 350.251 33.7514 352.865 36.7128C355.48 39.5871 357.484 43.0711 358.878 47.1649C360.36 51.2586 361.101 55.7878 361.101 60.7525C361.101 61.0138 361.101 61.2751 361.101 61.5364C361.013 61.7977 360.97 62.0155 360.97 62.1897V62.9736H308.29C308.377 67.4157 309.162 71.2046 310.643 74.3402C312.125 77.4758 313.998 80.0888 316.264 82.1792C318.53 84.1825 321.057 85.6632 323.846 86.6213C326.634 87.5794 329.336 88.0585 331.95 88.0585C337.353 88.0585 341.623 86.8391 344.761 84.4003C347.898 81.8744 350.512 78.6952 352.604 74.8628L359.793 78.3903ZM352.342 55.9184C352.255 53.828 351.82 51.5634 351.035 49.1246C350.251 46.6858 349.031 44.4212 347.375 42.3308C345.719 40.2404 343.497 38.4984 340.708 37.1048C338.007 35.7112 334.652 35.0144 330.643 35.0144C328.029 35.0144 325.501 35.4934 323.061 36.4515C320.621 37.3225 318.399 38.629 316.395 40.371C314.478 42.113 312.822 44.2905 311.427 46.9036C310.12 49.5166 309.249 52.5215 308.813 55.9184H352.342Z"
                fill="black"
              />
              <path
                d="M101.211 60.1065C101.211 60.1065 107.877 26.7486 76.2712 7.84631C76.4781 7.97189 58.3876 -4.7109 38.4167 3.42406C38.7673 3.78 35.9017 18.7582 45.3684 27.9065C45.161 27.7809 101.213 60.1093 101.213 60.1093"
                fill="url(#paint0_linear_100_16650)"
              />
              <path
                d="M100.141 65.9671C99.6608 66.1056 87.8509 56.4508 75.2936 60.4101C75.5028 60.2877 20.3579 94.1359 20.3579 94.1359C20.3579 94.1359 46.5252 115.888 78.2137 97.1233C78.0045 97.2452 97.7823 87.3929 100.141 65.9671"
                fill="url(#paint1_linear_100_16650)"
              />
              <path
                d="M31.9853 5.67737C31.9853 5.67737 -0.0892988 17.3353 0.153201 54.2498C0.153201 54.0065 -1.30549 76.1073 16.0681 88.9789C16.1905 88.4932 30.5257 83.1504 33.4426 70.2788C33.444 70.522 31.9853 5.67737 31.9853 5.67737Z"
                fill="url(#paint2_linear_100_16650)"
              />
              <defs>
                <linearGradient id="paint0_linear_100_16650" x1="101.798" y1="60.1093" x2="43.5053" y2="0.000186692" gradientUnits="userSpaceOnUse">
                  <stop stopColor="#95C3E6" />
                  <stop offset="1" stopColor="#2672AB" />
                </linearGradient>
                <linearGradient id="paint1_linear_100_16650" x1="21.3951" y1="93.8819" x2="70.8134" y2="39.2931" gradientUnits="userSpaceOnUse">
                  <stop stopColor="#9494F3" />
                  <stop offset="1" stopColor="#2626C4" />
                </linearGradient>
                <linearGradient id="paint2_linear_100_16650" x1="31.6767" y1="5.67737" x2="-17.9893" y2="50.1198" gradientUnits="userSpaceOnUse">
                  <stop stopColor="#14DA9E" />
                  <stop offset="1" stopColor="#26B489" />
                </linearGradient>
              </defs>
            </svg>
          </div>

          <h4 className="login__card__title">Login to your account</h4>

          <form className="login__form" onSubmit={handleSubmit} noValidate>
            <div className="login__form__group">
              <label htmlFor="email">Email</label>
              <input
                type="text"
                id="email"
                placeholder="Enter your email"
                value={email}
                onChange={(e) => onEmailChange(e.target.value)}
                aria-invalid={!!errors.email}
                aria-describedby={errors.email ? 'email-error' : undefined}
              />
              {errors.email && (
                <div id="email-error" className="login__error">
                  {errors.email}
                </div>
              )}
            </div>

            <div className="login__form__group">
              <label htmlFor="password">Password</label>
              <input
                type="password"
                id="password"
                placeholder="Enter your password"
                value={password}
                onChange={(e) => onPasswordChange(e.target.value)}
                aria-invalid={!!errors.password}
                aria-describedby={errors.password ? 'password-error' : undefined}
              />
              {errors.password && (
                <div id="password-error" className="login__error">
                  {errors.password}
                </div>
              )}
            </div>

            {errors.form && <div className="login__error">{errors.form}</div>}

            <button type="submit" className="cta cta--primary">
              Login
            </button>

            <button type="button" className="login__forgot" onClick={handleForgotPassword}>
              Forgot password?
            </button>
          </form>
        </div>
      </div>
    </div>
  );
};

export default Login;
