import { relations } from "drizzle-orm";

import { users } from "./auth";
import { accounts } from "./auth";
import { sessions } from "./auth";
import { deviceTokens } from "./auth";
import { familyGroups, familyGroupMembers, subscriptions } from "./subscription";
import { invitations } from "./invitation";
import { watchlistItems, exposures } from "./darkwatch";
import { alerts } from "./alerts";
import { voiceEnrollments, voiceAnalyses, analysisJobs, analysisResults } from "./voiceprint";
import { spamFeedback, spamRules } from "./spamshield";
import { normalizedAlerts, correlationGroups } from "./correlation";
import { securityReports } from "./reports";
import { propertyWatchlistItems, propertySnapshots, propertyChanges } from "./hometitle";
import { infoBrokers, removalRequests, brokerListings } from "./removebrokers";

export const usersRelations = relations(users, ({ many }) => ({
  accounts: many(accounts),
  sessions: many(sessions),
  deviceTokens: many(deviceTokens),
  familyGroups: many(familyGroupMembers),
  familyGroupOwned: many(familyGroups),
  subscriptions: many(subscriptions),
  invitationsSent: many(invitations),
  alerts: many(alerts),
  voiceEnrollments: many(voiceEnrollments),
  voiceAnalyses: many(voiceAnalyses),
  spamFeedback: many(spamFeedback),
  spamRules: many(spamRules),
  normalizedAlerts: many(normalizedAlerts),
  correlationGroups: many(correlationGroups),
  securityReports: many(securityReports),
  analysisJobs: many(analysisJobs),
}));

export const accountsRelations = relations(accounts, ({ one }) => ({
  user: one(users, { fields: [accounts.userId], references: [users.id] }),
}));

export const sessionsRelations = relations(sessions, ({ one }) => ({
  user: one(users, { fields: [sessions.userId], references: [users.id] }),
}));

export const deviceTokensRelations = relations(deviceTokens, ({ one }) => ({
  user: one(users, { fields: [deviceTokens.userId], references: [users.id] }),
}));

export const familyGroupsRelations = relations(familyGroups, ({ one, many }) => ({
  owner: one(users, { fields: [familyGroups.ownerId], references: [users.id] }),
  members: many(familyGroupMembers),
  subscriptions: many(subscriptions),
  invitations: many(invitations),
}));

export const invitationsRelations = relations(invitations, ({ one }) => ({
  group: one(familyGroups, { fields: [invitations.groupId], references: [familyGroups.id] }),
  inviter: one(users, { fields: [invitations.invitedBy], references: [users.id] }),
}));

export const familyGroupMembersRelations = relations(familyGroupMembers, ({ one }) => ({
  group: one(familyGroups, { fields: [familyGroupMembers.groupId], references: [familyGroups.id] }),
  user: one(users, { fields: [familyGroupMembers.userId], references: [users.id] }),
}));

export const subscriptionsRelations = relations(subscriptions, ({ one, many }) => ({
  user: one(users, { fields: [subscriptions.userId], references: [users.id] }),
  familyGroup: one(familyGroups, { fields: [subscriptions.familyGroupId], references: [familyGroups.id] }),
  watchlistItems: many(watchlistItems),
  exposures: many(exposures),
  alerts: many(alerts),
  propertyWatchlistItems: many(propertyWatchlistItems),
  removalRequests: many(removalRequests),
  brokerListings: many(brokerListings),
}));

export const watchlistItemsRelations = relations(watchlistItems, ({ one, many }) => ({
  subscription: one(subscriptions, { fields: [watchlistItems.subscriptionId], references: [subscriptions.id] }),
  exposures: many(exposures),
}));

export const exposuresRelations = relations(exposures, ({ one, many }) => ({
  subscription: one(subscriptions, { fields: [exposures.subscriptionId], references: [subscriptions.id] }),
  watchlistItem: one(watchlistItems, { fields: [exposures.watchlistItemId], references: [watchlistItems.id] }),
  alerts: many(alerts),
}));

export const alertsRelations = relations(alerts, ({ one }) => ({
  subscription: one(subscriptions, { fields: [alerts.subscriptionId], references: [subscriptions.id] }),
  user: one(users, { fields: [alerts.userId], references: [users.id] }),
  exposure: one(exposures, { fields: [alerts.exposureId], references: [exposures.id] }),
}));

export const voiceEnrollmentsRelations = relations(voiceEnrollments, ({ one, many }) => ({
  user: one(users, { fields: [voiceEnrollments.userId], references: [users.id] }),
  analyses: many(voiceAnalyses),
}));

export const voiceAnalysesRelations = relations(voiceAnalyses, ({ one }) => ({
  enrollment: one(voiceEnrollments, { fields: [voiceAnalyses.enrollmentId], references: [voiceEnrollments.id] }),
  user: one(users, { fields: [voiceAnalyses.userId], references: [users.id] }),
}));

export const analysisJobsRelations = relations(analysisJobs, ({ one }) => ({
  user: one(users, { fields: [analysisJobs.userId], references: [users.id] }),
  result: one(analysisResults),
}));

export const analysisResultsRelations = relations(analysisResults, ({ one }) => ({
  analysisJob: one(analysisJobs, { fields: [analysisResults.analysisJobId], references: [analysisJobs.id] }),
}));

export const spamFeedbackRelations = relations(spamFeedback, ({ one }) => ({
  user: one(users, { fields: [spamFeedback.userId], references: [users.id] }),
}));

export const spamRulesRelations = relations(spamRules, ({ one }) => ({
  user: one(users, { fields: [spamRules.userId], references: [users.id] }),
}));

export const normalizedAlertsRelations = relations(normalizedAlerts, ({ one }) => ({
  correlationGroup: one(correlationGroups, { fields: [normalizedAlerts.groupId], references: [correlationGroups.id] }),
  user: one(users, { fields: [normalizedAlerts.userId], references: [users.id] }),
}));

export const correlationGroupsRelations = relations(correlationGroups, ({ one, many }) => ({
  user: one(users, { fields: [correlationGroups.userId], references: [users.id] }),
  alerts: many(normalizedAlerts),
}));

export const securityReportsRelations = relations(securityReports, ({ one }) => ({
  user: one(users, { fields: [securityReports.userId], references: [users.id] }),
}));

export const propertyWatchlistItemsRelations = relations(propertyWatchlistItems, ({ one, many }) => ({
  subscription: one(subscriptions, { fields: [propertyWatchlistItems.subscriptionId], references: [subscriptions.id] }),
  snapshots: many(propertySnapshots),
  changes: many(propertyChanges),
}));

export const propertySnapshotsRelations = relations(propertySnapshots, ({ one, many }) => ({
  propertyWatchlistItem: one(propertyWatchlistItems, { fields: [propertySnapshots.propertyWatchlistItemId], references: [propertyWatchlistItems.id] }),
  changes: many(propertyChanges),
}));

export const propertyChangesRelations = relations(propertyChanges, ({ one }) => ({
  propertyWatchlistItem: one(propertyWatchlistItems, { fields: [propertyChanges.propertyWatchlistItemId], references: [propertyWatchlistItems.id] }),
  snapshot: one(propertySnapshots, { fields: [propertyChanges.snapshotId], references: [propertySnapshots.id] }),
}));

export const infoBrokersRelations = relations(infoBrokers, ({ many }) => ({
  removalRequests: many(removalRequests),
}));

export const removalRequestsRelations = relations(removalRequests, ({ one, many }) => ({
  broker: one(infoBrokers, { fields: [removalRequests.brokerId], references: [infoBrokers.id] }),
  subscription: one(subscriptions, { fields: [removalRequests.subscriptionId], references: [subscriptions.id] }),
  brokerListings: many(brokerListings),
}));

export const brokerListingsRelations = relations(brokerListings, ({ one }) => ({
  removalRequest: one(removalRequests, { fields: [brokerListings.removalRequestId], references: [removalRequests.id] }),
  subscription: one(subscriptions, { fields: [brokerListings.subscriptionId], references: [subscriptions.id] }),
}));
