import { relations } from "drizzle-orm/relations";
import { applications, evals, evalResults, prompts, promptsLibrary, promptsLibraryPrompts, monitoringAlerts, testRuns, monitoring, evalsToDatasets, datasets, evalsToPrompts, evalsToTags, tags, testRunsToDatasets, trusysDatasets, testRunsToSecurityConfigs, securityConfig, testRunsToEvals, evalsToTraces } from "./schema";

export const evalsRelations = relations(evals, ({one, many}) => ({
	application: one(applications, {
		fields: [evals.applicationId],
		references: [applications.id]
	}),
	evalResults: many(evalResults),
	evalsToDatasets: many(evalsToDatasets),
	evalsToPrompts: many(evalsToPrompts),
	evalsToTags: many(evalsToTags),
	testRunsToEvals: many(testRunsToEvals),
	evalsToTraces: many(evalsToTraces),
}));

export const applicationsRelations = relations(applications, ({many}) => ({
	evals: many(evals),
	evalResults: many(evalResults),
	monitoringAlerts: many(monitoringAlerts),
	monitorings: many(monitoring),
}));

export const evalResultsRelations = relations(evalResults, ({one}) => ({
	eval: one(evals, {
		fields: [evalResults.evalId],
		references: [evals.id]
	}),
	prompt: one(prompts, {
		fields: [evalResults.promptId],
		references: [prompts.id]
	}),
	application: one(applications, {
		fields: [evalResults.applicationId],
		references: [applications.id]
	}),
}));

export const promptsRelations = relations(prompts, ({many}) => ({
	evalResults: many(evalResults),
	promptsLibraryPrompts: many(promptsLibraryPrompts),
	evalsToPrompts: many(evalsToPrompts),
}));

export const promptsLibraryPromptsRelations = relations(promptsLibraryPrompts, ({one}) => ({
	promptsLibrary: one(promptsLibrary, {
		fields: [promptsLibraryPrompts.libraryId],
		references: [promptsLibrary.id]
	}),
	prompt: one(prompts, {
		fields: [promptsLibraryPrompts.promptId],
		references: [prompts.id]
	}),
}));

export const promptsLibraryRelations = relations(promptsLibrary, ({many}) => ({
	promptsLibraryPrompts: many(promptsLibraryPrompts),
	testRuns: many(testRuns),
}));

export const monitoringAlertsRelations = relations(monitoringAlerts, ({one}) => ({
	application: one(applications, {
		fields: [monitoringAlerts.applicationId],
		references: [applications.id]
	}),
}));

export const testRunsRelations = relations(testRuns, ({one, many}) => ({
	promptsLibrary: one(promptsLibrary, {
		fields: [testRuns.libraryId],
		references: [promptsLibrary.id]
	}),
	testRunsToDatasets: many(testRunsToDatasets),
	testRunsToSecurityConfigs: many(testRunsToSecurityConfigs),
	testRunsToEvals: many(testRunsToEvals),
}));

export const monitoringRelations = relations(monitoring, ({one}) => ({
	application: one(applications, {
		fields: [monitoring.applicationId],
		references: [applications.id]
	}),
}));

export const evalsToDatasetsRelations = relations(evalsToDatasets, ({one}) => ({
	eval: one(evals, {
		fields: [evalsToDatasets.evalId],
		references: [evals.id]
	}),
	dataset: one(datasets, {
		fields: [evalsToDatasets.datasetId],
		references: [datasets.id]
	}),
}));

export const datasetsRelations = relations(datasets, ({many}) => ({
	evalsToDatasets: many(evalsToDatasets),
}));

export const evalsToPromptsRelations = relations(evalsToPrompts, ({one}) => ({
	eval: one(evals, {
		fields: [evalsToPrompts.evalId],
		references: [evals.id]
	}),
	prompt: one(prompts, {
		fields: [evalsToPrompts.promptId],
		references: [prompts.id]
	}),
}));

export const evalsToTagsRelations = relations(evalsToTags, ({one}) => ({
	eval: one(evals, {
		fields: [evalsToTags.evalId],
		references: [evals.id]
	}),
	tag: one(tags, {
		fields: [evalsToTags.tagId],
		references: [tags.id]
	}),
}));

export const tagsRelations = relations(tags, ({many}) => ({
	evalsToTags: many(evalsToTags),
}));

export const testRunsToDatasetsRelations = relations(testRunsToDatasets, ({one}) => ({
	testRun: one(testRuns, {
		fields: [testRunsToDatasets.testRunId],
		references: [testRuns.id]
	}),
	trusysDataset: one(trusysDatasets, {
		fields: [testRunsToDatasets.datasetId],
		references: [trusysDatasets.id]
	}),
}));

export const trusysDatasetsRelations = relations(trusysDatasets, ({many}) => ({
	testRunsToDatasets: many(testRunsToDatasets),
}));

export const testRunsToSecurityConfigsRelations = relations(testRunsToSecurityConfigs, ({one}) => ({
	testRun: one(testRuns, {
		fields: [testRunsToSecurityConfigs.testRunId],
		references: [testRuns.id]
	}),
	securityConfig: one(securityConfig, {
		fields: [testRunsToSecurityConfigs.securityConfigId],
		references: [securityConfig.id]
	}),
}));

export const securityConfigRelations = relations(securityConfig, ({many}) => ({
	testRunsToSecurityConfigs: many(testRunsToSecurityConfigs),
}));

export const testRunsToEvalsRelations = relations(testRunsToEvals, ({one}) => ({
	testRun: one(testRuns, {
		fields: [testRunsToEvals.testRunId],
		references: [testRuns.id]
	}),
	eval: one(evals, {
		fields: [testRunsToEvals.evalId],
		references: [evals.id]
	}),
}));

export const evalsToTracesRelations = relations(evalsToTraces, ({one}) => ({
	eval: one(evals, {
		fields: [evalsToTraces.evalId],
		references: [evals.id]
	}),
}));