import { relations } from "drizzle-orm";
import { integer, pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";

export const finished_videos = pgTable("finished_videos", {
  id: uuid("id").primaryKey(),
  bucket_object: uuid("bucket_object").notNull().references(() => bucket_objects.id),
  name: text("name").notNull(),
  created_by: text("created_by").notNull(),
  tags: text("tags").notNull(),
  media_title: text("media_title").notNull(),
  media_description: text("media_description").notNull(),
  created_at: timestamp("created_at").defaultNow(),
});

export const finishedVideosRelations = relations(finished_videos, ({ one }) => ({
  bucketObject: one(bucket_objects, {
    fields: [finished_videos.bucket_object],
    references: [bucket_objects.id],
  }),
}));

export const bucket_objects = pgTable("bucket_objects", {
  id: uuid("id").primaryKey(),
  video_id: uuid("video_id").notNull().references(() => videos.id),
  post_id: uuid("post_id").notNull().references(() => posts.id),
  created_at: timestamp("created_at").defaultNow(),
});

export const bucketObjectsRelations = relations(bucket_objects, ({ one }) => ({
  video: one(videos, {
    fields: [bucket_objects.video_id],
    references: [videos.id],
  }),
  post: one(posts, {
    fields: [bucket_objects.post_id],
    references: [posts.id],
  }),
}));

export const videos = pgTable("videos", {
  id: uuid("id").primaryKey(),
  title: text("title").notNull(),
  description: text("description").notNull(),
  youtube_id: text("youtube_id").notNull(),
  thumbnail: text("thumbnail").notNull(),
  duration: integer("duration").notNull(),
  size: integer("size").notNull(),
  likes: integer("likes").notNull(),
  views: integer("views").notNull(),
  published_at: timestamp("published_at").notNull(),
  created_at: timestamp("created_at").defaultNow(),
});

export const videosRelations = relations(videos, ({ many }) => ({
  videoTags: many(video_tags),
  bucketObjects: many(bucket_objects),
}));

export const posts = pgTable("posts", {
  id: uuid("id").primaryKey(),
  subreddit: text("subreddit").notNull(),
  title: text("title").notNull(),
  description: text("description").notNull(),
  upvotes: integer("upvotes").notNull(),
  published_at: timestamp("published_at").notNull(),
  created_at: timestamp("created_at").defaultNow(),
});

export const postsRelations = relations(posts, ({ many }) => ({
  postTags: many(post_tags),
  comments: many(comments),
  bucketObjects: many(bucket_objects),
}));

export const comments = pgTable("comments", {
  id: uuid("id").primaryKey(),
  post_id: uuid("post_id").notNull().references(() => posts.id),
  comment: text("comment").notNull(),
  upvotes: integer("upvotes").notNull(),
  published_at: timestamp("published_at").notNull(),
  created_at: timestamp("created_at").defaultNow(),
});

export const commentsRelations = relations(comments, ({ one, many }) => ({
  post: one(posts, {
    fields: [comments.post_id],
    references: [posts.id],
  }),
  commentTags: many(comments_tags),
}));

export const tags = pgTable("tags", {
  id: uuid("id").primaryKey(),
  name: text("name").notNull(),
});

export const tagsRelations = relations(tags, ({ many }) => ({
  videoTags: many(video_tags),
  postTags: many(post_tags),
  commentTags: many(comments_tags),
}));

export const video_tags = pgTable("video_tags", {
  id: uuid("id").primaryKey(),
  video_id: uuid("video_id").notNull().references(() => videos.id),
  tag_id: uuid("tag_id").notNull().references(() => tags.id),
});

export const videoTagsRelations = relations(video_tags, ({ one }) => ({
  video: one(videos, {
    fields: [video_tags.video_id],
    references: [videos.id],
  }),
  tag: one(tags, {
    fields: [video_tags.tag_id],
    references: [tags.id],
  }),
}));

export const post_tags = pgTable("post_tags", {
  id: uuid("id").primaryKey(),
  post_id: uuid("post_id").notNull().references(() => posts.id),
  tag_id: uuid("tag_id").notNull().references(() => tags.id),
});

export const postTagsRelations = relations(post_tags, ({ one }) => ({
  post: one(posts, {
    fields: [post_tags.post_id],
    references: [posts.id],
  }),
  tag: one(tags, {
    fields: [post_tags.tag_id],
    references: [tags.id],
  }),
}));

export const comments_tags = pgTable("comments_tags", {
  id: uuid("id").primaryKey(),
  comment_id: uuid("comment_id").notNull().references(() => comments.id),
  tag_id: uuid("tag_id").notNull().references(() => tags.id),
});

export const commentsTagsRelations = relations(comments_tags, ({ one }) => ({
  comment: one(comments, {
    fields: [comments_tags.comment_id],
    references: [comments.id],
  }),
  tag: one(tags, {
    fields: [comments_tags.tag_id],
    references: [tags.id],
  }),
}));

export const workflows = pgTable("workflows", {
  id: uuid("id").primaryKey(),
  video_id: uuid("video_id").notNull().references(() => videos.id),
  status: text("status").notNull(),
  fsm_state: text("fsm_state").notNull(),
  title: text("title"),
  description: text("description"),
  tags: text("tags"),
  created_at: timestamp("created_at").defaultNow(),
  updated_at: timestamp("updated_at").defaultNow(),
});

export const workflow_states = pgTable("workflow_states", {
  id: uuid("id").primaryKey(),
  workflow_id: uuid("workflow_id").notNull().references(() => workflows.id),
  state: text("state").notNull(),
  event: text("event").notNull(),
  metadata: text("metadata"),
  timestamp: timestamp("timestamp").defaultNow(),
});

export const service_callbacks = pgTable("service_callbacks", {
  id: uuid("id").primaryKey(),
  workflow_id: uuid("workflow_id").notNull().references(() => workflows.id),
  service: text("service").notNull(),
  result: text("result"),
  error: text("error"),
  timestamp: timestamp("timestamp").defaultNow(),
});

export const workflowsRelations = relations(workflows, ({ one, many }) => ({
  video: one(videos, {
    fields: [workflows.video_id],
    references: [videos.id],
  }),
  states: many(workflow_states),
  callbacks: many(service_callbacks),
}));

export const workflowStatesRelations = relations(workflow_states, ({ one }) => ({
  workflow: one(workflows, {
    fields: [workflow_states.workflow_id],
    references: [workflows.id],
  }),
}));

export const serviceCallbacksRelations = relations(service_callbacks, ({ one }) => ({
  workflow: one(workflows, {
    fields: [service_callbacks.workflow_id],
    references: [workflows.id],
  }),
}));
