import { defineTable } from "convex/server";
import { v } from "convex/values";

// ── Enums ──────────────────────────────────────────────────────────────

export const vehicleTypeValidator = v.union(
  v.literal("CAR"),
  v.literal("SUV"),
  v.literal("VAN"),
  v.literal("TRUCK"),
  v.literal("MOTORCYCLE"),
  v.literal("BUS"),
  v.literal("LUXURY"),
  v.literal("ELECTRIC"),
);

export const rentalStatusValidator = v.union(
  v.literal("pending"),
  v.literal("confirmed"),
  v.literal("active"),
  v.literal("completed"),
  v.literal("cancelled"),
  v.literal("overdue"),
);

export const vehicleFuelTypeValidator = v.union(
  v.literal("PETROL"),
  v.literal("DIESEL"),
  v.literal("ELECTRIC"),
  v.literal("HYBRID"),
);

export const transmissionValidator = v.union(
  v.literal("AUTOMATIC"),
  v.literal("MANUAL"),
);

// ── Tables ─────────────────────────────────────────────────────────────

export const vehicleTables = {
  vehicles: defineTable({
    organizationId: v.string(),
    userId: v.string(),
    make: v.string(),
    model: v.string(),
    year: v.number(),
    type: vehicleTypeValidator,
    color: v.optional(v.string()),
    licensePlate: v.optional(v.string()),
    vin: v.optional(v.string()),
    seats: v.optional(v.number()),
    fuelType: v.optional(vehicleFuelTypeValidator),
    transmission: v.optional(transmissionValidator),
    mileage: v.optional(v.number()),
    pricePerDay: v.number(),
    pricePerWeek: v.optional(v.number()),
    pricePerMonth: v.optional(v.number()),
    currency: v.string(),
    depositAmount: v.optional(v.number()),
    descriptionEn: v.optional(v.string()),
    isAvailable: v.boolean(),
    isActive: v.boolean(),
    totalRentals: v.number(),
    totalRevenue: v.number(),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_organizationId_isActive", ["organizationId", "isActive"])
    .index("by_organizationId_type", ["organizationId", "type"])
    .index("by_organizationId_isAvailable", ["organizationId", "isAvailable"])
    .index("by_userId_isActive", ["userId", "isActive"]),

  vehicle_rentals: defineTable({
    organizationId: v.string(),
    userId: v.string(),
    vehicleId: v.string(),
    customerName: v.string(),
    customerPhone: v.string(),
    customerEmail: v.optional(v.string()),
    customerLicenseNumber: v.optional(v.string()),
    pickupDate: v.number(),
    returnDate: v.number(),
    actualReturnDate: v.optional(v.number()),
    durationDays: v.number(),
    dailyRate: v.number(),
    totalAmount: v.number(),
    depositAmount: v.optional(v.number()),
    depositPaid: v.boolean(),
    depositPaymentId: v.optional(v.string()),
    depositPaymentMethod: v.optional(v.string()),
    amountPaid: v.number(),
    currency: v.string(),
    status: rentalStatusValidator,
    agreementNumber: v.optional(v.string()),
    notes: v.optional(v.string()),
    source: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_organizationId_status", ["organizationId", "status"])
    .index("by_organizationId_pickupDate", ["organizationId", "pickupDate"])
    .index("by_vehicleId_status", ["vehicleId", "status"]),
};
