import { relations } from 'drizzle-orm';
import { organizations } from './organizations';
import { projects } from './projects';
import { apiKeys } from './apiKeys';
import { flows } from './flows';
import { images } from './images';
import { generations } from './generations';
import { promptUrlCache } from './promptUrlCache';
import { liveScopes } from './liveScopes';

// Export all tables
export * from './organizations';
export * from './projects';
export * from './apiKeys';
export * from './flows';
export * from './images';
export * from './generations';
export * from './promptUrlCache';
export * from './liveScopes';

// Define relations
export const organizationsRelations = relations(organizations, ({ many }) => ({
  projects: many(projects),
  apiKeys: many(apiKeys),
}));

export const projectsRelations = relations(projects, ({ one, many }) => ({
  organization: one(organizations, {
    fields: [projects.organizationId],
    references: [organizations.id],
  }),
  apiKeys: many(apiKeys),
  flows: many(flows),
  images: many(images),
  generations: many(generations),
  promptUrlCache: many(promptUrlCache),
  liveScopes: many(liveScopes),
}));

export const apiKeysRelations = relations(apiKeys, ({ one, many }) => ({
  organization: one(organizations, {
    fields: [apiKeys.organizationId],
    references: [organizations.id],
  }),
  project: one(projects, {
    fields: [apiKeys.projectId],
    references: [projects.id],
  }),
  images: many(images),
  generations: many(generations),
}));

export const flowsRelations = relations(flows, ({ one, many }) => ({
  project: one(projects, {
    fields: [flows.projectId],
    references: [projects.id],
  }),
  images: many(images),
  generations: many(generations),
}));

export const imagesRelations = relations(images, ({ one, many }) => ({
  project: one(projects, {
    fields: [images.projectId],
    references: [projects.id],
  }),
  generation: one(generations, {
    fields: [images.generationId],
    references: [generations.id],
  }),
  flow: one(flows, {
    fields: [images.flowId],
    references: [flows.id],
  }),
  apiKey: one(apiKeys, {
    fields: [images.apiKeyId],
    references: [apiKeys.id],
  }),
  promptUrlCacheEntries: many(promptUrlCache),
}));

export const generationsRelations = relations(generations, ({ one, many }) => ({
  project: one(projects, {
    fields: [generations.projectId],
    references: [projects.id],
  }),
  flow: one(flows, {
    fields: [generations.flowId],
    references: [flows.id],
  }),
  apiKey: one(apiKeys, {
    fields: [generations.apiKeyId],
    references: [apiKeys.id],
  }),
  outputImage: one(images, {
    fields: [generations.outputImageId],
    references: [images.id],
  }),
  promptUrlCacheEntries: many(promptUrlCache),
}));

export const promptUrlCacheRelations = relations(promptUrlCache, ({ one }) => ({
  project: one(projects, {
    fields: [promptUrlCache.projectId],
    references: [projects.id],
  }),
  generation: one(generations, {
    fields: [promptUrlCache.generationId],
    references: [generations.id],
  }),
  image: one(images, {
    fields: [promptUrlCache.imageId],
    references: [images.id],
  }),
}));

export const liveScopesRelations = relations(liveScopes, ({ one }) => ({
  project: one(projects, {
    fields: [liveScopes.projectId],
    references: [projects.id],
  }),
}));
