import { eq, desc } from "drizzle-orm";
import { drizzle } from "drizzle-orm/mysql2";
import { InsertUser, users, conversations, khayoPersonality, messages, selfUpdates } from "../drizzle/schema";
import { ENV } from './_core/env';

let _db: ReturnType<typeof drizzle> | null = null;

// Lazily create the drizzle instance so local tooling can run without a DB.
export async function getDb() {
  if (!_db && process.env.DATABASE_URL) {
    try {
      _db = drizzle(process.env.DATABASE_URL);
    } catch (error) {
      console.warn("[Database] Failed to connect:", error);
      _db = null;
    }
  }
  return _db;
}

export async function upsertUser(user: InsertUser): Promise<void> {
  if (!user.openId) {
    throw new Error("User openId is required for upsert");
  }

  const db = await getDb();
  if (!db) {
    console.warn("[Database] Cannot upsert user: database not available");
    return;
  }

  try {
    const values: InsertUser = {
      openId: user.openId,
    };
    const updateSet: Record<string, unknown> = {};

    const textFields = ["name", "email", "loginMethod"] as const;
    type TextField = (typeof textFields)[number];

    const assignNullable = (field: TextField) => {
      const value = user[field];
      if (value === undefined) return;
      const normalized = value ?? null;
      values[field] = normalized;
      updateSet[field] = normalized;
    };

    textFields.forEach(assignNullable);

    if (user.lastSignedIn !== undefined) {
      values.lastSignedIn = user.lastSignedIn;
      updateSet.lastSignedIn = user.lastSignedIn;
    }
    if (user.role !== undefined) {
      values.role = user.role;
      updateSet.role = user.role;
    } else if (user.openId === ENV.ownerOpenId) {
      values.role = 'admin';
      updateSet.role = 'admin';
    }

    if (!values.lastSignedIn) {
      values.lastSignedIn = new Date();
    }

    if (Object.keys(updateSet).length === 0) {
      updateSet.lastSignedIn = new Date();
    }

    await db.insert(users).values(values).onDuplicateKeyUpdate({
      set: updateSet,
    });
  } catch (error) {
    console.error("[Database] Failed to upsert user:", error);
    throw error;
  }
}

export async function getUserByOpenId(openId: string) {
  const db = await getDb();
  if (!db) {
    console.warn("[Database] Cannot get user: database not available");
    return undefined;
  }

  const result = await db.select().from(users).where(eq(users.openId, openId)).limit(1);

  return result.length > 0 ? result[0] : undefined;
}

/**
 * Get the current Khayo personality directive
 */
export async function getKhayoPersonality() {
  const db = await getDb();
  if (!db) return null;

  const result = await db
    .select()
    .from(khayoPersonality)
    .orderBy((t) => desc(t.version))
    .limit(1);

  return result.length > 0 ? result[0] : null;
}

/**
 * Update Khayo's personality with a new directive
 */
export async function updateKhayoPersonality(newDirective: string) {
  const db = await getDb();
  if (!db) throw new Error("Database not available");

  const currentPersonality = await getKhayoPersonality();
  const newVersion = (currentPersonality?.version ?? 0) + 1;

  const result = await db.insert(khayoPersonality).values({
    directive: newDirective,
    version: newVersion,
  });

  return { version: newVersion, directive: newDirective };
}

/**
 * Create a new conversation for a user
 */
export async function createConversation(userId: number, title: string = "New Chat") {
  const db = await getDb();
  if (!db) throw new Error("Database not available");

  const result = await db.insert(conversations).values({
    userId,
    title,
  });

  return result;
}

/**
 * Get all conversations for a user
 */
export async function getUserConversations(userId: number) {
  const db = await getDb();
  if (!db) return [];

  const result = await db
    .select()
    .from(conversations)
    .where(eq(conversations.userId, userId))
    .orderBy((t) => desc(t.updatedAt));

  return result;
}

/**
 * Get a specific conversation
 */
export async function getConversation(conversationId: number) {
  const db = await getDb();
  if (!db) return null;

  const result = await db
    .select()
    .from(conversations)
    .where(eq(conversations.id, conversationId))
    .limit(1);

  return result.length > 0 ? result[0] : null;
}

/**
 * Get all messages in a conversation
 */
export async function getConversationMessages(conversationId: number) {
  const db = await getDb();
  if (!db) return [];

  const result = await db
    .select()
    .from(messages)
    .where(eq(messages.conversationId, conversationId))
    .orderBy((t) => t.createdAt);

  return result;
}

/**
 * Add a message to a conversation
 */
export async function addMessage(
  conversationId: number,
  role: "user" | "assistant",
  content: string
) {
  const db = await getDb();
  if (!db) throw new Error("Database not available");

  const result = await db.insert(messages).values({
    conversationId,
    role,
    content,
  });

  return result;
}

/**
 * Record a self-update event
 */
export async function recordSelfUpdate(
  directive: string,
  previousVersion: number,
  newVersion: number
) {
  const db = await getDb();
  if (!db) throw new Error("Database not available");

  const result = await db.insert(selfUpdates).values({
    directive,
    previousVersion,
    newVersion,
  });

  return result;
}


