import { relations } from "drizzle-orm/relations";
import { tasks, agents, messages, initiatives, phases, plans, taskDependencies, phaseDependencies } from "./schema";

export const agentsRelations = relations(agents, ({one, many}) => ({
	task: one(tasks, {
		fields: [agents.taskId],
		references: [tasks.id]
	}),
	messages_recipientId: many(messages, {
		relationName: "messages_recipientId_agents_id"
	}),
	messages_senderId: many(messages, {
		relationName: "messages_senderId_agents_id"
	}),
}));

export const tasksRelations = relations(tasks, ({one, many}) => ({
	agents: many(agents),
	taskDependencies_dependsOnTaskId: many(taskDependencies, {
		relationName: "taskDependencies_dependsOnTaskId_tasks_id"
	}),
	taskDependencies_taskId: many(taskDependencies, {
		relationName: "taskDependencies_taskId_tasks_id"
	}),
	plan: one(plans, {
		fields: [tasks.planId],
		references: [plans.id]
	}),
}));

export const messagesRelations = relations(messages, ({one, many}) => ({
	message: one(messages, {
		fields: [messages.parentMessageId],
		references: [messages.id],
		relationName: "messages_parentMessageId_messages_id"
	}),
	messages: many(messages, {
		relationName: "messages_parentMessageId_messages_id"
	}),
	agent_recipientId: one(agents, {
		fields: [messages.recipientId],
		references: [agents.id],
		relationName: "messages_recipientId_agents_id"
	}),
	agent_senderId: one(agents, {
		fields: [messages.senderId],
		references: [agents.id],
		relationName: "messages_senderId_agents_id"
	}),
}));

export const phasesRelations = relations(phases, ({one, many}) => ({
	initiative: one(initiatives, {
		fields: [phases.initiativeId],
		references: [initiatives.id]
	}),
	plans: many(plans),
	phaseDependencies_dependsOnPhaseId: many(phaseDependencies, {
		relationName: "phaseDependencies_dependsOnPhaseId_phases_id"
	}),
	phaseDependencies_phaseId: many(phaseDependencies, {
		relationName: "phaseDependencies_phaseId_phases_id"
	}),
}));

export const initiativesRelations = relations(initiatives, ({many}) => ({
	phases: many(phases),
}));

export const plansRelations = relations(plans, ({one, many}) => ({
	phase: one(phases, {
		fields: [plans.phaseId],
		references: [phases.id]
	}),
	tasks: many(tasks),
}));

export const taskDependenciesRelations = relations(taskDependencies, ({one}) => ({
	task_dependsOnTaskId: one(tasks, {
		fields: [taskDependencies.dependsOnTaskId],
		references: [tasks.id],
		relationName: "taskDependencies_dependsOnTaskId_tasks_id"
	}),
	task_taskId: one(tasks, {
		fields: [taskDependencies.taskId],
		references: [tasks.id],
		relationName: "taskDependencies_taskId_tasks_id"
	}),
}));

export const phaseDependenciesRelations = relations(phaseDependencies, ({one}) => ({
	phase_dependsOnPhaseId: one(phases, {
		fields: [phaseDependencies.dependsOnPhaseId],
		references: [phases.id],
		relationName: "phaseDependencies_dependsOnPhaseId_phases_id"
	}),
	phase_phaseId: one(phases, {
		fields: [phaseDependencies.phaseId],
		references: [phases.id],
		relationName: "phaseDependencies_phaseId_phases_id"
	}),
}));