import { sqliteTable, AnySQLiteColumn, foreignKey, text, integer, blob, primaryKey, uniqueIndex } from "drizzle-orm/sqlite-core"
  import { sql } from "drizzle-orm"

export const explorerContent = sqliteTable("explorer_content", {
	path: text().primaryKey().notNull(),
	owner: integer().notNull().references(() => users.id, { onDelete: "cascade", onUpdate: "cascade" } ),
	title: text().notNull(),
	type: text().notNull(),
	content: blob(),
	navigable: integer().default(true),
	private: integer().default(false),
});

export const userSessions = sqliteTable("user_sessions", {
	id: integer().notNull(),
	userId: integer("user_id").notNull().references(() => users.id, { onDelete: "cascade", onUpdate: "cascade" } ),
	timestamp: integer().notNull(),
},
(table) => {
	return {
		pk0: primaryKey({ columns: [table.id, table.userId], name: "user_sessions_id_user_id_pk"})
	}
});

export const usersData = sqliteTable("users_data", {
	id: integer().primaryKey().notNull().references(() => users.id, { onDelete: "cascade", onUpdate: "cascade" } ),
	signin: integer().notNull(),
});

export const users = sqliteTable("users", {
	id: integer().primaryKey({ autoIncrement: true }).notNull(),
	username: text().notNull(),
	email: text().notNull(),
	hash: text().notNull(),
	state: integer().default(0).notNull(),
},
(table) => {
	return {
		hashUnique: uniqueIndex("users_hash_unique").on(table.hash),
		emailUnique: uniqueIndex("users_email_unique").on(table.email),
		usernameUnique: uniqueIndex("users_username_unique").on(table.username),
	}
});

export const userPermissions = sqliteTable("user_permissions", {
	id: integer().notNull().references(() => users.id, { onDelete: "cascade", onUpdate: "cascade" } ),
	permissions: text().notNull(),
},
(table) => {
	return {
		pk0: primaryKey({ columns: [table.id, table.permissions], name: "user_permissions_id_permissions_pk"})
	}
});

export const drizzleMigrations = sqliteTable("__drizzle_migrations", {
});

