/**
 * Test: Appointment timezone handling
 * Ensures appointments are created and synced to calendar with correct local time
 */

import { describe, it, expect } from "@jest/globals";

describe("Appointment Timezone Handling", () => {
  it("should parse appointment date in local timezone, not UTC", () => {
    // Simulate the fix: parse date string as local date
    const appointmentDate = "2026-01-27";
    const [year, month, day] = appointmentDate.split("-").map(Number);
    const localDate = new Date(year, month - 1, day, 0, 0, 0, 0);

    // Verify it's midnight local time, not UTC
    expect(localDate.getFullYear()).toBe(2026);
    expect(localDate.getMonth()).toBe(0); // January (0-indexed)
    expect(localDate.getDate()).toBe(27);
    expect(localDate.getHours()).toBe(0);
    expect(localDate.getMinutes()).toBe(0);

    // The old buggy way (for comparison)
    const utcDate = new Date(appointmentDate);
    // This would parse as UTC midnight, which is different from local midnight
    // in non-UTC timezones
  });

  it("should create calendar event with correct local time", () => {
    // Simulate the fix: create startDateTime in local timezone
    const appointmentDate = "2026-01-27";
    const appointmentTime = "09:00";

    // Parse date in local timezone
    const [year, month, day] = appointmentDate.split("-").map(Number);
    const dateObj = new Date(year, month - 1, day, 0, 0, 0, 0);

    // Add time in local timezone
    const [hours, minutes] = appointmentTime.split(":").map(Number);
    const startDateTime = new Date(
      dateObj.getFullYear(),
      dateObj.getMonth(),
      dateObj.getDate(),
      hours,
      minutes,
      0,
      0,
    );

    // Verify time is set correctly in local timezone
    expect(startDateTime.getHours()).toBe(9);
    expect(startDateTime.getMinutes()).toBe(0);
    expect(startDateTime.getDate()).toBe(27);
    expect(startDateTime.getMonth()).toBe(0); // January

    // When sent to Google Calendar API, toISOString() will convert to UTC correctly
    const isoString = startDateTime.toISOString();
    expect(isoString).toContain("2026-01-27");
    // The time in ISO string will be offset by timezone (e.g., 06:00:00Z for UTC+3)
  });

  it("should calculate end time correctly", () => {
    const appointmentDate = "2026-01-27";
    const appointmentTime = "09:00";
    const duration = 30; // minutes

    // Create start time
    const [year, month, day] = appointmentDate.split("-").map(Number);
    const dateObj = new Date(year, month - 1, day, 0, 0, 0, 0);
    const [hours, minutes] = appointmentTime.split(":").map(Number);
    const startDateTime = new Date(
      dateObj.getFullYear(),
      dateObj.getMonth(),
      dateObj.getDate(),
      hours,
      minutes,
      0,
      0,
    );

    // Calculate end time
    const endDateTime = new Date(startDateTime);
    endDateTime.setMinutes(endDateTime.getMinutes() + duration);

    // Verify end time
    expect(endDateTime.getHours()).toBe(9);
    expect(endDateTime.getMinutes()).toBe(30);
    expect(endDateTime.getDate()).toBe(27);
  });
});
