"use client";

import { useState } from "react";
import { useTranslation } from "@enode/core/translations/hooks";
import { ProgressRing } from "@enode/ui/charts/progress-ring";
import { ChevronRightIcon, CloseXIcon, SunMaxFillIcon } from "@enode/ui/icons";
import { Sheet } from "@enode/ui/sheet";
import { YourWeekCard, type YourWeekCardProps } from "./your-week-card";
import { HEADER_ICON_BUTTON } from "@enode/ui/header-button";

// "Your week" for someone who looks after other people: one line on Today
// instead of the card.
//
// A coach's Today is about the gym — the schedule, the athletes who still owe a
// check-in. Their own readiness is theirs, not the room's, so it does not get
// a card there; but hidden in a drawer it would not be answered at all. One
// row keeps it a tap away: while today's check-in is open it opens the
// questions, afterwards it shows the total and opens the full card in a sheet.

const SCORE_MAX = 25;

/**
 * Whether the row shows at all. While the check-in is open (`askNow`) it is
 * hidden: for owners/coaches the daily check-in prompt in the notifications
 * menu is where they start it. Once answered, the summary row comes back.
 */
export function yourWeekRowVisible(
  props: Pick<YourWeekCardProps, "readiness" | "load" | "onCheckIn">,
): boolean {
  if (!props.readiness && !props.load) return false;
  return props.onCheckIn === undefined;
}

export function YourWeekRow(props: YourWeekCardProps) {
  const { t } = useTranslation();
  const [open, setOpen] = useState(false);
  const { readiness, checkedInToday } = props;
  if (!yourWeekRowVisible(props)) return null;

  const total = readiness?.today ?? null;

  return (
    <>
      <button
        type="button"
        onClick={() => setOpen(true)}
        className="flex min-h-14 w-full items-center gap-3 rounded-2xl border border-surface-stroke bg-surface-base px-4 py-2.5 text-left hover:bg-surface-base-muted active:bg-surface-base-muted"
      >
        {total !== null ? (
          <ProgressRing
            value={total / SCORE_MAX}
            size={32}
            lineWidth={4}
            color="var(--foreground-platinum-green-bright)"
            trackColor="var(--surface-base-muted)"
          />
        ) : (
          <span className="flex size-8 shrink-0 items-center justify-center rounded-full bg-surface-base-muted text-foreground-muted">
            <SunMaxFillIcon className="size-4" />
          </span>
        )}
        <span className="min-w-0 flex-1">
          <span className="body-prominent block text-foreground-default">
            {total !== null
              ? t("You · {total} of {max} today", { total, max: SCORE_MAX })
              : checkedInToday
                ? t("You · checked in today")
                : t("Your week")}
          </span>
          <span className="body-small block text-foreground-muted">
            {t("Your readiness and training load")}
          </span>
        </span>
        <ChevronRightIcon className="size-4 shrink-0 text-foreground-muted" />
      </button>

      <Sheet
        open={open}
        onClose={() => setOpen(false)}
        slideFrom="bottom"
        ariaLabel={t("Your week")}
        backdrop
        className="flex flex-col bg-background pt-[env(safe-area-inset-top)] pb-[env(safe-area-inset-bottom)]"
      >
        <header className="flex shrink-0 items-center justify-between gap-3 px-5 pt-4 pb-2">
          <h1 className="heading-normal">{t("Your week")}</h1>
          <button
            type="button"
            onClick={() => setOpen(false)}
            aria-label={t("Close")}
            className={HEADER_ICON_BUTTON}
          >
            <CloseXIcon className="size-5" />
          </button>
        </header>
        <div className="scrollbar-none min-h-0 flex-1 overflow-y-auto px-5 pt-2 pb-6">
          <div className="mx-auto w-full max-w-[560px]">
            <YourWeekCard
              {...props}
              // The questions open over Today, not over this sheet — and
              // Today is where the new total is then read.
              onChangeCheckIn={
                props.onChangeCheckIn &&
                (() => {
                  setOpen(false);
                  props.onChangeCheckIn?.();
                })
              }
              bare
            />
          </div>
        </div>
      </Sheet>
    </>
  );
}
