import { eq, inArray, and, or, ilike, isNull, sql } from "drizzle-orm";
import { database } from "~/database/context";
import * as schema from "~/database/schema";
import { generateFlagConfig } from "~/lib/flag-generator";

export type User = typeof schema.users.$inferSelect;
export type NewUser = typeof schema.users.$inferInsert;

export const USERNAME_RE = /^[a-zA-Z0-9_-]{3,30}$/;

export function getUserDisplayName(
  user: Pick<User, "username" | "displayName">
): string | null {
  return user.username ?? user.displayName ?? null;
}

export async function createUser(data: NewUser): Promise<User> {
  const db = database();
  const id = data.id ?? crypto.randomUUID();
  const [user] = await db
    .insert(schema.users)
    .values({ ...data, id, flagConfig: data.flagConfig ?? generateFlagConfig(id) })
    .returning();
  return user;
}

export async function findUserById(id: string): Promise<User | undefined> {
  const db = database();
  return await db.query.users.findFirst({
    where: eq(schema.users.id, id),
  });
}

export async function findUsersByIds(ids: string[]): Promise<User[]> {
  if (ids.length === 0) return [];
  const db = database();
  return await db
    .select()
    .from(schema.users)
    .where(inArray(schema.users.id, ids));
}

export async function findUserByUsername(username: string): Promise<User | undefined> {
  const db = database();
  return await db.query.users.findFirst({
    where: (u) => sql`lower(${u.username}) = lower(${username})`,
  });
}

export async function updateUser(
  id: string,
  data: Partial<NewUser>
): Promise<User> {
  const db = database();
  const [user] = await db
    .update(schema.users)
    .set({ ...data, updatedAt: new Date() })
    .where(eq(schema.users.id, id))
    .returning();
  return user;
}

export async function anonymizeUserAccount(id: string): Promise<void> {
  const db = database();
  const shortId = id.replace(/-/g, "").slice(0, 8);
  await db.transaction(async (tx) => {
    await tx.delete(schema.sessions).where(eq(schema.sessions.userId, id));
    await tx.delete(schema.accounts).where(eq(schema.accounts.userId, id));
    await tx
      .update(schema.users)
      .set({
        email: `deleted-${shortId}@deleted.brackt.com`,
        displayName: null,
        username: null,
        imageUrl: null,
        customAvatarUrl: null,
        flagConfig: null,
        avatarType: "flag",
        deletedAt: new Date(),
        updatedAt: new Date(),
      })
      .where(eq(schema.users.id, id));
  });
}

export async function findAdmins(): Promise<User[]> {
  const db = database();
  return await db.query.users.findMany({
    where: eq(schema.users.isAdmin, true),
    orderBy: (users, { asc }) => [asc(users.displayName)],
  });
}

export async function isUserAdmin(userId: string): Promise<boolean> {
  const user = await findUserById(userId);
  return user?.isAdmin ?? false;
}

export async function setUserAdmin(userId: string, isAdmin: boolean): Promise<User> {
  return await updateUser(userId, { isAdmin });
}

export async function findAllUsers(): Promise<User[]> {
  const db = database();
  return await db.query.users.findMany({
    orderBy: (users, { asc }) => [asc(users.displayName)],
  });
}

export async function findUsersPaginated(options: {
  limit: number;
  offset: number;
  search?: string;
}): Promise<{ users: User[]; total: number; hasMore: boolean }> {
  const db = database();
  const notDeleted = isNull(schema.users.deletedAt);
  const filter = options.search
    ? and(
        notDeleted,
        or(
          ilike(schema.users.email, `%${options.search}%`),
          ilike(schema.users.username, `%${options.search}%`),
          ilike(schema.users.displayName, `%${options.search}%`),
        ),
      )
    : notDeleted;

  const [users, [{ count }]] = await Promise.all([
    db.query.users.findMany({
      where: filter,
      orderBy: (t, { asc }) => [asc(t.displayName)],
      limit: options.limit,
      offset: options.offset,
    }),
    db
      .select({ count: sql<number>`count(*)::int` })
      .from(schema.users)
      .where(filter),
  ]);
  return { users, total: count, hasMore: options.offset + users.length < count };
}

/**
 * Returns true if the user currently owns a team in a draft-status season.
 * Used to prevent timezone changes mid-draft.
 */
export async function isUserInActiveDraft(userId: string): Promise<boolean> {
  const db = database();
  const result = await db
    .select({ id: schema.teams.id })
    .from(schema.teams)
    .innerJoin(schema.seasons, eq(schema.teams.seasonId, schema.seasons.id))
    .where(
      and(
        eq(schema.teams.ownerId, userId),
        eq(schema.seasons.status, "draft")
      )
    )
    .limit(1);
  return result.length > 0;
}
