import { relations } from "drizzle-orm";
import { brigades } from "./brigades";
import { users } from "./users";
import { merkmale, merkmalOptionen } from "./merkmale";
import {
  vehicleTemplates,
  vehicleTemplateMerkmale,
  vehicleTemplateAliasse,
} from "./templates";
import {
  equipmentCategories,
  equipmentCategoryMerkmale,
} from "./equipment-categories";
import { vehicles, equipment } from "./assets";
import { merkmalValues } from "./merkmal-values";
import { auditLog } from "./audit";

export const brigadesRelations = relations(brigades, ({ many }) => ({
  users: many(users),
  vehicles: many(vehicles),
  equipment: many(equipment),
}));

export const usersRelations = relations(users, ({ one }) => ({
  brigade: one(brigades, {
    fields: [users.brigadeId],
    references: [brigades.id],
  }),
}));

export const merkmaleRelations = relations(merkmale, ({ many, one }) => ({
  optionen: many(merkmalOptionen),
  values: many(merkmalValues),
  templateMerkmale: many(vehicleTemplateMerkmale),
  categoryMerkmale: many(equipmentCategoryMerkmale),
  vorgeschlagenVonBrigade: one(brigades, {
    fields: [merkmale.vorgeschlagenVonBrigadeId],
    references: [brigades.id],
  }),
}));

export const merkmalOptionenRelations = relations(merkmalOptionen, ({ one }) => ({
  merkmal: one(merkmale, {
    fields: [merkmalOptionen.merkmalId],
    references: [merkmale.id],
  }),
}));

export const vehicleTemplatesRelations = relations(
  vehicleTemplates,
  ({ many }) => ({
    merkmale: many(vehicleTemplateMerkmale),
    aliasse: many(vehicleTemplateAliasse),
    vehicles: many(vehicles),
  }),
);

export const vehicleTemplateMerkmaleRelations = relations(
  vehicleTemplateMerkmale,
  ({ one }) => ({
    template: one(vehicleTemplates, {
      fields: [vehicleTemplateMerkmale.templateId],
      references: [vehicleTemplates.id],
    }),
    merkmal: one(merkmale, {
      fields: [vehicleTemplateMerkmale.merkmalId],
      references: [merkmale.id],
    }),
  }),
);

export const vehicleTemplateAliasseRelations = relations(
  vehicleTemplateAliasse,
  ({ one }) => ({
    template: one(vehicleTemplates, {
      fields: [vehicleTemplateAliasse.templateId],
      references: [vehicleTemplates.id],
    }),
  }),
);

export const equipmentCategoriesRelations = relations(
  equipmentCategories,
  ({ many }) => ({
    merkmale: many(equipmentCategoryMerkmale),
    equipment: many(equipment),
  }),
);

export const equipmentCategoryMerkmaleRelations = relations(
  equipmentCategoryMerkmale,
  ({ one }) => ({
    category: one(equipmentCategories, {
      fields: [equipmentCategoryMerkmale.categoryId],
      references: [equipmentCategories.id],
    }),
    merkmal: one(merkmale, {
      fields: [equipmentCategoryMerkmale.merkmalId],
      references: [merkmale.id],
    }),
  }),
);

export const vehiclesRelations = relations(vehicles, ({ one, many }) => ({
  brigade: one(brigades, {
    fields: [vehicles.brigadeId],
    references: [brigades.id],
  }),
  template: one(vehicleTemplates, {
    fields: [vehicles.templateId],
    references: [vehicleTemplates.id],
  }),
  equipment: many(equipment),
}));

export const equipmentRelations = relations(equipment, ({ one }) => ({
  brigade: one(brigades, {
    fields: [equipment.brigadeId],
    references: [brigades.id],
  }),
  category: one(equipmentCategories, {
    fields: [equipment.categoryId],
    references: [equipmentCategories.id],
  }),
  vehicle: one(vehicles, {
    fields: [equipment.vehicleId],
    references: [vehicles.id],
  }),
}));

export const merkmalValuesRelations = relations(merkmalValues, ({ one }) => ({
  merkmal: one(merkmale, {
    fields: [merkmalValues.merkmalId],
    references: [merkmale.id],
  }),
}));

export const auditLogRelations = relations(auditLog, ({ one }) => ({
  actor: one(users, {
    fields: [auditLog.actorUserId],
    references: [users.id],
  }),
}));
