import React, { useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { Header } from './components/Header.jsx';
import { Card } from './components/Card.jsx';
import { Button } from './components/Button.jsx';
import { PageTransition } from './components/PageTransition.jsx';
import {
  loadCars,
  addCar,
  updateCar,
  deleteCarById,
  verifyPassword,
} from './carStorage.js';

export default function CarManager() {
  const navigate = useNavigate();
  const [options, setOptions] = useState([]);
  const [addForm, setAddForm] = useState({
    name: '',
    width: '',
    height: '',
    hasRear: false,
    rearWidth: '',
    rearHeight: '',
  });
  const [editName, setEditName] = useState('');
  const [editForm, setEditForm] = useState({
    name: '',
    width: '',
    height: '',
    hasRear: false,
    rearWidth: '',
    rearHeight: '',
  });
  const [deleteName, setDeleteName] = useState('');
  const [password, setPassword] = useState('');
  const [authenticated, setAuthenticated] = useState(false);
  const [error, setError] = useState('');
  const [toast, setToast] = useState(false);

  const applyCars = (cars) => {
    const sorted = [...cars].sort((a, b) => a.name.localeCompare(b.name));
    setOptions(sorted);
    setEditName(sorted[0]?.name || '');
    setDeleteName(sorted[0]?.name || '');
  };

  useEffect(() => {
    loadCars().then((opts) => {
      applyCars(opts);
    });
  }, []);

  useEffect(() => {
    const car = options.find((o) => o.name === editName);
    if (car) {
      setEditForm({
        name: car.name,
        width: String(car.width),
        height: String(car.height),
        hasRear: Boolean(car.rearWidth && car.rearHeight),
        rearWidth: car.rearWidth ? String(car.rearWidth) : '',
        rearHeight: car.rearHeight ? String(car.rearHeight) : '',
      });
    }
  }, [editName, options]);

  useEffect(() => {
    setDeleteName((dn) =>
      options.find((o) => o.name === dn) ? dn : options[0]?.name || ''
    );
  }, [options]);

  useEffect(() => {
    if (toast) {
      const t = setTimeout(() => setToast(false), 2000);
      return () => clearTimeout(t);
    }
  }, [toast]);

  const handleAddChange = (e) => {
    const { name, value, type, checked } = e.target;
    setAddForm((fd) => ({
      ...fd,
      [name]: type === 'checkbox' ? checked : value,
    }));
  };

  const handleEditChange = (e) => {
    const { name, value, type, checked } = e.target;
    setEditForm((fd) => ({
      ...fd,
      [name]: type === 'checkbox' ? checked : value,
    }));
  };

  const handleAddSubmit = async (e) => {
    e.preventDefault();
    const newCar = {
      name: addForm.name,
      width: parseFloat(addForm.width),
      height: parseFloat(addForm.height),
    };
    if (addForm.hasRear) {
      newCar.rearWidth = parseFloat(addForm.rearWidth);
      newCar.rearHeight = parseFloat(addForm.rearHeight);
    }
    await addCar(newCar);
    const refreshed = await loadCars();
    applyCars(refreshed);
    setAddForm({
      name: '',
      width: '',
      height: '',
      hasRear: false,
      rearWidth: '',
      rearHeight: '',
    });
    setToast(true);
  };

  const handleEditSubmit = async (e) => {
    e.preventDefault();
    const car = options.find((o) => o.name === editName);
    if (!car) return;
    const updatedCar = {
      name: editForm.name,
      width: parseFloat(editForm.width),
      height: parseFloat(editForm.height),
    };
    if (editForm.hasRear) {
      updatedCar.rearWidth = parseFloat(editForm.rearWidth);
      updatedCar.rearHeight = parseFloat(editForm.rearHeight);
    }
    await updateCar(car.id, updatedCar);
    const refreshed = await loadCars();
    applyCars(refreshed);
    setEditName(updatedCar.name);
    setToast(true);
  };

  const handleDeleteSubmit = async (e) => {
    e.preventDefault();
    const car = options.find((o) => o.name === deleteName);
    if (!car) return;
    await deleteCarById(car.id);
    const refreshed = await loadCars();
    applyCars(refreshed);
    setToast(true);
  };

  const handleAuthSubmit = async (e) => {
    e.preventDefault();
    const valid = await verifyPassword(password);
    if (valid) {
      setAuthenticated(true);
      setPassword('');
      setError('');
    } else {
      setError('Onjuist wachtwoord');
    }
  };

  return (
    <PageTransition>
      <Header
        count={options.length}
        title="Kinderauto's beheren"
        onHome={() => navigate('/')}
      />
      {!authenticated ? (
        <div className="container mt-6">
          <Card title="Toegang vereist">
            <form onSubmit={handleAuthSubmit} className="mb-lg">
              <div className="form-row">
                <label htmlFor="managerPassword">Wachtwoord:</label>
                <input
                  id="managerPassword"
                  type="password"
                  value={password}
                  onChange={(e) => setPassword(e.target.value)}
                  required
                />
              </div>
              {error && <p className="text-sm text-red-500">{error}</p>}
              <Button type="submit" variant="primary" className="mt">Inloggen</Button>
            </form>
          </Card>
        </div>
      ) : (
        <div className="container mt-6">
          <aside className="side-cards">
            <Card title="Kinderauto toevoegen">
              <form onSubmit={handleAddSubmit} className="mb-lg">
                <div className="form-row">
                  <label htmlFor="addName">Naam:</label>
                  <input
                    id="addName"
                    name="name"
                    value={addForm.name}
                    onChange={handleAddChange}
                    required
                  />
                </div>
                <div className="form-row">
                  <label htmlFor="addWidth">Breedte (cm):</label>
                  <input
                    id="addWidth"
                    type="number"
                    step="0.1"
                    min="0"
                    name="width"
                    value={addForm.width}
                    onChange={handleAddChange}
                    required
                  />
                </div>
                <div className="form-row">
                  <label htmlFor="addHeight">Hoogte (cm):</label>
                  <input
                    id="addHeight"
                    type="number"
                    step="0.1"
                    min="0"
                    name="height"
                    value={addForm.height}
                    onChange={handleAddChange}
                    required
                  />
                </div>
                <div className="form-row checkbox">
                  <label>
                    <input
                      type="checkbox"
                      name="hasRear"
                      checked={addForm.hasRear}
                      onChange={handleAddChange}
                    />
                    Achter heeft andere maat
                  </label>
                </div>
                {addForm.hasRear && (
                  <>
                    <div className="form-row">
                      <label htmlFor="addRearWidth">Breedte achter (cm):</label>
                      <input
                        id="addRearWidth"
                        type="number"
                        step="0.1"
                        min="0"
                        name="rearWidth"
                        value={addForm.rearWidth}
                        onChange={handleAddChange}
                        required
                      />
                    </div>
                    <div className="form-row">
                      <label htmlFor="addRearHeight">Hoogte achter (cm):</label>
                      <input
                        id="addRearHeight"
                        type="number"
                        step="0.1"
                        min="0"
                        name="rearHeight"
                        value={addForm.rearHeight}
                        onChange={handleAddChange}
                        required
                      />
                    </div>
                  </>
                )}
                <Button type="submit" variant="primary" className="mt">
                  Opslaan
                </Button>
              </form>
            </Card>
            <Card title="Kinderauto bewerken">
              <form onSubmit={handleEditSubmit} className="mb-lg">
                <div className="form-row">
                  <label htmlFor="editSelect">Selecteer:</label>
                  <select
                    id="editSelect"
                    value={editName}
                    onChange={(e) => setEditName(e.target.value)}
                  >
                    {options.map((opt) => (
                      <option key={opt.name} value={opt.name}>
                        {opt.name}
                      </option>
                    ))}
                  </select>
                </div>
                <div className="form-row">
                  <label htmlFor="editName">Naam:</label>
                  <input
                    id="editName"
                    name="name"
                    value={editForm.name}
                    onChange={handleEditChange}
                    required
                  />
                </div>
                <div className="form-row">
                  <label htmlFor="editWidth">Breedte (cm):</label>
                  <input
                    id="editWidth"
                    type="number"
                    step="0.1"
                    min="0"
                    name="width"
                    value={editForm.width}
                    onChange={handleEditChange}
                    required
                  />
                </div>
                <div className="form-row">
                  <label htmlFor="editHeight">Hoogte (cm):</label>
                  <input
                    id="editHeight"
                    type="number"
                    step="0.1"
                    min="0"
                    name="height"
                    value={editForm.height}
                    onChange={handleEditChange}
                    required
                  />
                </div>
                <div className="form-row checkbox">
                  <label>
                    <input
                      type="checkbox"
                      name="hasRear"
                      checked={editForm.hasRear}
                      onChange={handleEditChange}
                    />
                    Achter heeft andere maat
                  </label>
                </div>
                {editForm.hasRear && (
                  <>
                    <div className="form-row">
                      <label htmlFor="editRearWidth">Breedte achter (cm):</label>
                      <input
                        id="editRearWidth"
                        type="number"
                        step="0.1"
                        min="0"
                        name="rearWidth"
                        value={editForm.rearWidth}
                        onChange={handleEditChange}
                        required
                      />
                    </div>
                    <div className="form-row">
                      <label htmlFor="editRearHeight">Hoogte achter (cm):</label>
                      <input
                        id="editRearHeight"
                        type="number"
                        step="0.1"
                        min="0"
                        name="rearHeight"
                        value={editForm.rearHeight}
                        onChange={handleEditChange}
                        required
                      />
                    </div>
                  </>
                )}
                <Button type="submit" variant="primary" className="mt">
                  Bijwerken
                </Button>
              </form>
            </Card>
            <Card title="Kinderauto verwijderen">
              <form onSubmit={handleDeleteSubmit} className="mb-lg">
                <div className="form-row">
                  <label htmlFor="deleteSelect">Selecteer:</label>
                  <select
                    id="deleteSelect"
                    value={deleteName}
                    onChange={(e) => setDeleteName(e.target.value)}
                  >
                    {options.map((opt) => (
                      <option key={opt.name} value={opt.name}>
                        {opt.name}
                      </option>
                    ))}
                  </select>
                </div>
                <Button type="submit" variant="ghost" className="mt">
                  Verwijder
                </Button>
              </form>
            </Card>
          </aside>
        </div>
      )}
      {toast && <div className="toast">Opgeslagen</div>}
    </PageTransition>
  );
}
