import { pgTable, varchar, date, boolean, timestamp, uuid, foreignKey, text, index, time, primaryKey, pgSequence } from "drizzle-orm/pg-core"
import { sql } from "drizzle-orm"


export const versionIdSeq = pgSequence("version_id_seq", {  startWith: "1", increment: "1", minValue: "1", maxValue: "9223372036854775807", cache: "1", cycle: false })
export const accessTokenIdSeq = pgSequence("access_token_id_seq", {  startWith: "1", increment: "1", minValue: "1", maxValue: "9223372036854775807", cache: "1", cycle: false })
export const oauth2ApplicationIdSeq = pgSequence("oauth2_application_id_seq", {  startWith: "1", increment: "1", minValue: "1", maxValue: "9223372036854775807", cache: "1", cycle: false })
export const oauth2AuthorizationCodeIdSeq = pgSequence("oauth2_authorization_code_id_seq", {  startWith: "1", increment: "1", minValue: "1", maxValue: "9223372036854775807", cache: "1", cycle: false })
export const oauth2GrantIdSeq = pgSequence("oauth2_grant_id_seq", {  startWith: "1", increment: "1", minValue: "1", maxValue: "9223372036854775807", cache: "1", cycle: false })
export const loginSourceIdSeq = pgSequence("login_source_id_seq", {  startWith: "1", increment: "1", minValue: "1", maxValue: "9223372036854775807", cache: "1", cycle: false })
export const twoFactorIdSeq = pgSequence("two_factor_id_seq", {  startWith: "1", increment: "1", minValue: "1", maxValue: "9223372036854775807", cache: "1", cycle: false })
export const webauthnCredentialIdSeq = pgSequence("webauthn_credential_id_seq", {  startWith: "1", increment: "1", minValue: "1", maxValue: "9223372036854775807", cache: "1", cycle: false })
export const dbfsMetaIdSeq = pgSequence("dbfs_meta_id_seq", {  startWith: "1", increment: "1", minValue: "1", maxValue: "9223372036854775807", cache: "1", cycle: false })
export const dbfsDataIdSeq = pgSequence("dbfs_data_id_seq", {  startWith: "1", increment: "1", minValue: "1", maxValue: "9223372036854775807", cache: "1", cycle: false })
export const noticeIdSeq = pgSequence("notice_id_seq", {  startWith: "1", increment: "1", minValue: "1", maxValue: "9223372036854775807", cache: "1", cycle: false })
export const systemSettingIdSeq = pgSequence("system_setting_id_seq", {  startWith: "1", increment: "1", minValue: "1", maxValue: "9223372036854775807", cache: "1", cycle: false })
export const badgeIdSeq = pgSequence("badge_id_seq", {  startWith: "1", increment: "1", minValue: "1", maxValue: "9223372036854775807", cache: "1", cycle: false })
export const userBadgeIdSeq = pgSequence("user_badge_id_seq", {  startWith: "1", increment: "1", minValue: "1", maxValue: "9223372036854775807", cache: "1", cycle: false })
export const userBlockingIdSeq = pgSequence("user_blocking_id_seq", {  startWith: "1", increment: "1", minValue: "1", maxValue: "9223372036854775807", cache: "1", cycle: false })
export const emailAddressIdSeq = pgSequence("email_address_id_seq", {  startWith: "1", increment: "1", minValue: "1", maxValue: "9223372036854775807", cache: "1", cycle: false })

export const account = pgTable("account", {
	name: varchar().notNull(),
	email: varchar().notNull(),
	password: varchar().notNull(),
	birthday: date(),
	accountId: varchar("account_id").notNull(),
	nickname: varchar().notNull(),
	status: varchar().default('active').notNull(),
	isDeleted: boolean("is_deleted").default(false).notNull(),
	createdAt: timestamp("created_at", { mode: 'string' }).defaultNow().notNull(),
	id: uuid().default(sql`uuid_generate_v4()`).primaryKey().notNull(),
});

export const comment = pgTable("comment", {
	id: uuid().default(sql`uuid_generate_v4()`).primaryKey().notNull(),
	content: text(),
	createdAt: timestamp("created_at", { mode: 'string' }),
	isDeleted: boolean("is_deleted").default(false),
	writerId: uuid("writer_id"),
	parentId: uuid("parent_id"),
}, (table) => [
	foreignKey({
			columns: [table.writerId],
			foreignColumns: [account.id],
			name: "writer_id"
		}),
	foreignKey({
			columns: [table.parentId],
			foreignColumns: [table.id],
			name: "parent_id"
		}),
]);

export const participant = pgTable("participant", {
	participantId: uuid("participant_id").notNull(),
	scheduleId: uuid("schedule_id").notNull(),
	isDeleted: boolean("is_deleted").default(false),
}, (table) => [
	index("participant_participant_id_idx").using("btree", table.participantId.asc().nullsLast().op("uuid_ops")),
	index("participant_schedule_id_idx").using("btree", table.scheduleId.asc().nullsLast().op("uuid_ops")),
	foreignKey({
			columns: [table.scheduleId],
			foreignColumns: [schedule.id],
			name: "schedule_id"
		}),
	foreignKey({
			columns: [table.participantId],
			foreignColumns: [account.id],
			name: "participant_id"
		}),
]);

export const schedule = pgTable("schedule", {
	id: uuid().default(sql`uuid_generate_v4()`).primaryKey().notNull(),
	name: varchar().notNull(),
	startDate: date("start_date").notNull(),
	endDate: date("end_date").notNull(),
	status: varchar().default('yet').notNull(),
	content: text(),
	isDeleted: boolean("is_deleted").default(false).notNull(),
	type: varchar().notNull(),
	createdAt: timestamp("created_at", { mode: 'string' }).defaultNow().notNull(),
	owner: uuid().notNull(),
	style: varchar().notNull(),
	startTime: time("start_time").notNull(),
	endTime: time("end_time").notNull(),
	dayList: varchar("day_list"),
}, (table) => [
	index("schedule_enddatetime_idx").using("btree", table.endDate.asc().nullsLast().op("date_ops")),
	index("schedule_name_idx").using("btree", table.name.asc().nullsLast().op("text_ops"), table.content.asc().nullsLast().op("text_ops")),
	index("schedule_startdatetime_idx").using("btree", table.startDate.asc().nullsLast().op("date_ops")),
	index("schedule_status_idx").using("btree", table.status.asc().nullsLast().op("text_ops")),
	index("schedule_type_idx").using("btree", table.type.asc().nullsLast().op("text_ops")),
	foreignKey({
			columns: [table.owner],
			foreignColumns: [account.id],
			name: "schedule_user_fk"
		}),
]);

export const favorite = pgTable("favorite", {
	isDeleted: boolean("is_deleted").default(false),
	createdAt: date("created_at"),
	userId: uuid("user_id").notNull(),
	scheduleId: uuid("schedule_id").notNull(),
}, (table) => [
	foreignKey({
			columns: [table.scheduleId],
			foreignColumns: [schedule.id],
			name: "schedule_id"
		}),
	foreignKey({
			columns: [table.userId],
			foreignColumns: [account.id],
			name: "user_id"
		}),
	primaryKey({ columns: [table.userId, table.scheduleId], name: "favorite_pk"}),
]);

export const follow = pgTable("follow", {
	isDeleted: boolean("is_deleted").default(false),
	isAccepted: boolean("is_accepted").default(false),
	isLinked: boolean("is_linked").default(false),
	createdAt: timestamp("created_at", { mode: 'string' }).defaultNow().notNull(),
	following: uuid().notNull(),
	follower: uuid().notNull(),
}, (table) => [
	foreignKey({
			columns: [table.follower],
			foreignColumns: [account.id],
			name: "follower_id"
		}),
	foreignKey({
			columns: [table.following],
			foreignColumns: [account.id],
			name: "following_id"
		}),
	primaryKey({ columns: [table.following, table.follower], name: "follow_pk"}),
]);
