import assert from "node:assert/strict";
import test from "node:test";
import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { BookingPaymentConditions, bookingRefundPending, type BookingPaymentConditionsData } from "../src/components/booking-payment-conditions";

const render = (data: BookingPaymentConditionsData) => renderToStaticMarkup(createElement(BookingPaymentConditions, { data }));

test("booking terms show a concrete deposit and a Moscow cutoff even after payment", () => {
  const html = render({ deposit_required: 3000, prepayment_total: 3000, refund_cutoff: "2026-11-09T19:00:00Z" });
  assert.match(html, /3000/);
  assert.match(html, /22:00/);
  assert.match(html, /часть или вся предоплата может быть удержана/);
  assert.doesNotMatch(html, /Внести предоплату/);
});

test("payment needs both backend permission and an actual link", () => {
  assert.doesNotMatch(render({ deposit_required: 3000, payment_url: "https://example.test/payment", can_pay: false }), /href=/);
  assert.doesNotMatch(render({ deposit_required: 3000, can_pay: true }), /href=/);
  const html = render({ deposit_required: 3000, can_pay: true, payment_url: "https://example.test/payment", payment_deadline_at: "2026-11-09T16:15:00Z" });
  assert.match(html, /Внести предоплату/);
  assert.match(html, /19:15/);
  assert.match(html, /15 минут/);
});

test("insufficient tables means restaurant approval before payment, without raw null", () => {
  const html = render({ deposit_required: 3000, requires_confirmation: true });
  assert.match(html, /Оператор проверит/);
  assert.match(html, /после подтверждения оператором/);
  assert.doesNotMatch(html, /null|undefined|href=/);
});

test("a small company has no deposit requirement", () => {
  assert.doesNotMatch(render({ deposit_required: 0 }), /Требуется предоплата|удержана|href=/);
});

test("SBIS-confirmed return is complete without waiting for bank posting", () => {
  const html = render({ cancellation: { refund_state: "confirmed", refund_eligible: true, refund_amount: 3000, refund_source: "sbis" } });
  assert.match(html, /Возврат предоплаты/);
  assert.match(html, /оформлен/);
  assert.doesNotMatch(html, /Оформляем|30 дней|не выполнен|свяжитесь/);
});

test("late cancellation keeps restaurant-contact policy and never promises an automatic return", () => {
  const html = render({ cancellation: { refund_state: "contact_restaurant", refund_eligible: false } });
  assert.match(html, /свяжитесь с рестораном/);
  assert.doesNotMatch(html, /оформлен/);
});

test("partial SBIS return shows the actual amount and keeps readback active", () => {
  const cancellation = { refund_state: "partial", refund_eligible: true, refund_amount: 1000 };
  assert.match(render({ cancellation }), /Частичный возврат предоплаты/);
  assert.match(render({ cancellation }), /1[^0-9]*000/);
  assert.equal(bookingRefundPending(cancellation), true);
  assert.equal(bookingRefundPending({ ...cancellation, refund_state: "confirmed" }), false);
});
