import { relations } from "drizzle-orm/relations";
import { clients, notes, payments, phases, quoteItems, serviceCatalog, documents, tasks, deliverables } from "./schema";

export const notesRelations = relations(notes, ({one}) => ({
	client: one(clients, {
		fields: [notes.clientId],
		references: [clients.id]
	}),
}));

export const clientsRelations = relations(clients, ({many}) => ({
	notes: many(notes),
	payments: many(payments),
	phases: many(phases),
	quoteItems: many(quoteItems),
	documents: many(documents),
}));

export const paymentsRelations = relations(payments, ({one}) => ({
	client: one(clients, {
		fields: [payments.clientId],
		references: [clients.id]
	}),
}));

export const phasesRelations = relations(phases, ({one, many}) => ({
	client: one(clients, {
		fields: [phases.clientId],
		references: [clients.id]
	}),
	tasks: many(tasks),
}));

export const quoteItemsRelations = relations(quoteItems, ({one}) => ({
	client: one(clients, {
		fields: [quoteItems.clientId],
		references: [clients.id]
	}),
	serviceCatalog: one(serviceCatalog, {
		fields: [quoteItems.serviceId],
		references: [serviceCatalog.id]
	}),
}));

export const serviceCatalogRelations = relations(serviceCatalog, ({many}) => ({
	quoteItems: many(quoteItems),
}));

export const documentsRelations = relations(documents, ({one}) => ({
	client: one(clients, {
		fields: [documents.clientId],
		references: [clients.id]
	}),
}));

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

export const deliverablesRelations = relations(deliverables, ({one}) => ({
	task: one(tasks, {
		fields: [deliverables.taskId],
		references: [tasks.id]
	}),
}));