import { integer, sqliteTable, text, index, unique } from 'drizzle-orm/sqlite-core';

import { sql } from 'drizzle-orm';

export const user = sqliteTable('user', { id: text('id').primaryKey(), age: integer('age') });

export const session = sqliteTable('session', {
	id: text('id').primaryKey(),
	userId: text('user_id').notNull().references(() => user.id),
	expiresAt: integer('expires_at', { mode: 'timestamp' }).notNull()
});

export type Session = typeof session.$inferSelect;

export type User = typeof user.$inferSelect;

export const dailyVerses = sqliteTable('daily_verses', {
	id: text('id').primaryKey(),
	date: text('date').unique().notNull(),
	bookId: text('book_id').notNull(),
	verseText: text('verse_text').notNull(),
	reference: text('reference').notNull(),
	createdAt: integer('created_at', { mode: 'timestamp' }),
});

export type DailyVerse = typeof dailyVerses.$inferSelect;

export const dailyCompletions = sqliteTable('daily_completions', {
	id: text('id').primaryKey(),
	anonymousId: text('anonymous_id').notNull(),
	date: text('date').notNull(),
	guessCount: integer('guess_count').notNull(),
	completedAt: integer('completed_at', { mode: 'timestamp' }).notNull(),
}, (table) => ({
	uniqueCompletion: unique().on(table.anonymousId, table.date),
	dateIndex: index('date_idx').on(table.date),
	dateGuessIndex: index('date_guess_idx').on(table.date, table.guessCount),
}));

export type DailyCompletion = typeof dailyCompletions.$inferSelect;
