import { relations } from "drizzle-orm/relations";
import { account, comment, schedule, participant, favorite, follow } from "./schema";

export const commentRelations = relations(comment, ({one, many}) => ({
	account: one(account, {
		fields: [comment.writerId],
		references: [account.id]
	}),
	comment: one(comment, {
		fields: [comment.parentId],
		references: [comment.id],
		relationName: "comment_parentId_comment_id"
	}),
	comments: many(comment, {
		relationName: "comment_parentId_comment_id"
	}),
}));

export const accountRelations = relations(account, ({many}) => ({
	comments: many(comment),
	schedules: many(schedule),
	participants: many(participant),
	favorites: many(favorite),
	follows_follower: many(follow, {
		relationName: "follow_follower_account_id"
	}),
	follows_following: many(follow, {
		relationName: "follow_following_account_id"
	}),
}));

export const scheduleRelations = relations(schedule, ({one, many}) => ({
	account: one(account, {
		fields: [schedule.owner],
		references: [account.id]
	}),
	participants: many(participant),
	favorites: many(favorite),
}));

export const participantRelations = relations(participant, ({one}) => ({
	schedule: one(schedule, {
		fields: [participant.scheduleId],
		references: [schedule.id]
	}),
	account: one(account, {
		fields: [participant.participantId],
		references: [account.id]
	}),
}));

export const favoriteRelations = relations(favorite, ({one}) => ({
	schedule: one(schedule, {
		fields: [favorite.scheduleId],
		references: [schedule.id]
	}),
	account: one(account, {
		fields: [favorite.userId],
		references: [account.id]
	}),
}));

export const followRelations = relations(follow, ({one}) => ({
	account_follower: one(account, {
		fields: [follow.follower],
		references: [account.id],
		relationName: "follow_follower_account_id"
	}),
	account_following: one(account, {
		fields: [follow.following],
		references: [account.id],
		relationName: "follow_following_account_id"
	}),
}));