"use client";

import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { X } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Field, Select } from "@/components/ui/input";

const LEVELS = [
  { value: "", label: "Semua tingkat" },
  { value: "BEGINNER", label: "Pemula" },
  { value: "INTERMEDIATE", label: "Menengah" },
  { value: "ADVANCED", label: "Mahir" },
];

const LANGUAGES = [
  { value: "", label: "Semua bahasa" },
  { value: "JAVASCRIPT", label: "JavaScript" },
  { value: "PYTHON", label: "Python" },
  { value: "ROBOT", label: "Robot" },
];

const STATUSES = [
  { value: "", label: "Semua status" },
  { value: "unsolved", label: "Belum selesai" },
  { value: "solved", label: "Sudah selesai" },
];

/** Filter bar for the coding lab; state lives in the URL. */
export function CodingFilters({
  courses,
  showStatus,
  initial,
}: {
  courses: { id: string; title: string }[];
  showStatus: boolean;
  initial: { difficulty: string; language: string; courseId: string; status: string };
}) {
  const router = useRouter();
  const pathname = usePathname();
  const searchParams = useSearchParams();

  function push(patch: Record<string, string>) {
    const params = new URLSearchParams(searchParams.toString());
    for (const [key, value] of Object.entries(patch)) {
      if (value) params.set(key, value);
      else params.delete(key);
    }
    const search = params.toString();
    router.replace(search ? `${pathname}?${search}` : pathname, { scroll: false });
  }

  const hasFilters = Boolean(
    initial.difficulty || initial.language || initial.courseId || (showStatus && initial.status)
  );

  return (
    <div className="flex flex-col gap-3 rounded-xl border border-subtle bg-surface p-4 shadow-[var(--shadow-card)] lg:flex-row lg:items-end">
      <Field label="Tingkat" htmlFor="coding-difficulty" className="lg:w-44">
        <Select
          id="coding-difficulty"
          defaultValue={initial.difficulty}
          onChange={(event) => push({ difficulty: event.target.value })}
        >
          {LEVELS.map((option) => (
            <option key={option.value} value={option.value}>
              {option.label}
            </option>
          ))}
        </Select>
      </Field>

      <Field label="Bahasa" htmlFor="coding-language" className="lg:w-44">
        <Select
          id="coding-language"
          defaultValue={initial.language}
          onChange={(event) => push({ language: event.target.value })}
        >
          {LANGUAGES.map((option) => (
            <option key={option.value} value={option.value}>
              {option.label}
            </option>
          ))}
        </Select>
      </Field>

      <Field label="Kursus" htmlFor="coding-course" className="flex-1">
        <Select
          id="coding-course"
          defaultValue={initial.courseId}
          onChange={(event) => push({ courseId: event.target.value })}
        >
          <option value="">Semua kursus</option>
          {courses.map((course) => (
            <option key={course.id} value={course.id}>
              {course.title}
            </option>
          ))}
        </Select>
      </Field>

      {showStatus ? (
        <Field label="Status" htmlFor="coding-status" className="lg:w-44">
          <Select
            id="coding-status"
            defaultValue={initial.status}
            onChange={(event) => push({ status: event.target.value })}
          >
            {STATUSES.map((option) => (
              <option key={option.value} value={option.value}>
                {option.label}
              </option>
            ))}
          </Select>
        </Field>
      ) : null}

      {hasFilters ? (
        <Button type="button" variant="ghost" onClick={() => router.replace(pathname, { scroll: false })}>
          <X className="size-4" />
          Reset
        </Button>
      ) : null}
    </div>
  );
}
