import { relations } from "drizzle-orm";
import { anime } from "./tables/anime";
import { animeGenres } from "./tables/anime-genres";
import { animeNames } from "./tables/anime-names";
import { animeSongLinks } from "./tables/anime-song-links";
import { animeTags } from "./tables/anime-tags";
import { artistAltNames } from "./tables/artist-alt-names";
import { artistGroups } from "./tables/artist-groups";
import { artists } from "./tables/artists";
import { genres } from "./tables/genres";
import { groupAltNames } from "./tables/group-alt-names";
import { groupArtistMembers } from "./tables/group-artist-members";
import { groupGroupMembers } from "./tables/group-group-members";
import { groups } from "./tables/groups";
import { songs } from "./tables/songs";
import { tags } from "./tables/tags";

// ----------------------
// Relations (optional but helpful for Drizzle queries)
// ----------------------

export const animeRelations = relations(anime, ({ many }) => ({
	names: many(animeNames),
	genres: many(animeGenres),
	tags: many(animeTags),
	songLinks: many(animeSongLinks),
}));

export const songRelations = relations(songs, ({ many }) => ({
	animeLinks: many(animeSongLinks),
}));

export const artistRelations = relations(artists, ({ many }) => ({
	inGroups: many(artistGroups),
	altNames: many(artistAltNames),
	groupMemberships: many(groupArtistMembers),
}));

export const groupRelations = relations(groups, ({ many }) => ({
	artists: many(artistGroups),
	artistMembers: many(groupArtistMembers),
	groupMembers: many(groupGroupMembers),
	altNames: many(groupAltNames),
}));

export const animeNamesRelations = relations(animeNames, ({ one }) => ({
	anime: one(anime, {
		fields: [animeNames.annId],
		references: [anime.annId],
	}),
}));

export const animeGenresRelations = relations(animeGenres, ({ one }) => ({
	anime: one(anime, {
		fields: [animeGenres.annId],
		references: [anime.annId],
	}),
	genre: one(genres, {
		fields: [animeGenres.genreName],
		references: [genres.name],
	}),
}));

export const animeTagsRelations = relations(animeTags, ({ one }) => ({
	anime: one(anime, {
		fields: [animeTags.annId],
		references: [anime.annId],
	}),
	tag: one(tags, {
		fields: [animeTags.tagName],
		references: [tags.name],
	}),
}));

export const animeSongLinksRelations = relations(animeSongLinks, ({ one }) => ({
	anime: one(anime, {
		fields: [animeSongLinks.annId],
		references: [anime.annId],
	}),
	song: one(songs, {
		fields: [animeSongLinks.annSongId],
		references: [songs.annSongId],
	}),
}));
