import type { User } from "@prisma/client";
import { AdminAutoTextarea } from "@/admin/components/AdminAutoTextarea";
import { AdminFormField, AdminFormSection } from "@/admin/components/AdminFormField";
import { saveStaffAction } from "@/lib/actions";

const permissions = [
  { key: "canManagePosts", label: "Stories / Articles" },
  { key: "canManageVideos", label: "Videos" },
  { key: "canManageGallery", label: "Gallery" },
  { key: "canManageHome", label: "Home Page / Slider / Reviews" },
  { key: "canManagePages", label: "Static Pages" },
  { key: "canManageContact", label: "Contact / Messages" }
] as const;

type StaffEditor = Pick<
  User,
  | "id"
  | "name"
  | "email"
  | "bio"
  | "canManageContact"
  | "canManageGallery"
  | "canManageHome"
  | "canManagePages"
  | "canManagePosts"
  | "canManageVideos"
>;

type StaffFormProps = {
  embedded?: boolean;
  user?: StaffEditor | null;
};

export function StaffForm({ embedded = false, user }: StaffFormProps) {
  const isEdit = Boolean(user);

  return (
    <form
      action={saveStaffAction}
      className={`admin-form${embedded ? " admin-form--embedded" : " glass-panel"}`}
    >
      {user ? <input name="id" type="hidden" value={user.id} /> : null}

      <AdminFormSection title={isEdit ? "Staff profile" : "Account details"} />

      <AdminFormField label="Full name">
        <input
          className="admin-form-control"
          defaultValue={user?.name}
          name="name"
          placeholder="Staff member name"
          required
        />
      </AdminFormField>

      <AdminFormField label="Email address">
        <input
          className="admin-form-control"
          defaultValue={user?.email}
          name="email"
          placeholder="name@justchillnepal.com"
          required
          type="email"
        />
      </AdminFormField>

      <AdminFormField
        hint="Optional notes about their role or responsibilities."
        label="About / role notes"
        span="full"
      >
        <AdminAutoTextarea
          defaultValue={user?.bio ?? ""}
          name="bio"
          placeholder="Example: Gallery uploader, video editor, story writer…"
          variant="compact"
        />
      </AdminFormField>

      <AdminFormField
        hint={isEdit ? "Leave blank to keep the current password." : "Minimum 8 characters. Share securely with the staff member."}
        label={isEdit ? "New password (optional)" : "Temporary password"}
        span="full"
      >
        <input
          className="admin-form-control"
          minLength={8}
          name="password"
          placeholder={isEdit ? "••••••••" : "Create a secure password"}
          required={!isEdit}
          type="password"
        />
      </AdminFormField>

      <AdminFormSection title="Access permissions" />

      <div className="admin-permission-grid">
        {permissions.map((permission) => (
          <label className="checklist-item" key={permission.key}>
            <input
              defaultChecked={user ? Boolean(user[permission.key]) : false}
              name={permission.key}
              type="checkbox"
            />
            {permission.label}
          </label>
        ))}
      </div>

      <button className="admin-btn admin-btn-modify admin-form__submit" type="submit">
        {isEdit ? "Save changes" : "Create staff"}
      </button>
    </form>
  );
}
