"use client";

import { useEffect } from "react";
import { tasksDict } from "@/features/tasks/dict";

type ItemDescriptionSheetProps = {
  open: boolean;
  title: string;
  description: string;
  onClose: () => void;
};

export function ItemDescriptionSheet({
  open,
  title,
  description,
  onClose,
}: ItemDescriptionSheetProps) {
  useEffect(() => {
    if (!open) {
      return;
    }

    const handleEscape = (event: KeyboardEvent) => {
      if (event.key === "Escape") {
        onClose();
      }
    };

    document.addEventListener("keydown", handleEscape);
    document.body.style.overflow = "hidden";

    return () => {
      document.removeEventListener("keydown", handleEscape);
      document.body.style.overflow = "";
    };
  }, [open, onClose]);

  if (!open) {
    return null;
  }

  return (
    <div className="fixed inset-0 z-[100] flex items-end justify-center bg-black/40 p-0 sm:items-center sm:p-4">
      <button
        type="button"
        aria-label={tasksDict.cancel}
        className="absolute inset-0"
        onClick={onClose}
      />

      <div className="relative z-10 flex max-h-[85vh] w-full max-w-lg flex-col overflow-hidden rounded-t-3xl bg-card shadow-soft sm:rounded-3xl">
        <div className="shrink-0 px-6 pt-4">
          <div className="mx-auto mb-4 h-1.5 w-12 rounded-full bg-border sm:hidden" />
          <div className="space-y-1 pb-4 text-center sm:text-start">
            <h2 className="text-xl font-bold text-card-foreground">
              {tasksDict.descriptionLabel}
            </h2>
            <p className="text-sm font-medium text-card-foreground">{title}</p>
          </div>
        </div>

        <div className="min-h-0 flex-1 overflow-y-auto px-6 pb-8">
          <p className="whitespace-pre-wrap text-sm leading-7 text-muted-foreground">
            {description}
          </p>
        </div>
      </div>
    </div>
  );
}
