"use client";

import { useTranslation } from "react-i18next";

import { Button } from "@/components/ui/Button";
import DateRangePicker from "@/components/ui/DateRangePicker";
import Searchbar from "@/components/ui/Searchbar";
import CheckInWindowBanner from "./CheckInWindowBanner";
import type { CheckInWindow } from "@/features/opportunities/checkInWindow";
import AssetIcon from "@/components/ui/AssetIcon";

interface VolunteerListToolbarProps {
  opportunity_start_date: string | undefined;
  opportunity_end_date: string | undefined;
  selectedDate: string;
  effectiveEndDate: string;
  onDateChange: (date: string) => void;
  onOpenRoleModal: () => void;
  roleDisabled?: boolean;
  attendancePermissionDisabled?: boolean;
  onOpenFilterModal: () => void;
  onSearchChange: (value: string) => void;
  isSearching?: boolean;
  onOpenAddVolunteer: () => void;
  /** Disabled once the opportunity already has everyone it asked for. */
  addVolunteerDisabled?: boolean;
  onSendCertificates: () => void;
  sendCertificatesDisabled?: boolean;
  /** Why the button is disabled, as its tooltip. */
  sendCertificatesTitle?: string;
  onDownloadSheet: () => void;
  downloadDisabled?: boolean;
  /** Organizer-only: «إذن تحضير» is theirs to grant, not a holder's to pass on. */
  onOpenAttendancePermission?: () => void;
  checkInWindow: CheckInWindow;
}

/**
 * Page title, the organizer's action row, the check-in banner, and the
 * search/date/export row.
 *
 * Laid out to the client's mockup: the four actions sit together under the
 * title, the sentence explaining *why* attendance matters comes before the
 * window banner rather than after it, and export moved up beside the filters
 * from the bottom of the page.
 *
 * «الفريق» is gone from here and from the table — the client dropped teams
 * entirely, roles are the only grouping now.
 */
export default function VolunteerListToolbar({
  opportunity_start_date,
  opportunity_end_date,
  selectedDate,
  effectiveEndDate,
  onDateChange,
  onOpenRoleModal,
  roleDisabled = false,
  attendancePermissionDisabled = false,
  onOpenFilterModal,
  onSearchChange,
  isSearching = false,
  onOpenAddVolunteer,
  addVolunteerDisabled = false,
  onSendCertificates,
  sendCertificatesDisabled = false,
  sendCertificatesTitle,
  onDownloadSheet,
  downloadDisabled = false,
  onOpenAttendancePermission,
  checkInWindow,
}: VolunteerListToolbarProps) {
  const { t } = useTranslation();

  return (
    <div className="volunteer-list-toolbar">
      <div className="flex flex-col items-center gap-6">
        <h2 className="2xl:text-[40px] lg:text-[32px] md:text-[30px] text-[24px] text-primary-5 font-bold">
          {t("COMMON.LIST_TITLE")}
        </h2>

        {/* Wraps rather than scrolling, so every button stays reachable on a phone.
            «إذن تحضير» lets a volunteer record attendance for this opportunity only
            (BE-255: adding volunteers is owner-only); only the organizer grants it. */}
        <div className="flex flex-wrap justify-center gap-4 extrasmall:gap-2">
           <Button
            onClick={onOpenAddVolunteer}
            variant="primary"
            size="medium"
            disabled={addVolunteerDisabled}
            className="!w-auto px-5 !text-sm 2xl:!text-base"
          >
            {t("COMMON.ADD_VOLUNTEER")}
          </Button>
           <Button
            onClick={onOpenRoleModal}
            disabled={roleDisabled}
            variant="primary"
            size="medium"
            className="!w-auto px-5 !text-sm 2xl:!text-base"
          >
            {t("COMMON.ADD_ROLE")}
          </Button>
          {onOpenAttendancePermission && (
            <Button
              onClick={onOpenAttendancePermission}
              disabled={attendancePermissionDisabled}
              variant="primary"
              size="medium"
              className="!w-auto px-5 !text-sm 2xl:!text-base"
            >
              {t("COMMON.ATTENDANCE_PERMISSION")}
            </Button>
          )}
          <Button
            onClick={onSendCertificates}
            variant="primary"
            size="medium"
            disabled={sendCertificatesDisabled}
            title={sendCertificatesTitle}
            className="!w-auto px-5 !text-sm 2xl:!text-base"
          >
            {t("COMMON.SEND_CERTIFICATES")}
          </Button>
        </div>
      </div>

      {/* Why attendance matters, then the deadline for doing it — in that order,
          so the banner reads as the consequence of the sentence above it. */}
      {checkInWindow.requiresCheckIn && (
        <p className="mx-auto max-w-3xl pt-8 text-center mobilescreen:text-[16px] text-[18px] text-[#181822CC]/70 leading-relaxed">
          {t("COMMON.CONFIRM_ATTENDANCE")}
        </p>
      )}

      <CheckInWindowBanner
        window={checkInWindow}
        className="mt-4 mb-6 max-w-2xl mx-auto"
      />

      {/*
        BE-61 Part C — «مسح رمز الاستجابة» is gone. It opened `/scan-qr`, where
        the organizer scanned the *volunteer's* personal QR; the client retired
        that direction entirely. Attendance is now either the volunteer's own
        scan of the printed IN/OUT sheet («التحضير الذاتي QR» on the detail
        page) or the manual roster below, which is unchanged.
      */}

      {/*
        Stacked below `lg`, one row above it.

        `DateRangePicker` and `Searchbar` both carry their own width ladder
        ending at `md:w-[350px]`, and only fall back to `w-full` inside
        `mobilescreen` (≤ 767px). So between 768px and the `lg` switch the row
        was already stacked while those two sat at a fixed 350px next to a
        full-width export button — three children, three different widths.

        Rather than edit the shared components, each wrapper forces the inner
        `.searchitms` box to fill it. The wrappers are already `flex-1`, so on
        desktop they were the same width as each other anyway — the fixed 450px
        inner just did not fill them (and slightly overflowed at `max-w-5xl`).
        So this settles both ends: equal widths stacked, and the row above `lg`
        divides evenly instead of overflowing.

        Not scoped with `max-lg:` on purpose — this config registers a dozen
        `raw` screens, which stops Tailwind generating any `max-*` variant at
        all. `max-lg:w-full` compiles to nothing here; verified against the
        real config, not assumed.
      */}
      <div className="mobilescreen:w-[100%] flex justify-center pt-4 pb-12">
        <div className="flex flex-col lg:flex-row gap-4 items-center justify-center w-full max-w-5xl">
          {opportunity_start_date && opportunity_end_date && (
            <div className="w-full lg:w-auto flex-1 [&_.searchitms]:!w-full">
              <DateRangePicker
                selectedDate={selectedDate}
                startDate={opportunity_start_date}
                endDate={effectiveEndDate}
                onDateChange={onDateChange}
                placeholder={t("COMMON.DATES")}
              />
            </div>
          )}
          <div className="w-full lg:w-auto flex-1 [&_.searchitms]:!w-full">
            <Searchbar
              isLoading={isSearching}
              onFilterClick={onOpenFilterModal}
              onSearchChange={onSearchChange}
            />
          </div>
           <Button
            variant="primary"
            size="medium"
            className="w-full lg:w-auto shrink-0 gap-2 flex-shrink-0 justify-center px-4"
            onClick={onDownloadSheet}
            disabled={downloadDisabled}
          >
            <AssetIcon src="/assets/voluneteerevent/downloadsheet.svg" size={34} />
            {t("COMMON.SHEET")}
          </Button>
        </div>
      </div>
    </div>
  );
}
