import { pgTable, text, serial, timestamp, integer, jsonb } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { z } from "zod/v4";

export const timetablesTable = pgTable("timetables", {
  id: serial("id").primaryKey(),
  planId: integer("plan_id").notNull().unique(),
  termStartDate: text("term_start_date"),
  termEndDate: text("term_end_date"),
  // Array of {dayOfWeek: 0-6 (Sun=0), startTime: "HH:MM", endTime: "HH:MM", venue: string}
  teachingSlots: jsonb("teaching_slots").notNull().default([]),
  // Array of date strings "YYYY-MM-DD"
  holidays: jsonb("holidays").notNull().default([]),
  excludedDates: jsonb("excluded_dates").notNull().default([]),
  notes: text("notes"),
  createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
  updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow().$onUpdate(() => new Date()),
});

export const insertTimetableSchema = createInsertSchema(timetablesTable).omit({ id: true, createdAt: true, updatedAt: true });
export type InsertTimetable = z.infer<typeof insertTimetableSchema>;
export type Timetable = typeof timetablesTable.$inferSelect;
