const test = require("node:test");
const assert = require("node:assert/strict");
const { validPublishedUrl, validateWorkspaceSettings, publicWorkspaceSettings } = require("../src/security/workspaceSettings");

test("booking publication rejects placeholders and insecure URLs", () => {
  assert.equal(validPublishedUrl("https://calendly.com/your-coach/intro"), false);
  assert.equal(validPublishedUrl("http://calendar.test/intro"), false);
  assert.equal(validPublishedUrl("https://calendar.coach.test/intro"), false);
  assert.equal(validPublishedUrl("https://calendar.realcoach.com/intro"), true);
});

test("publishing requires configured identity and contact", () => {
  assert.deepEqual(validateWorkspaceSettings({ published: true, brand_color: "teal", social_links: [], packages: [], testimonials: [] }).sort(), [
    "booking_url_required",
    "coach_bio_required",
    "coach_name_required",
    "contact_email_invalid",
    "legal_contact_email_invalid",
    "legal_entity_name_required",
    "legal_review_required",
    "legal_terms_effective_date_required",
    "practice_name_required",
    "published_package_required",
  ]);
});

test("unpublished settings do not expose workspace identity", () => {
  const value = publicWorkspaceSettings({ practice_name: "Private Practice", contact_email: "private@coach.test", published: false });
  assert.deepEqual(value, { published: false });
});

test("published settings omit invalid URLs and unverified testimonials", () => {
  const value = publicWorkspaceSettings({
    published: true,
    default_booking_url: "https://example.com",
    social_links: [{ label: "Unsafe", href: "http://social.test" }],
    packages: [{ name: "Coaching", booking_url: "https://example.com" }],
    testimonials: [{ quote: "Private", name: "Client", verified: false }],
  });
  assert.equal(value.default_booking_url, null);
  assert.deepEqual(value.social_links, []);
  assert.equal(value.packages[0].booking_url, null);
  assert.deepEqual(value.testimonials, []);
});

test("publication rejects placeholder identity and package booking links", () => {
  const errors = validateWorkspaceSettings({
    published: true,
    practice_name: "Demo Coaching Practice",
    coach_name: "Demo Coach",
    contact_email: "coach@example.com",
    brand_color: "teal",
    social_links: [],
    packages: [{ name: "Coaching", booking_url: "https://example.com/book" }],
    testimonials: [],
  });
  assert.equal(errors.includes("practice_name_placeholder"), true);
  assert.equal(errors.includes("coach_name_placeholder"), true);
  assert.equal(errors.includes("contact_email_invalid"), true);
  assert.equal(errors.includes("packages_invalid"), true);
});

test("workspace assets must use non-placeholder HTTPS URLs", () => {
  const errors = validateWorkspaceSettings({
    published: false,
    logo_url: "http://assets.coach.test/logo.png",
    coach_photo_url: "https://example.com/coach.jpg",
    social_links: [],
    packages: [],
    testimonials: [],
  });
  assert.equal(errors.includes("logo_url_invalid"), true);
  assert.equal(errors.includes("coach_photo_url_invalid"), true);
});
