"use client";

import { useRouter } from "next/navigation";
import { useState } from "react";
import type { InquiryStatus } from "@/lib/admin/types";

type Props = {
  id: string;
  type: "contact" | "repair";
  status: InquiryStatus;
};

export default function StatusSelect({ id, type, status }: Props) {
  const router = useRouter();
  const [value, setValue] = useState(status);

  async function update(nextStatus: InquiryStatus) {
    setValue(nextStatus);
    const response = await fetch(`/api/admin/${type}/${id}`, {
      method: "PATCH",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ status: nextStatus }),
    });
    if (response.ok) router.refresh();
  }

  return (
    <select className="status-select" value={value} onChange={(event) => update(event.target.value as InquiryStatus)}>
      <option value="new">Neu</option>
      <option value="in_progress">In Prüfung</option>
      <option value="done">Erledigt</option>
    </select>
  );
}
