import { relations } from "drizzle-orm/relations";
import { restaurants, customers, plans, subscriptions, branches, users, orders, bookings, menuCategories, aiAgents, phoneNumbers, conversations, aiAgentConfigs, knowledgeBase, staff, roles, webhooks, apiKeys, notifications, supportTickets, callLogs, passwordResetTokens, notificationPreferences, messageTemplates, webhookDeliveryLogs, menuItems, menuModifierGroups, menuModifierOptions, llmProviders, llmModels, voiceProviders, voiceModels, restaurantApiKeys, telephoneSettings, sipRegistrations, sipCallSessions, widgetSettings, alertSettings, translationKeys, translations, languages, agentKnowledgeBases } from "./schema";

export const customersRelations = relations(customers, ({one, many}) => ({
	restaurant: one(restaurants, {
		fields: [customers.restaurantId],
		references: [restaurants.id]
	}),
	orders: many(orders),
	bookings: many(bookings),
	conversations: many(conversations),
}));

export const restaurantsRelations = relations(restaurants, ({many}) => ({
	customers: many(customers),
	subscriptions: many(subscriptions),
	branches: many(branches),
	users: many(users),
	orders: many(orders),
	bookings: many(bookings),
	menuCategories: many(menuCategories),
	phoneNumbers: many(phoneNumbers),
	conversations: many(conversations),
	aiAgentConfigs: many(aiAgentConfigs),
	knowledgeBases: many(knowledgeBase),
	staff: many(staff),
	webhooks: many(webhooks),
	apiKeys: many(apiKeys),
	notifications: many(notifications),
	supportTickets: many(supportTickets),
	callLogs: many(callLogs),
	notificationPreferences: many(notificationPreferences),
	messageTemplates: many(messageTemplates),
	webhookDeliveryLogs: many(webhookDeliveryLogs),
	menuItems: many(menuItems),
	menuModifierGroups: many(menuModifierGroups),
	restaurantApiKeys: many(restaurantApiKeys),
	telephoneSettings: many(telephoneSettings),
	sipRegistrations: many(sipRegistrations),
	sipCallSessions: many(sipCallSessions),
	widgetSettings: many(widgetSettings),
	aiAgents: many(aiAgents),
	roles: many(roles),
	alertSettings: many(alertSettings),
}));

export const subscriptionsRelations = relations(subscriptions, ({one}) => ({
	plan: one(plans, {
		fields: [subscriptions.planId],
		references: [plans.id]
	}),
	restaurant: one(restaurants, {
		fields: [subscriptions.restaurantId],
		references: [restaurants.id]
	}),
}));

export const plansRelations = relations(plans, ({many}) => ({
	subscriptions: many(subscriptions),
}));

export const branchesRelations = relations(branches, ({one, many}) => ({
	restaurant: one(restaurants, {
		fields: [branches.restaurantId],
		references: [restaurants.id]
	}),
	users: many(users),
	orders: many(orders),
	bookings: many(bookings),
	menuCategories: many(menuCategories),
	phoneNumbers: many(phoneNumbers),
	conversations: many(conversations),
	aiAgentConfigs: many(aiAgentConfigs),
	knowledgeBases: many(knowledgeBase),
	staff: many(staff),
	callLogs: many(callLogs),
	menuItems: many(menuItems),
	menuModifierGroups: many(menuModifierGroups),
	aiAgents: many(aiAgents),
}));

export const usersRelations = relations(users, ({one, many}) => ({
	branch: one(branches, {
		fields: [users.branchId],
		references: [branches.id]
	}),
	restaurant: one(restaurants, {
		fields: [users.restaurantId],
		references: [restaurants.id]
	}),
	staff: many(staff),
	notifications: many(notifications),
	supportTickets: many(supportTickets),
	passwordResetTokens: many(passwordResetTokens),
	notificationPreferences: many(notificationPreferences),
}));

export const ordersRelations = relations(orders, ({one}) => ({
	branch: one(branches, {
		fields: [orders.branchId],
		references: [branches.id]
	}),
	customer: one(customers, {
		fields: [orders.customerId],
		references: [customers.id]
	}),
	restaurant: one(restaurants, {
		fields: [orders.restaurantId],
		references: [restaurants.id]
	}),
}));

export const bookingsRelations = relations(bookings, ({one}) => ({
	branch: one(branches, {
		fields: [bookings.branchId],
		references: [branches.id]
	}),
	customer: one(customers, {
		fields: [bookings.customerId],
		references: [customers.id]
	}),
	restaurant: one(restaurants, {
		fields: [bookings.restaurantId],
		references: [restaurants.id]
	}),
}));

export const menuCategoriesRelations = relations(menuCategories, ({one, many}) => ({
	branch: one(branches, {
		fields: [menuCategories.branchId],
		references: [branches.id]
	}),
	restaurant: one(restaurants, {
		fields: [menuCategories.restaurantId],
		references: [restaurants.id]
	}),
	menuItems: many(menuItems),
}));

export const phoneNumbersRelations = relations(phoneNumbers, ({one, many}) => ({
	aiAgent: one(aiAgents, {
		fields: [phoneNumbers.assignedAgentId],
		references: [aiAgents.id]
	}),
	branch: one(branches, {
		fields: [phoneNumbers.branchId],
		references: [branches.id]
	}),
	restaurant: one(restaurants, {
		fields: [phoneNumbers.restaurantId],
		references: [restaurants.id]
	}),
	sipCallSessions: many(sipCallSessions),
}));

export const aiAgentsRelations = relations(aiAgents, ({one, many}) => ({
	phoneNumbers: many(phoneNumbers),
	sipCallSessions: many(sipCallSessions),
	widgetSettings: many(widgetSettings),
	branch: one(branches, {
		fields: [aiAgents.branchId],
		references: [branches.id]
	}),
	llmModel: one(llmModels, {
		fields: [aiAgents.llmModelId],
		references: [llmModels.id]
	}),
	restaurant: one(restaurants, {
		fields: [aiAgents.restaurantId],
		references: [restaurants.id]
	}),
	voiceModel: one(voiceModels, {
		fields: [aiAgents.voiceModelId],
		references: [voiceModels.id]
	}),
	agentKnowledgeBases: many(agentKnowledgeBases),
}));

export const conversationsRelations = relations(conversations, ({one, many}) => ({
	branch: one(branches, {
		fields: [conversations.branchId],
		references: [branches.id]
	}),
	customer: one(customers, {
		fields: [conversations.customerId],
		references: [customers.id]
	}),
	restaurant: one(restaurants, {
		fields: [conversations.restaurantId],
		references: [restaurants.id]
	}),
	callLogs: many(callLogs),
}));

export const aiAgentConfigsRelations = relations(aiAgentConfigs, ({one}) => ({
	branch: one(branches, {
		fields: [aiAgentConfigs.branchId],
		references: [branches.id]
	}),
	restaurant: one(restaurants, {
		fields: [aiAgentConfigs.restaurantId],
		references: [restaurants.id]
	}),
}));

export const knowledgeBaseRelations = relations(knowledgeBase, ({one, many}) => ({
	branch: one(branches, {
		fields: [knowledgeBase.branchId],
		references: [branches.id]
	}),
	restaurant: one(restaurants, {
		fields: [knowledgeBase.restaurantId],
		references: [restaurants.id]
	}),
	agentKnowledgeBases: many(agentKnowledgeBases),
}));

export const staffRelations = relations(staff, ({one}) => ({
	branch: one(branches, {
		fields: [staff.branchId],
		references: [branches.id]
	}),
	restaurant: one(restaurants, {
		fields: [staff.restaurantId],
		references: [restaurants.id]
	}),
	role: one(roles, {
		fields: [staff.roleId],
		references: [roles.id]
	}),
	user: one(users, {
		fields: [staff.userId],
		references: [users.id]
	}),
}));

export const rolesRelations = relations(roles, ({one, many}) => ({
	staff: many(staff),
	restaurant: one(restaurants, {
		fields: [roles.restaurantId],
		references: [restaurants.id]
	}),
}));

export const webhooksRelations = relations(webhooks, ({one, many}) => ({
	restaurant: one(restaurants, {
		fields: [webhooks.restaurantId],
		references: [restaurants.id]
	}),
	webhookDeliveryLogs: many(webhookDeliveryLogs),
}));

export const apiKeysRelations = relations(apiKeys, ({one}) => ({
	restaurant: one(restaurants, {
		fields: [apiKeys.restaurantId],
		references: [restaurants.id]
	}),
}));

export const notificationsRelations = relations(notifications, ({one}) => ({
	restaurant: one(restaurants, {
		fields: [notifications.restaurantId],
		references: [restaurants.id]
	}),
	user: one(users, {
		fields: [notifications.userId],
		references: [users.id]
	}),
}));

export const supportTicketsRelations = relations(supportTickets, ({one}) => ({
	restaurant: one(restaurants, {
		fields: [supportTickets.restaurantId],
		references: [restaurants.id]
	}),
	user: one(users, {
		fields: [supportTickets.userId],
		references: [users.id]
	}),
}));

export const callLogsRelations = relations(callLogs, ({one}) => ({
	branch: one(branches, {
		fields: [callLogs.branchId],
		references: [branches.id]
	}),
	conversation: one(conversations, {
		fields: [callLogs.conversationId],
		references: [conversations.id]
	}),
	restaurant: one(restaurants, {
		fields: [callLogs.restaurantId],
		references: [restaurants.id]
	}),
}));

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

export const notificationPreferencesRelations = relations(notificationPreferences, ({one}) => ({
	restaurant: one(restaurants, {
		fields: [notificationPreferences.restaurantId],
		references: [restaurants.id]
	}),
	user: one(users, {
		fields: [notificationPreferences.userId],
		references: [users.id]
	}),
}));

export const messageTemplatesRelations = relations(messageTemplates, ({one}) => ({
	restaurant: one(restaurants, {
		fields: [messageTemplates.restaurantId],
		references: [restaurants.id]
	}),
}));

export const webhookDeliveryLogsRelations = relations(webhookDeliveryLogs, ({one}) => ({
	restaurant: one(restaurants, {
		fields: [webhookDeliveryLogs.restaurantId],
		references: [restaurants.id]
	}),
	webhook: one(webhooks, {
		fields: [webhookDeliveryLogs.webhookId],
		references: [webhooks.id]
	}),
}));

export const menuItemsRelations = relations(menuItems, ({one, many}) => ({
	branch: one(branches, {
		fields: [menuItems.branchId],
		references: [branches.id]
	}),
	menuCategory: one(menuCategories, {
		fields: [menuItems.categoryId],
		references: [menuCategories.id]
	}),
	restaurant: one(restaurants, {
		fields: [menuItems.restaurantId],
		references: [restaurants.id]
	}),
	menuModifierGroups: many(menuModifierGroups),
}));

export const menuModifierGroupsRelations = relations(menuModifierGroups, ({one, many}) => ({
	branch: one(branches, {
		fields: [menuModifierGroups.branchId],
		references: [branches.id]
	}),
	menuItem: one(menuItems, {
		fields: [menuModifierGroups.menuItemId],
		references: [menuItems.id]
	}),
	restaurant: one(restaurants, {
		fields: [menuModifierGroups.restaurantId],
		references: [restaurants.id]
	}),
	menuModifierOptions: many(menuModifierOptions),
}));

export const menuModifierOptionsRelations = relations(menuModifierOptions, ({one}) => ({
	menuModifierGroup: one(menuModifierGroups, {
		fields: [menuModifierOptions.modifierGroupId],
		references: [menuModifierGroups.id]
	}),
}));

export const llmModelsRelations = relations(llmModels, ({one, many}) => ({
	llmProvider: one(llmProviders, {
		fields: [llmModels.providerId],
		references: [llmProviders.id]
	}),
	aiAgents: many(aiAgents),
}));

export const llmProvidersRelations = relations(llmProviders, ({many}) => ({
	llmModels: many(llmModels),
}));

export const voiceModelsRelations = relations(voiceModels, ({one, many}) => ({
	voiceProvider: one(voiceProviders, {
		fields: [voiceModels.voiceProviderId],
		references: [voiceProviders.id]
	}),
	aiAgents: many(aiAgents),
}));

export const voiceProvidersRelations = relations(voiceProviders, ({many}) => ({
	voiceModels: many(voiceModels),
}));

export const restaurantApiKeysRelations = relations(restaurantApiKeys, ({one}) => ({
	restaurant: one(restaurants, {
		fields: [restaurantApiKeys.restaurantId],
		references: [restaurants.id]
	}),
}));

export const telephoneSettingsRelations = relations(telephoneSettings, ({one}) => ({
	restaurant: one(restaurants, {
		fields: [telephoneSettings.restaurantId],
		references: [restaurants.id]
	}),
}));

export const sipRegistrationsRelations = relations(sipRegistrations, ({one}) => ({
	restaurant: one(restaurants, {
		fields: [sipRegistrations.restaurantId],
		references: [restaurants.id]
	}),
}));

export const sipCallSessionsRelations = relations(sipCallSessions, ({one}) => ({
	aiAgent: one(aiAgents, {
		fields: [sipCallSessions.agentId],
		references: [aiAgents.id]
	}),
	phoneNumber: one(phoneNumbers, {
		fields: [sipCallSessions.phoneNumberId],
		references: [phoneNumbers.id]
	}),
	restaurant: one(restaurants, {
		fields: [sipCallSessions.restaurantId],
		references: [restaurants.id]
	}),
}));

export const widgetSettingsRelations = relations(widgetSettings, ({one}) => ({
	aiAgent: one(aiAgents, {
		fields: [widgetSettings.agentId],
		references: [aiAgents.id]
	}),
	restaurant: one(restaurants, {
		fields: [widgetSettings.restaurantId],
		references: [restaurants.id]
	}),
}));

export const alertSettingsRelations = relations(alertSettings, ({one}) => ({
	restaurant: one(restaurants, {
		fields: [alertSettings.restaurantId],
		references: [restaurants.id]
	}),
}));

export const translationsRelations = relations(translations, ({one}) => ({
	translationKey: one(translationKeys, {
		fields: [translations.keyId],
		references: [translationKeys.id]
	}),
	language: one(languages, {
		fields: [translations.langCode],
		references: [languages.code]
	}),
}));

export const translationKeysRelations = relations(translationKeys, ({many}) => ({
	translations: many(translations),
}));

export const languagesRelations = relations(languages, ({many}) => ({
	translations: many(translations),
}));

export const agentKnowledgeBasesRelations = relations(agentKnowledgeBases, ({one}) => ({
	aiAgent: one(aiAgents, {
		fields: [agentKnowledgeBases.agentId],
		references: [aiAgents.id]
	}),
	knowledgeBase: one(knowledgeBase, {
		fields: [agentKnowledgeBases.kbId],
		references: [knowledgeBase.id]
	}),
}));