import {
	integer,
	sqliteTable,
	text,
	uniqueIndex,
} from "drizzle-orm/sqlite-core";
import type { LibraryType } from "./enums";

export const libraryFolders = sqliteTable(
	"library_folders",
	{
		id: integer("id")
			.primaryKey()
			.$defaultFn(() => {
				return Math.floor(100000 + Math.random() * 99999);
			}),
		name: text("name").notNull(), // "Movies", "TV Shows"
		type: text("type").$type<LibraryType>().notNull(), // "MOVIE" | "SERIES"
		path: text("path").notNull(), // absolute/mount path
		primaryLanguage: text("primary_language").notNull().default("en"),
		fallbackLanguage: text("fallback_language").notNull().default("en"),
		createdAt: integer("created_at", { mode: "timestamp" })
			.$defaultFn(() => new Date())
			.notNull(),
		updatedAt: integer("updated_at", { mode: "timestamp" })
			.$defaultFn(() => new Date())
			.$onUpdate(() => new Date())
			.notNull(),
	},
	(t) => [
		uniqueIndex("ux_library_folder_path").on(t.path),
		uniqueIndex("ux_library_folder_name").on(t.name),
	],
);
