import { and, desc, eq } from "drizzle-orm";
import { assistantProfiles } from "../db/schema.js";
import { uuidv7 } from "../db/uuidv7.js";
function parseJsonArray(value) {
    if (!value)
        return [];
    try {
        const parsed = JSON.parse(value);
        return Array.isArray(parsed) ? parsed.filter((item) => typeof item === "string") : [];
    }
    catch {
        return [];
    }
}
function serializeStringArray(value) {
    return JSON.stringify((value ?? []).filter((item) => typeof item === "string" && item.trim().length > 0));
}
function toAssistantProfile(row) {
    return {
        id: row.id,
        userId: row.userId,
        name: row.name,
        description: row.description,
        systemPrompt: row.systemPrompt,
        runtimeMode: row.runtimeMode,
        toolProfile: row.toolProfile,
        enabledSkills: parseJsonArray(row.enabledSkills),
        enabledPlugins: parseJsonArray(row.enabledPlugins),
        isDefault: row.isDefault === 1,
        createdAt: row.createdAt,
        updatedAt: row.updatedAt,
    };
}
export class AssistantProfileService {
    db;
    constructor(db) {
        this.db = db;
    }
    list(userId) {
        const query = this.db.select().from(assistantProfiles);
        const rows = userId
            ? query.where(eq(assistantProfiles.userId, userId)).orderBy(desc(assistantProfiles.updatedAt)).all()
            : query.orderBy(desc(assistantProfiles.updatedAt)).all();
        return rows.map(toAssistantProfile);
    }
    getById(id, userId) {
        const row = userId
            ? this.db.select().from(assistantProfiles).where(and(eq(assistantProfiles.id, id), eq(assistantProfiles.userId, userId))).get()
            : this.db.select().from(assistantProfiles).where(eq(assistantProfiles.id, id)).get();
        return row ? toAssistantProfile(row) : undefined;
    }
    create(userId, params) {
        const id = uuidv7();
        const now = new Date().toISOString();
        const existing = this.list(userId);
        const shouldBeDefault = params.isDefault === true || existing.length === 0;
        if (shouldBeDefault)
            this.clearDefault(userId);
        this.db.insert(assistantProfiles).values({
            id,
            userId,
            name: params.name.trim(),
            description: params.description ?? null,
            systemPrompt: params.systemPrompt ?? null,
            runtimeMode: params.runtimeMode ?? null,
            toolProfile: params.toolProfile ?? null,
            enabledSkills: serializeStringArray(params.enabledSkills),
            enabledPlugins: serializeStringArray(params.enabledPlugins),
            isDefault: shouldBeDefault ? 1 : 0,
            createdAt: now,
            updatedAt: now,
        }).run();
        return this.getById(id, userId);
    }
    update(id, userId, params) {
        const existing = this.getById(id, userId);
        if (!existing)
            return undefined;
        if (params.isDefault === true)
            this.clearDefault(userId);
        const now = new Date().toISOString();
        this.db.update(assistantProfiles).set({
            name: params.name !== undefined ? params.name.trim() : undefined,
            description: params.description !== undefined ? params.description : undefined,
            systemPrompt: params.systemPrompt !== undefined ? params.systemPrompt : undefined,
            runtimeMode: params.runtimeMode !== undefined ? params.runtimeMode : undefined,
            toolProfile: params.toolProfile !== undefined ? params.toolProfile : undefined,
            enabledSkills: params.enabledSkills !== undefined ? serializeStringArray(params.enabledSkills) : undefined,
            enabledPlugins: params.enabledPlugins !== undefined ? serializeStringArray(params.enabledPlugins) : undefined,
            isDefault: params.isDefault !== undefined ? (params.isDefault ? 1 : 0) : undefined,
            updatedAt: now,
        }).where(and(eq(assistantProfiles.id, id), eq(assistantProfiles.userId, userId))).run();
        const updated = this.getById(id, userId);
        if (!updated)
            return undefined;
        if (!this.list(userId).some((profile) => profile.isDefault)) {
            this.db.update(assistantProfiles).set({ isDefault: 1, updatedAt: now }).where(and(eq(assistantProfiles.id, id), eq(assistantProfiles.userId, userId))).run();
        }
        return this.getById(id, userId);
    }
    delete(id, userId) {
        const existing = this.getById(id, userId);
        if (!existing)
            return false;
        this.db.delete(assistantProfiles).where(and(eq(assistantProfiles.id, id), eq(assistantProfiles.userId, userId))).run();
        if (existing.isDefault) {
            const next = this.list(userId)[0];
            if (next) {
                this.db.update(assistantProfiles).set({ isDefault: 1, updatedAt: new Date().toISOString() }).where(eq(assistantProfiles.id, next.id)).run();
            }
        }
        return true;
    }
    clearDefault(userId) {
        this.db.update(assistantProfiles).set({ isDefault: 0, updatedAt: new Date().toISOString() }).where(eq(assistantProfiles.userId, userId)).run();
    }
}
//# sourceMappingURL=assistant-profiles.js.map