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



export const comment = pgTable("comment", {
	id: uuid().primaryKey().notNull(),
	content: text(),
	createdAt: date("created_at"),
	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 schedule = pgTable("schedule", {
	id: uuid().primaryKey().notNull(),
	name: varchar(),
	startAt: date("start_at"),
	endAt: date("end_at"),
	status: varchar(),
	content: text(),
	isDeleted: boolean("is_deleted").default(false),
	type: varchar(),
	createdAt: date("created_at"),
	owner: uuid(),
}, (table) => [
	index("schedule_enddatetime_idx").using("btree", table.endAt.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.startAt.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 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: date("created_at").defaultNow().notNull(),
	id: uuid().default(sql`uuid_generate_v4()`).primaryKey().notNull(),
});

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 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: date("created_at"),
	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"}),
]);
