import {
  pgTable,
  text,
  timestamp,
  uuid,
  varchar,
  date,
  index,
} from "drizzle-orm/pg-core";

// ---------------------------------------------------------------------------
// Medical schema — aligned to PRODUCTION as of 2026-05-19 baseline.
// Tables: medicines, medication_doses, allergies, illness_logs, doctor_visits
// (legacy migrations 0003_medical_data, 0012_medical_doses).
//
// Note: `medicines` here is the per-child medicine list with reminder times.
// `medications` (in logs.ts) is a separate prescription-record table that
// predates it. Both exist in prod; keep them distinct.
// ---------------------------------------------------------------------------

export const medicines = pgTable("medicines", {
  id: uuid("id").primaryKey().defaultRandom(),
  childId: uuid("child_id").notNull(),
  name: varchar("name", { length: 255 }).notNull(),
  dose: varchar("dose", { length: 255 }),
  notes: text("notes"),
  reminderTime: varchar("reminder_time", { length: 10 }),
  createdAt: timestamp("created_at", { withTimezone: true }).defaultNow(),
  updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow(),
});

// Append-only dose administration log. Corrections go via log_corrections.
export const medicationDoses = pgTable(
  "medication_doses",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    medicineId: uuid("medicine_id").notNull(),
    familyId: uuid("family_id").notNull(),
    administeredAt: timestamp("administered_at", { withTimezone: true })
      .defaultNow()
      .notNull(),
    administeredBy: uuid("administered_by"),
    amountGiven: text("amount_given"),
    notes: text("notes"),
    createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
  },
  (table) => [
    index("medication_doses_family_idx").on(table.familyId),
    index("medication_doses_medicine_idx").on(table.medicineId),
  ]
);

export const allergies = pgTable("allergies", {
  id: uuid("id").primaryKey().defaultRandom(),
  childId: uuid("child_id").notNull(),
  name: varchar("name", { length: 255 }).notNull(),
  severity: varchar("severity", { length: 50 }).default("mild"),
  notes: text("notes"),
  createdAt: timestamp("created_at", { withTimezone: true }).defaultNow(),
  updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow(),
});

export const illnessLogs = pgTable("illness_logs", {
  id: uuid("id").primaryKey().defaultRandom(),
  childId: uuid("child_id").notNull(),
  name: varchar("name", { length: 255 }).notNull(),
  startDate: date("start_date").notNull(),
  endDate: date("end_date"),
  notes: text("notes"),
  createdAt: timestamp("created_at", { withTimezone: true }).defaultNow(),
  updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow(),
});

export const doctorVisits = pgTable("doctor_visits", {
  id: uuid("id").primaryKey().defaultRandom(),
  childId: uuid("child_id").notNull(),
  doctorName: varchar("doctor_name", { length: 255 }).notNull(),
  reason: varchar("reason", { length: 255 }),
  visitDate: date("visit_date").notNull(),
  notes: text("notes"),
  createdAt: timestamp("created_at", { withTimezone: true }).defaultNow(),
  updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow(),
});

export type Medicine = typeof medicines.$inferSelect;
export type MedicationDose = typeof medicationDoses.$inferSelect;
export type Allergy = typeof allergies.$inferSelect;
export type IllnessLog = typeof illnessLogs.$inferSelect;
export type DoctorVisit = typeof doctorVisits.$inferSelect;
