import AuthenticatedLayout from "@/layouts/authenticated-layout";
import { Head, Link, useForm } from "@inertiajs/react";
import {
  Card,
  CardHeader,
  CardTitle,
  CardDescription,
  CardContent,
} from "@/components/ui/card";
import { Label } from "@/components/ui/label";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";

type Role = {
  id: number;
  name: string;
};

type User = {
  id: number;
  name: string;
  email: string;
  role: string;
};

type Props = {
  user: User;
  userRoles: string[]; // assuming it's an array of role names
  roles: Role[];
};

export default function Edit({ user, userRoles, roles }: Props) {
  const { data, setData, errors, put, processing } = useForm({
    name: user.name || "",
    email: user.email || "",
    password: "",
    roles: userRoles || [],
  });

  function submit(e: React.FormEvent<HTMLFormElement>) {
    e.preventDefault();
    put(route("users.update", user.id));
  }

  return (
    <AuthenticatedLayout header="Edit User">
      <Head title="Edit User" />

      <div className="container mx-auto px-4 py-6 max-w-4xl">
        <div className="flex justify-between items-center mb-6">
          <h1 className="text-2xl font-bold">Edit User</h1>
          <Link
            href={route("users.index")}
            className="px-4 py-2 bg-gray-200 hover:bg-gray-300 rounded-md transition duration-200"
          >
            Back to Users
          </Link>
        </div>

        <Card className="max-w-lg mx-auto">
          <CardHeader>
            <CardTitle className="text-xl">Edit User Account</CardTitle>
            <CardDescription>
              Enter user information to update the account.
            </CardDescription>
          </CardHeader>
          <CardContent>
            <form onSubmit={submit}>
              <div className="grid gap-4 mb-4">
                <div className="grid gap-2">
                  <Label htmlFor="name">Full Name</Label>
                  <Input
                    id="name"
                    placeholder="John Doe"
                    value={data.name}
                    onChange={(e) => setData("name", e.target.value)}
                    required
                    autoFocus
                  />
                  {errors.name && (
                    <div className="text-red-500 text-sm">{errors.name}</div>
                  )}
                </div>

                <div className="grid gap-2">
                  <Label htmlFor="email">Email</Label>
                  <Input
                    id="email"
                    type="email"
                    placeholder="user@example.com"
                    value={data.email}
                    onChange={(e) => setData("email", e.target.value)}
                    required
                  />
                  {errors.email && (
                    <div className="text-red-500 text-sm">{errors.email}</div>
                  )}
                </div>

                <div className="grid gap-2">
                  <Label htmlFor="password">Password (leave blank to keep current)</Label>
                  <Input
                    id="password"
                    type="password"
                    value={data.password}
                    onChange={(e) => setData("password", e.target.value)}
                  />
                  {errors.password && (
                    <div className="text-red-500 text-sm">{errors.password}</div>
                  )}
                </div>

                <div className="grid gap-2">
                  <Label htmlFor="roles">Roles</Label>
                  <div className="flex flex-col gap-2">
                    {roles.map((role) => (
                      <label key={role.id} className="flex items-center space-x-2">
                        <input
                          type="checkbox"
                          value={role.name}
                          checked={data.roles.includes(role.name)}
                          onChange={(e) => {
                            if (e.target.checked) {
                              setData("roles", [...data.roles, role.name]);
                            } else {
                              setData("roles", data.roles.filter((r) => r !== role.name));
                            }
                          }}
                          className="form-checkbox h-5 w-5 text-blue-600 rounded"
                        />
                        <span>{role.name}</span>
                      </label>
                    ))}
                    {errors.roles && (
                      <div className="text-red-500 text-sm">{errors.roles}</div>
                    )}
                  </div>
                </div>
              </div>

              <Button type="submit" className="w-full" disabled={processing}>
                {processing ? "Updating..." : "Update User"}
              </Button>
            </form>
          </CardContent>
        </Card>
      </div>
    </AuthenticatedLayout>
  );
}
