import { Link } from "@tanstack/react-router";

import type { Workout } from "../../types";
import { Metric } from "../../shared/Metric";

function statusLabel(status: Workout["status"]) {
  return status === "finished" ? "завершена" : status === "discarded" ? "отменена" : "активная";
}

export function WorkoutSummary({ workout }: { workout: Workout }) {
  const duration = workout.finished_at
    ? Math.round((new Date(workout.finished_at).getTime() - new Date(workout.started_at).getTime()) / 60000)
    : null;

  return (
    <section className={`card workout-summary status-${workout.status}`}>
      <div className="summary-title">
        <div>
          <span className="status-chip">{statusLabel(workout.status)}</span>
          <h3>{new Date(workout.started_at).toLocaleString("ru-RU")}</h3>
          <p>{workout.notes || "Без заметок"}</p>
        </div>
        <Link to="/workouts/$workoutId" params={{ workoutId: workout.id }} className="ghost detail-link">Детали</Link>
      </div>
      <div className="workout-stats">
        <Metric label="Длительность" value={duration === null ? "идет" : `${duration} мин`} />
        <Metric label="Упражнения" value={workout.items.length} />
        <Metric label="Подходы" value={workout.total_sets} />
        <Metric label="Объем" value={`${Math.round(workout.total_volume)} кг`} />
        <Metric label="Ккал" value={`~${Math.round(workout.estimated_calories ?? 0)}`} />
      </div>
    </section>
  );
}
