import { integer, sqliteTable, text, real } from 'drizzle-orm/sqlite-core';
import { user } from './auth.schema';

// Define devil fruit types
export type DevilFruitType = 'Paramecia' | 'Zoan' | 'Logia' | 'Unknown';

// Define the site config table schema
export const config = sqliteTable('config', {
	key: text('key').primaryKey(),
	value: text('value')
});

// Define the arc table schema
export const arc = sqliteTable('arc', {
	id: text('id').primaryKey(),
	name: text('name').notNull(),
	startChapter: integer('startChapter').notNull(),
	endChapter: integer('endChapter'),
	url: text('url')
});

// Define the devil fruit table schema
export const devilFruit = sqliteTable('devilFruit', {
	id: text('id').primaryKey(),
	name: text('name').notNull().unique(),
	type: text('type').$type<DevilFruitType>(),
	url: text('url')
});

// Define the character table schema
export const character = sqliteTable('character', {
	id: text('id').primaryKey(),
	name: text('name').notNull(),
	gender: text('gender'),
	age: integer('age'),
	affiliations: text('affiliations', { mode: 'json' }).$type<string[]>(),
	devilFruitId: text('devilFruitId').references(() => devilFruit.id),
	hakiObservation: integer('hakiObservation', { mode: 'boolean' }).default(false),
	hakiArmament: integer('hakiArmament', { mode: 'boolean' }).default(false),
	hakiConqueror: integer('hakiConqueror', { mode: 'boolean' }).default(false),
	bounty: integer('bounty').default(0),
	height: real('height'),
	origin: text('origin'),
	firstAppearance: integer('firstAppearance').notNull(),
	pictureUrl: text('pictureUrl'),
	epithets: text('epithets', { mode: 'json' }).$type<string[]>(),
	status: text('status'),
	arcId: text('arcId').references(() => arc.id),
	url: text('url'),
	isInDailyMode: integer('isInDailyMode', { mode: 'boolean' }).default(true)
});

// Define the character override table schema
export const characterOverride = sqliteTable('characterOverride', {
	characterId: text('characterId').primaryKey().references(() => character.id),
	name: text('name'),
	gender: text('gender'),
	age: integer('age'),
	affiliations: text('affiliations', { mode: 'json' }).$type<string[]>(),
	devilFruitId: text('devilFruitId').references(() => devilFruit.id),
	hakiObservation: integer('hakiObservation', { mode: 'boolean' }),
	hakiArmament: integer('hakiArmament', { mode: 'boolean' }),
	hakiConqueror: integer('hakiConqueror', { mode: 'boolean' }),
	bounty: integer('bounty'),
	height: real('height'),
	origin: text('origin'),
	firstAppearance: integer('firstAppearance'),
	pictureUrl: text('pictureUrl'),
	epithets: text('epithets', { mode: 'json' }).$type<string[]>(),
	status: text('status'),
	arcId: text('arcId').references(() => arc.id),
	url: text('url'),
	notes: text('notes')
});

// Define the character scrape validation table schema
export const characterScrapeValidation = sqliteTable('characterScrapeValidation', {
	id: text('id').primaryKey(),
	name: text('name').notNull(),
	gender: text('gender'),
	age: integer('age'),
	affiliations: text('affiliations', { mode: 'json' }).$type<string[]>(),
	devilFruitId: text('devilFruitId').references(() => devilFruit.id),
	hakiObservation: integer('hakiObservation', { mode: 'boolean' }).default(false),
	hakiArmament: integer('hakiArmament', { mode: 'boolean' }).default(false),
	hakiConqueror: integer('hakiConqueror', { mode: 'boolean' }).default(false),
	bounty: integer('bounty'),
	height: real('height'),
	origin: text('origin'),
	firstAppearance: integer('firstAppearance').notNull(),
	pictureUrl: text('pictureUrl'),
	epithets: text('epithets', { mode: 'json' }).$type<string[]>(),
	status: text('status'),
	arcId: text('arcId').references(() => arc.id),
	url: text('url')
});

// Define the caracter history table schema
export const characterHistory = sqliteTable('characterHistory', {
	id: text('id')
		.primaryKey()
		.$defaultFn(() => crypto.randomUUID()),
	characterId: text('characterId').references(() => character.id),
	date: integer('date').notNull().unique(),
	won: integer('won').notNull().default(0),
	createdAt: integer('createdAt').notNull().$default(() => Date.now()),
	updatedAt: integer('updatedAt').notNull().$default(() => Date.now()),
});

// Define the user character history table schema
export const userCharacterHistory = sqliteTable('userCharacterHistory', {
	id: text('id')
		.primaryKey()
		.$defaultFn(() => crypto.randomUUID()),
	userId: text('userId').references(() => user.id),
	characterHistoryId: text('characterHistoryId').references(() => characterHistory.id),
	tryCount: integer('tryCount').notNull(),
	createdAt: integer('createdAt').notNull().$default(() => Date.now())
});


export * from './auth.schema';
