import {
  pgTable,
  uuid,
  text,
  integer,
  unique,
  index,
  primaryKey,
} from "drizzle-orm/pg-core";
import { merkmale } from "./merkmale";

export const equipmentCategories = pgTable(
  "equipment_categories",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    name: text("name").notNull(),
    reihenfolge: integer("reihenfolge").notNull().default(0),
  },
  (t) => ({ nameUq: unique("equipment_categories_name_uq").on(t.name) }),
);

export const equipmentCategoryMerkmale = pgTable(
  "equipment_category_merkmale",
  {
    categoryId: uuid("category_id")
      .notNull()
      .references(() => equipmentCategories.id, { onDelete: "cascade" }),
    merkmalId: uuid("merkmal_id")
      .notNull()
      .references(() => merkmale.id, { onDelete: "cascade" }),
    reihenfolge: integer("reihenfolge").notNull().default(0),
  },
  (t) => ({
    pk: primaryKey({ columns: [t.categoryId, t.merkmalId] }),
    merkmalIdx: index("ecm_merkmal_idx").on(t.merkmalId),
  }),
);
