import { relations } from "drizzle-orm/relations";
import { identities, notifications, publications, documents, comments_on_documents, bsky_profiles, entity_sets, entities, facts, email_auth_tokens, recommends_on_documents, poll_votes_on_entity, permission_tokens, user_subscriptions, phone_rsvps_to_entity, site_standard_publications, custom_domains, custom_domain_routes, site_standard_documents, email_subscriptions_to_entity, atp_poll_records, atp_poll_votes, publication_newsletter_settings, publication_email_subscribers, publication_email_subscriber_events, bsky_follows, site_standard_documents_in_publications, documents_in_publications, leaflet_contributors, document_mentions_in_bsky, bsky_posts, permission_token_on_homepage, publication_domains, publication_contributors, publication_subscriptions, site_standard_subscriptions, user_entitlements, permission_token_rights, publication_post_sends, leaflets_to_documents, leaflets_in_publications, publication_pages } from "./schema";

export const notificationsRelations = relations(notifications, ({one}) => ({
	identity: one(identities, {
		fields: [notifications.recipient],
		references: [identities.atp_did]
	}),
}));

export const identitiesRelations = relations(identities, ({one, many}) => ({
	notifications: many(notifications),
	publications: many(publications),
	email_auth_tokens: many(email_auth_tokens),
	recommends_on_documents: many(recommends_on_documents),
	bsky_profiles: many(bsky_profiles),
	user_subscriptions: many(user_subscriptions),
	permission_token: one(permission_tokens, {
		fields: [identities.home_page],
		references: [permission_tokens.id]
	}),
	site_standard_publications: many(site_standard_publications),
	site_standard_documents: many(site_standard_documents),
	custom_domains_identity: many(custom_domains, {
		relationName: "custom_domains_identity_identities_email"
	}),
	custom_domains_identity_id: many(custom_domains, {
		relationName: "custom_domains_identity_id_identities_id"
	}),
	publication_email_subscribers: many(publication_email_subscribers),
	bsky_follows_follows: many(bsky_follows, {
		relationName: "bsky_follows_follows_identities_atp_did"
	}),
	bsky_follows_identity: many(bsky_follows, {
		relationName: "bsky_follows_identity_identities_atp_did"
	}),
	leaflet_contributors: many(leaflet_contributors),
	permission_token_on_homepages: many(permission_token_on_homepage),
	publication_domains: many(publication_domains),
	publication_contributors: many(publication_contributors),
	publication_subscriptions: many(publication_subscriptions),
	site_standard_subscriptions: many(site_standard_subscriptions),
	user_entitlements: many(user_entitlements),
}));

export const publicationsRelations = relations(publications, ({one, many}) => ({
	identity: one(identities, {
		fields: [publications.identity_did],
		references: [identities.atp_did]
	}),
	publication_newsletter_settings: many(publication_newsletter_settings),
	publication_email_subscribers: many(publication_email_subscribers),
	publication_email_subscriber_events: many(publication_email_subscriber_events),
	documents_in_publications: many(documents_in_publications),
	publication_domains: many(publication_domains),
	publication_contributors: many(publication_contributors),
	publication_subscriptions: many(publication_subscriptions),
	publication_post_sends: many(publication_post_sends),
	leaflets_in_publications: many(leaflets_in_publications),
	publication_pages: many(publication_pages),
}));

export const comments_on_documentsRelations = relations(comments_on_documents, ({one}) => ({
	document: one(documents, {
		fields: [comments_on_documents.document],
		references: [documents.uri]
	}),
}));

export const documentsRelations = relations(documents, ({many}) => ({
	comments_on_documents: many(comments_on_documents),
	recommends_on_documents: many(recommends_on_documents),
	documents_in_publications: many(documents_in_publications),
	document_mentions_in_bskies: many(document_mentions_in_bsky),
	publication_post_sends: many(publication_post_sends),
	leaflets_to_documents: many(leaflets_to_documents),
	leaflets_in_publications: many(leaflets_in_publications),
	publication_pages: many(publication_pages),
}));

export const entitiesRelations = relations(entities, ({one, many}) => ({
	entity_set: one(entity_sets, {
		fields: [entities.set],
		references: [entity_sets.id]
	}),
	facts: many(facts),
	poll_votes_on_entities_option_entity: many(poll_votes_on_entity, {
		relationName: "poll_votes_on_entity_option_entity_entities_id"
	}),
	poll_votes_on_entities_poll_entity: many(poll_votes_on_entity, {
		relationName: "poll_votes_on_entity_poll_entity_entities_id"
	}),
	permission_tokens: many(permission_tokens),
	phone_rsvps_to_entities: many(phone_rsvps_to_entity),
	email_subscriptions_to_entities: many(email_subscriptions_to_entity),
}));

export const entity_setsRelations = relations(entity_sets, ({many}) => ({
	entities: many(entities),
	permission_token_rights: many(permission_token_rights),
}));

export const factsRelations = relations(facts, ({one}) => ({
	entity: one(entities, {
		fields: [facts.entity],
		references: [entities.id]
	}),
}));

export const email_auth_tokensRelations = relations(email_auth_tokens, ({one}) => ({
	identity: one(identities, {
		fields: [email_auth_tokens.identity],
		references: [identities.id]
	}),
}));

export const recommends_on_documentsRelations = relations(recommends_on_documents, ({one}) => ({
	document: one(documents, {
		fields: [recommends_on_documents.document],
		references: [documents.uri]
	}),
	identity: one(identities, {
		fields: [recommends_on_documents.recommender_did],
		references: [identities.atp_did]
	}),
}));

export const bsky_profilesRelations = relations(bsky_profiles, ({one}) => ({
	identity: one(identities, {
		fields: [bsky_profiles.did],
		references: [identities.atp_did]
	}),
}));

export const poll_votes_on_entityRelations = relations(poll_votes_on_entity, ({one}) => ({
	entity_option_entity: one(entities, {
		fields: [poll_votes_on_entity.option_entity],
		references: [entities.id],
		relationName: "poll_votes_on_entity_option_entity_entities_id"
	}),
	entity_poll_entity: one(entities, {
		fields: [poll_votes_on_entity.poll_entity],
		references: [entities.id],
		relationName: "poll_votes_on_entity_poll_entity_entities_id"
	}),
}));

export const permission_tokensRelations = relations(permission_tokens, ({one, many}) => ({
	entity: one(entities, {
		fields: [permission_tokens.root_entity],
		references: [entities.id]
	}),
	identities: many(identities),
	custom_domain_routes_edit_permission_token: many(custom_domain_routes, {
		relationName: "custom_domain_routes_edit_permission_token_permission_tokens_id"
	}),
	custom_domain_routes_view_permission_token: many(custom_domain_routes, {
		relationName: "custom_domain_routes_view_permission_token_permission_tokens_id"
	}),
	email_subscriptions_to_entities: many(email_subscriptions_to_entity),
	leaflet_contributors: many(leaflet_contributors),
	permission_token_on_homepages: many(permission_token_on_homepage),
	permission_token_rights: many(permission_token_rights),
	leaflets_to_documents: many(leaflets_to_documents),
	leaflets_in_publications: many(leaflets_in_publications),
	publication_pages: many(publication_pages),
}));

export const user_subscriptionsRelations = relations(user_subscriptions, ({one}) => ({
	identity: one(identities, {
		fields: [user_subscriptions.identity_id],
		references: [identities.id]
	}),
}));

export const phone_rsvps_to_entityRelations = relations(phone_rsvps_to_entity, ({one}) => ({
	entity: one(entities, {
		fields: [phone_rsvps_to_entity.entity],
		references: [entities.id]
	}),
}));

export const site_standard_publicationsRelations = relations(site_standard_publications, ({one, many}) => ({
	identity: one(identities, {
		fields: [site_standard_publications.identity_did],
		references: [identities.atp_did]
	}),
	site_standard_documents_in_publications: many(site_standard_documents_in_publications),
	site_standard_subscriptions: many(site_standard_subscriptions),
}));

export const custom_domain_routesRelations = relations(custom_domain_routes, ({one}) => ({
	custom_domain: one(custom_domains, {
		fields: [custom_domain_routes.domain],
		references: [custom_domains.domain]
	}),
	permission_token_edit_permission_token: one(permission_tokens, {
		fields: [custom_domain_routes.edit_permission_token],
		references: [permission_tokens.id],
		relationName: "custom_domain_routes_edit_permission_token_permission_tokens_id"
	}),
	permission_token_view_permission_token: one(permission_tokens, {
		fields: [custom_domain_routes.view_permission_token],
		references: [permission_tokens.id],
		relationName: "custom_domain_routes_view_permission_token_permission_tokens_id"
	}),
}));

export const custom_domainsRelations = relations(custom_domains, ({one, many}) => ({
	custom_domain_routes: many(custom_domain_routes),
	identity_identity: one(identities, {
		fields: [custom_domains.identity],
		references: [identities.email],
		relationName: "custom_domains_identity_identities_email"
	}),
	identity_identity_id: one(identities, {
		fields: [custom_domains.identity_id],
		references: [identities.id],
		relationName: "custom_domains_identity_id_identities_id"
	}),
	publication_domains: many(publication_domains),
}));

export const site_standard_documentsRelations = relations(site_standard_documents, ({one, many}) => ({
	identity: one(identities, {
		fields: [site_standard_documents.identity_did],
		references: [identities.atp_did]
	}),
	site_standard_documents_in_publications: many(site_standard_documents_in_publications),
}));

export const email_subscriptions_to_entityRelations = relations(email_subscriptions_to_entity, ({one}) => ({
	entity: one(entities, {
		fields: [email_subscriptions_to_entity.entity],
		references: [entities.id]
	}),
	permission_token: one(permission_tokens, {
		fields: [email_subscriptions_to_entity.token],
		references: [permission_tokens.id]
	}),
}));

export const atp_poll_votesRelations = relations(atp_poll_votes, ({one}) => ({
	atp_poll_record: one(atp_poll_records, {
		fields: [atp_poll_votes.poll_uri],
		references: [atp_poll_records.uri]
	}),
}));

export const atp_poll_recordsRelations = relations(atp_poll_records, ({many}) => ({
	atp_poll_votes: many(atp_poll_votes),
}));

export const publication_newsletter_settingsRelations = relations(publication_newsletter_settings, ({one}) => ({
	publication: one(publications, {
		fields: [publication_newsletter_settings.publication],
		references: [publications.uri]
	}),
}));

export const publication_email_subscribersRelations = relations(publication_email_subscribers, ({one, many}) => ({
	identity: one(identities, {
		fields: [publication_email_subscribers.identity_id],
		references: [identities.id]
	}),
	publication: one(publications, {
		fields: [publication_email_subscribers.publication],
		references: [publications.uri]
	}),
	publication_email_subscriber_events: many(publication_email_subscriber_events),
}));

export const publication_email_subscriber_eventsRelations = relations(publication_email_subscriber_events, ({one}) => ({
	publication: one(publications, {
		fields: [publication_email_subscriber_events.publication],
		references: [publications.uri]
	}),
	publication_email_subscriber: one(publication_email_subscribers, {
		fields: [publication_email_subscriber_events.subscriber],
		references: [publication_email_subscribers.id]
	}),
}));

export const bsky_followsRelations = relations(bsky_follows, ({one}) => ({
	identity_follows: one(identities, {
		fields: [bsky_follows.follows],
		references: [identities.atp_did],
		relationName: "bsky_follows_follows_identities_atp_did"
	}),
	identity_identity: one(identities, {
		fields: [bsky_follows.identity],
		references: [identities.atp_did],
		relationName: "bsky_follows_identity_identities_atp_did"
	}),
}));

export const site_standard_documents_in_publicationsRelations = relations(site_standard_documents_in_publications, ({one}) => ({
	site_standard_document: one(site_standard_documents, {
		fields: [site_standard_documents_in_publications.document],
		references: [site_standard_documents.uri]
	}),
	site_standard_publication: one(site_standard_publications, {
		fields: [site_standard_documents_in_publications.publication],
		references: [site_standard_publications.uri]
	}),
}));

export const documents_in_publicationsRelations = relations(documents_in_publications, ({one}) => ({
	document: one(documents, {
		fields: [documents_in_publications.document],
		references: [documents.uri]
	}),
	publication: one(publications, {
		fields: [documents_in_publications.publication],
		references: [publications.uri]
	}),
}));

export const leaflet_contributorsRelations = relations(leaflet_contributors, ({one}) => ({
	identity: one(identities, {
		fields: [leaflet_contributors.contributor_did],
		references: [identities.atp_did]
	}),
	permission_token: one(permission_tokens, {
		fields: [leaflet_contributors.leaflet],
		references: [permission_tokens.id]
	}),
}));

export const document_mentions_in_bskyRelations = relations(document_mentions_in_bsky, ({one}) => ({
	document: one(documents, {
		fields: [document_mentions_in_bsky.document],
		references: [documents.uri]
	}),
	bsky_post: one(bsky_posts, {
		fields: [document_mentions_in_bsky.uri],
		references: [bsky_posts.uri]
	}),
}));

export const bsky_postsRelations = relations(bsky_posts, ({many}) => ({
	document_mentions_in_bskies: many(document_mentions_in_bsky),
}));

export const permission_token_on_homepageRelations = relations(permission_token_on_homepage, ({one}) => ({
	identity: one(identities, {
		fields: [permission_token_on_homepage.identity],
		references: [identities.id]
	}),
	permission_token: one(permission_tokens, {
		fields: [permission_token_on_homepage.token],
		references: [permission_tokens.id]
	}),
}));

export const publication_domainsRelations = relations(publication_domains, ({one}) => ({
	custom_domain: one(custom_domains, {
		fields: [publication_domains.domain],
		references: [custom_domains.domain]
	}),
	identity: one(identities, {
		fields: [publication_domains.identity],
		references: [identities.atp_did]
	}),
	publication: one(publications, {
		fields: [publication_domains.publication],
		references: [publications.uri]
	}),
}));

export const publication_contributorsRelations = relations(publication_contributors, ({one}) => ({
	identity: one(identities, {
		fields: [publication_contributors.contributor_did],
		references: [identities.atp_did]
	}),
	publication: one(publications, {
		fields: [publication_contributors.publication_uri],
		references: [publications.uri]
	}),
}));

export const publication_subscriptionsRelations = relations(publication_subscriptions, ({one}) => ({
	identity: one(identities, {
		fields: [publication_subscriptions.identity],
		references: [identities.atp_did]
	}),
	publication: one(publications, {
		fields: [publication_subscriptions.publication],
		references: [publications.uri]
	}),
}));

export const site_standard_subscriptionsRelations = relations(site_standard_subscriptions, ({one}) => ({
	identity: one(identities, {
		fields: [site_standard_subscriptions.identity],
		references: [identities.atp_did]
	}),
	site_standard_publication: one(site_standard_publications, {
		fields: [site_standard_subscriptions.publication],
		references: [site_standard_publications.uri]
	}),
}));

export const user_entitlementsRelations = relations(user_entitlements, ({one}) => ({
	identity: one(identities, {
		fields: [user_entitlements.identity_id],
		references: [identities.id]
	}),
}));

export const permission_token_rightsRelations = relations(permission_token_rights, ({one}) => ({
	entity_set: one(entity_sets, {
		fields: [permission_token_rights.entity_set],
		references: [entity_sets.id]
	}),
	permission_token: one(permission_tokens, {
		fields: [permission_token_rights.token],
		references: [permission_tokens.id]
	}),
}));

export const publication_post_sendsRelations = relations(publication_post_sends, ({one}) => ({
	document: one(documents, {
		fields: [publication_post_sends.document],
		references: [documents.uri]
	}),
	publication: one(publications, {
		fields: [publication_post_sends.publication],
		references: [publications.uri]
	}),
}));

export const leaflets_to_documentsRelations = relations(leaflets_to_documents, ({one}) => ({
	document: one(documents, {
		fields: [leaflets_to_documents.document],
		references: [documents.uri]
	}),
	permission_token: one(permission_tokens, {
		fields: [leaflets_to_documents.leaflet],
		references: [permission_tokens.id]
	}),
}));

export const leaflets_in_publicationsRelations = relations(leaflets_in_publications, ({one}) => ({
	document: one(documents, {
		fields: [leaflets_in_publications.doc],
		references: [documents.uri]
	}),
	permission_token: one(permission_tokens, {
		fields: [leaflets_in_publications.leaflet],
		references: [permission_tokens.id]
	}),
	publication: one(publications, {
		fields: [leaflets_in_publications.publication],
		references: [publications.uri]
	}),
}));

export const publication_pagesRelations = relations(publication_pages, ({one}) => ({
	document: one(documents, {
		fields: [publication_pages.document],
		references: [documents.uri]
	}),
	permission_token: one(permission_tokens, {
		fields: [publication_pages.leaflet_src],
		references: [permission_tokens.id]
	}),
	publication: one(publications, {
		fields: [publication_pages.publication],
		references: [publications.uri]
	}),
}));