import {
  pgTable,
  text,
  timestamp,
  integer,
  real,
  index,
  pgEnum,
  uniqueIndex,
} from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";
import { users } from "./users";
import { recordings } from "./recordings";

export const achievementStatusEnum = pgEnum("achievement_status", ["draft", "published"]);

export const achievements = pgTable(
  "achievements",
  {
    id: text("id")
      .primaryKey()
      .$defaultFn(() => crypto.randomUUID()),
    code: text("code").notNull(),
    name: text("name").notNull(),
    description: text("description"),
    icon: text("icon"),
    category: text("category"),
    required_count: integer("required_count").notNull().default(1),
    points_reward: integer("points_reward").notNull().default(0),
    status: achievementStatusEnum("status").notNull().default("published"),
    sort: integer("sort").default(0),
  },
  (t) => [uniqueIndex("achievements_code_idx").on(t.code)],
);

export const user_achievements = pgTable(
  "user_achievements",
  {
    id: integer("id").primaryKey().generatedAlwaysAsIdentity(),
    user_id: text("user_id")
      .notNull()
      .references(() => users.id, { onDelete: "cascade" }),
    achievement_id: text("achievement_id")
      .notNull()
      .references(() => achievements.id, { onDelete: "cascade" }),
    progress: integer("progress").default(0),
    date_unlocked: timestamp("date_unlocked"),
  },
  (t) => [
    index("user_achievements_user_idx").on(t.user_id),
    uniqueIndex("user_achievements_unique_idx").on(t.user_id, t.achievement_id),
  ],
);

export const user_points = pgTable(
  "user_points",
  {
    id: integer("id").primaryKey().generatedAlwaysAsIdentity(),
    user_id: text("user_id")
      .notNull()
      .references(() => users.id, { onDelete: "cascade" }),
    action: text("action").notNull(),
    points: integer("points").notNull(),
    recording_id: text("recording_id").references(() => recordings.id, {
      onDelete: "set null",
    }),
    date_created: timestamp("date_created").notNull().defaultNow(),
  },
  (t) => [
    index("user_points_user_idx").on(t.user_id),
    index("user_points_date_idx").on(t.date_created),
    uniqueIndex("user_points_unique_action_recording")
      .on(t.user_id, t.action, t.recording_id)
      .where(
        sql`"action" IN ('RECORDING_CREATE', 'RECORDING_FEATURED') AND "recording_id" IS NOT NULL`,
      ),
  ],
);

export const user_stats = pgTable(
  "user_stats",
  {
    id: integer("id").primaryKey().generatedAlwaysAsIdentity(),
    user_id: text("user_id")
      .notNull()
      .references(() => users.id, { onDelete: "cascade" }),
    total_raw_points: integer("total_raw_points").default(0),
    total_weighted_points: real("total_weighted_points").default(0),
    recordings_count: integer("recordings_count").default(0),
    playbacks_count: integer("playbacks_count").default(0),
    comments_count: integer("comments_count").default(0),
    achievements_count: integer("achievements_count").default(0),
    last_updated: timestamp("last_updated").defaultNow(),
  },
  (t) => [uniqueIndex("user_stats_user_idx").on(t.user_id)],
);

export type Achievement = typeof achievements.$inferSelect;
export type UserStats = typeof user_stats.$inferSelect;
