import { pgTable, text, serial, integer, timestamp } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { z } from "zod/v4";
import { usersTable } from "./users";
import { profilesTable } from "./profiles";
import { locationsTable } from "./locations";
import { servicesTable } from "./services";

export const bookingsTable = pgTable("bookings", {
  id: serial("id").primaryKey(),
  userId: integer("user_id").notNull().references(() => usersTable.id),
  profileId: integer("profile_id").references(() => profilesTable.id),
  locationId: integer("location_id").references(() => locationsTable.id),
  serviceId: integer("service_id").references(() => servicesTable.id),
  duration: text("duration", { enum: ["1h", "2h", "full_day", "night"] }),
  locationType: text("location_type", { enum: ["in_studio", "customer_location"] }),
  totalPrice: integer("total_price"),
  status: text("status", {
    enum: ["pending", "accepted", "paid", "proof_uploaded", "completed", "rejected", "cancelled", "refunded"],
  }).notNull().default("pending"),
  notes: text("notes"),
  scheduledAt: timestamp("scheduled_at", { withTimezone: true }),
  createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
  updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow().$onUpdate(() => new Date()),
});

export const insertBookingSchema = createInsertSchema(bookingsTable).omit({ id: true, createdAt: true, updatedAt: true });
export type InsertBooking = z.infer<typeof insertBookingSchema>;
export type Booking = typeof bookingsTable.$inferSelect;
