import {
  calculateRentalPrice,
  formatVehiclePrice,
} from "@/lib/types/vehicles.types";

describe("calculateRentalPrice", () => {
  const vehicle = {
    pricePerDay: 15000, // QAR 150.00 in fils
    pricePerWeek: 90000, // QAR 900.00
    pricePerMonth: 300000, // QAR 3000.00
  } as any;

  it("uses daily rate for < 7 days", () => {
    const { dailyRate, totalAmount } = calculateRentalPrice(vehicle, 3);
    expect(dailyRate).toBe(15000);
    expect(totalAmount).toBe(45000);
  });

  it("uses weekly rate for 7 days (exact week)", () => {
    const { totalAmount } = calculateRentalPrice(vehicle, 7);
    // 1 week * 90000 + 0 remaining days
    expect(totalAmount).toBe(90000);
  });

  it("uses weekly rate + daily remainder for 10 days", () => {
    const { totalAmount } = calculateRentalPrice(vehicle, 10);
    // 1 week * 90000 + 3 days * 15000 = 135000
    expect(totalAmount).toBe(135000);
  });

  it("uses monthly rate for 30 days", () => {
    const { totalAmount } = calculateRentalPrice(vehicle, 30);
    // months = 30/30 = 1, total = 1 * 300000 = 300000
    expect(totalAmount).toBe(300000);
  });

  it("uses monthly rate for 28 days", () => {
    const { totalAmount } = calculateRentalPrice(vehicle, 28);
    // months = 28/30 ≈ 0.9333, total = round(0.9333 * 300000) = 280000
    expect(totalAmount).toBe(Math.round((28 / 30) * 300000));
  });

  it("falls back to daily when no weekly rate set", () => {
    const v = { pricePerDay: 10000 } as any;
    const { dailyRate, totalAmount } = calculateRentalPrice(v, 7);
    expect(dailyRate).toBe(10000);
    expect(totalAmount).toBe(70000);
  });

  it("falls back to weekly when no monthly rate set", () => {
    const v = { pricePerDay: 10000, pricePerWeek: 60000 } as any;
    const { totalAmount } = calculateRentalPrice(v, 28);
    // 4 weeks * 60000 + 0 remaining days = 240000
    expect(totalAmount).toBe(240000);
  });

  it("returns correct dailyRate as average", () => {
    const { dailyRate, totalAmount } = calculateRentalPrice(vehicle, 10);
    expect(dailyRate).toBe(Math.round(totalAmount / 10));
  });
});

describe("formatVehiclePrice", () => {
  it("formats fils to display string", () => {
    expect(formatVehiclePrice(15000, "QAR")).toBe("QAR 150.00");
  });

  it("handles zero", () => {
    expect(formatVehiclePrice(0, "AED")).toBe("AED 0.00");
  });

  it("uses QAR as default currency", () => {
    expect(formatVehiclePrice(10000)).toBe("QAR 100.00");
  });
});
