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

export const plansTable = pgTable("plans", {
  id: serial("id").primaryKey(),
  unitTitle: text("unit_title").notNull(),
  unitCode: text("unit_code"),
  level: text("level"),
  trainerName: text("trainer_name"),
  className: text("class_name"),
  numberOfTrainees: integer("number_of_trainees"),
  totalDuration: text("total_duration"),
  deliveryMode: text("delivery_mode"),
  numberOfSessions: integer("number_of_sessions"),
  sessionDuration: text("session_duration"),
  institutionId: integer("institution_id"),
  departmentId: integer("department_id"),
  createdById: text("created_by_id"),
  templateId: integer("template_id"),
  status: text("status").notNull().default("draft"),
  versionNumber: integer("version_number").notNull().default(1),
  complianceFlags: text("compliance_flags"),
  improvementNotes: text("improvement_notes"),
  dateOfPreparation: text("date_of_preparation"),
  dateOfRevision: text("date_of_revision"),
  createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
  updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow().$onUpdate(() => new Date()),
});

export const insertPlanSchema = createInsertSchema(plansTable).omit({ id: true, createdAt: true, updatedAt: true });
export type InsertPlan = z.infer<typeof insertPlanSchema>;
export type Plan = typeof plansTable.$inferSelect;
