import Head from 'next/head';
import Link from 'next/link';
import { useRouter } from 'next/router';
import { useState } from 'react';
import { Button } from 'react-bootstrap';
import { toast } from 'react-hot-toast';

const UserTypeSelect = () => {
  const [selectedType, setSelectedType] = useState('');
  const [isLoading, setIsLoading] = useState(false);
  const router = useRouter();

  const handleSelection = (type: string) => {
    const currentType = localStorage.getItem('userType');

    if (currentType && currentType !== type) {
      localStorage.removeItem('userType'); // Remove old type
    }

    setSelectedType(type);
    localStorage.setItem('userType', type);
    toast.success(`${type.charAt(0).toUpperCase() + type.slice(1)} selected!`);
  };

  const handleContinue = () => {
    if (!selectedType) {
      toast.error('Please select a user type before proceeding!');
      return;
    }
    setIsLoading(true);
    router.push('/auth/signup');
  };

  return (
    <>
      <Head>
        <title>Select User Type | Deliver X</title>
      </Head>

      <div className="h-100 d-flex flex-column justify-content-center align-items-center text-center p-4">
        <h5 className="auth-page-title mb-4">Select User Type</h5>
        <p className="mb-4">Please choose whether you're signing up as a Carrier or a User</p>

        <div className="d-flex gap-4 flex-wrap justify-content-center mb-5">
          <div
            className={`user-box ${selectedType === 'carrier' ? 'active' : ''}`}
            onClick={() => handleSelection('carrier')}
          >
            <img src="/images/icons/delivery.png" alt="Carrier" width={40} />
            <h5 className="mt-2">Carrier</h5>
          </div>

          <div
            className={`user-box ${selectedType === 'user' ? 'active' : ''}`}
            onClick={() => handleSelection('user')}
          >
            <img src="/images/icons/user-icon.png" alt="User" width={40} />
            <h5 className="mt-2">User</h5>
          </div>
        </div>

        <div className="d-flex justify-content-center align-items-center">
          <Button
            type="button"
            size="md"
            className="mt-xl-5 mt-2 btn btn-feature auth-btn"
            onClick={handleContinue}
            disabled={!selectedType || isLoading}
          >
            {isLoading ? 'Signing Up...' : 'Sign Up'}
          </Button>
        </div>

        <div className="authFooter d-flex justify-content-center align-items-center gap-2 mt-4">
          <p className="pb-0 mb-0 fw-normal">Already have an account?</p>
          <Link href="/auth/login" className="text-primary-color fw-semibold">
            Login
          </Link>
        </div>
      </div>

      <style jsx>{`
        .user-box {
          width: 180px;
          padding: 20px;
          border: 2px solid #ccc;
          border-radius: 12px;
          cursor: pointer;
          transition: all 0.3s ease;
          background-color: #f9f9f9;
          text-align: center;
        }
        .user-box:hover {
          border-color: #007bff;
          background-color: #e6f0ff;
        }
        .user-box.active {
          border-color: #007bff;
          background-color: #d0e7ff;
        }
      `}</style>
    </>
  );
};

export default UserTypeSelect;
