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";

interface Role {
  id: number;
  name: string;
}

interface Props {
  roles: Role[];
}

export default function Create({ roles }: Props) {
  const { data, setData, errors, post, processing } = useForm({
    name: "",
    email: "",
    password: "",
    roles: [] as string[],
  });

  function submit(e: React.FormEvent<HTMLFormElement>) {
    e.preventDefault();
    post(route("users.store"));
  }

  return (
    <AuthenticatedLayout header="Create User">
      <Head title="Create 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">Create New 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">Create User Account</CardTitle>
            <CardDescription>Enter user information to create a new 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</Label>
                  <Input
                    id="password"
                    type="password"
                    value={data.password}
                    onChange={(e) => setData("password", e.target.value)}
                    required
                  />
                  {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}
                          id={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 focus:ring-2"
                        />
                        <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 ? "Creating..." : "Create User Account"}
              </Button>
            </form>
          </CardContent>
        </Card>
      </div>
    </AuthenticatedLayout>
  );
}
