/**
 * Tests for Voice Booking Reschedule API
 *
 * Tests the /api/bookings/voice/reschedule endpoint
 */

import { NextRequest } from "next/server";

// Mock dependencies
jest.mock("@/lib/db");

jest.mock("@/lib/logger", () => ({
  logger: {
    info: jest.fn(),
    warn: jest.fn(),
    error: jest.fn(),
  },
}));

// Route not yet implemented - imports skipped
let POST: any;
import { prisma } from "@/lib/db";

// Voice agent routes not yet implemented
describe.skip("POST /api/bookings/voice/reschedule", () => {
  beforeEach(() => {
    jest.clearAllMocks();
    jest.useFakeTimers();
    jest.setSystemTime(new Date("2024-01-15T10:00:00Z"));
  });

  afterEach(() => {
    jest.useRealTimers();
  });

  const createRequest = (body: Record<string, unknown>) => {
    return new NextRequest("http://localhost/api/bookings/voice/reschedule", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(body),
    });
  };

  it("should return 400 if required fields are missing", async () => {
    const request = createRequest({
      booking_id: "booking-123",
    });

    const response = await POST(request);

    expect(response.status).toBe(400);
    const data = await response.json();
    expect(data.error).toBe("Invalid reschedule data");
  });

  it("should return 400 if new date is in the past", async () => {
    const request = createRequest({
      booking_id: "booking-123",
      new_date: "2024-01-10", // Before mock current date
      new_time: "10:00",
      phone_number: "+1234567890",
    });

    const response = await POST(request);

    expect(response.status).toBe(400);
    const data = await response.json();
    expect(data.error).toBe("Cannot reschedule to a past date/time");
  });

  it("should return 404 if booking is not found", async () => {
    (prisma.bookings.findUnique as jest.Mock).mockResolvedValue(null);

    const request = createRequest({
      booking_id: "booking-123",
      new_date: "2024-01-20",
      new_time: "14:00",
      phone_number: "+1234567890",
    });

    const response = await POST(request);

    expect(response.status).toBe(404);
    const data = await response.json();
    expect(data.error).toBe("Booking not found");
  });

  it("should return 403 if phone number does not match", async () => {
    (prisma.bookings.findUnique as jest.Mock).mockResolvedValue({
      id: "booking-123",
      customerPhone: "+9999999999",
      customerName: "John Doe",
      service: "Checkup",
      status: "pending",
    });

    const request = createRequest({
      booking_id: "booking-123",
      new_date: "2024-01-20",
      new_time: "14:00",
      phone_number: "+1234567890",
    });

    const response = await POST(request);

    expect(response.status).toBe(403);
    const data = await response.json();
    expect(data.error).toBe("Phone number does not match booking");
  });

  it("should return 400 if booking is cancelled", async () => {
    (prisma.bookings.findUnique as jest.Mock).mockResolvedValue({
      id: "booking-123",
      customerPhone: "+1234567890",
      customerName: "John Doe",
      service: "Checkup",
      status: "cancelled",
    });

    const request = createRequest({
      booking_id: "booking-123",
      new_date: "2024-01-20",
      new_time: "14:00",
      phone_number: "+1234567890",
    });

    const response = await POST(request);

    expect(response.status).toBe(400);
    const data = await response.json();
    expect(data.error).toBe("Cannot reschedule a cancelled booking");
  });

  it("should reschedule booking successfully", async () => {
    (prisma.bookings.findUnique as jest.Mock).mockResolvedValue({
      id: "booking-123",
      customerPhone: "+1234567890",
      customerName: "John Doe",
      service: "Dental Checkup",
      appointmentDate: new Date("2024-01-18"),
      appointmentTime: "10:00",
      staffMember: "Dr. Smith",
      status: "confirmed",
      userId: "user-1",
    });

    (prisma.bookings.update as jest.Mock).mockResolvedValue({});

    const request = createRequest({
      booking_id: "booking-123",
      new_date: "2024-01-25",
      new_time: "15:30",
      phone_number: "+1234567890",
    });

    const response = await POST(request);

    expect(response.status).toBe(200);
    const data = await response.json();

    expect(data.success).toBe(true);
    expect(data.message).toContain("rescheduled");
    expect(data.rescheduled_booking.new_date).toBe("2024-01-25");
    expect(data.rescheduled_booking.new_time).toBe("15:30");
    expect(data.rescheduled_booking.doctor).toBe("Dr. Smith");

    // Verify booking was updated
    expect(prisma.bookings.update).toHaveBeenCalledWith({
      where: { id: "booking-123" },
      data: expect.objectContaining({
        appointmentTime: "15:30",
        status: "pending", // Reset to pending
        notes: expect.stringContaining("Rescheduled via phone"),
      }),
    });
  });
});
