import {
	index,
	integer,
	primaryKey,
	sqliteTable,
} from "drizzle-orm/sqlite-core";
import { anime } from "./anime";
import { songs } from "./songs";

/**
 * Join table: Anime <-> Songs links
 *
 * Source: AmqAnimeSchema.songLinks (AmqSongLink[])
 */
export const animeSongLinks = sqliteTable(
	"anime_song_links",
	{
		annId: integer("ann_id")
			.notNull()
			.references(() => anime.annId, { onDelete: "cascade" }),

		annSongId: integer("ann_song_id")
			.notNull()
			.references(() => songs.annSongId, { onDelete: "cascade" }),

		/** 1(OP) | 2(ED) | 3(INS) per SongLinkType */
		type: integer("type").notNull(),

		/** link number within type (1-based in source) */
		number: integer("number").notNull(),

		/** boolean ints 0/1 in source */
		uploaded: integer("uploaded").notNull(),
		rebroadcast: integer("rebroadcast").notNull(),
		dub: integer("dub").notNull(),
	},
	(t) => ({
		pk: primaryKey({
			name: "anime_songs_pk",
			columns: [t.annId, t.annSongId],
		}),
		annIdIndex: index("anime_songs_ann_id_idx").on(t.annId),
		annSongIdIndex: index("anime_songs_song_id_idx").on(t.annSongId),
	}),
);
