import { desc, eq } from "drizzle-orm";
import { automationRepoProposals } from "../db/schema.js";
import { uuidv7 } from "../db/uuidv7.js";
function parseCompletionHistory(value) {
    if (!value)
        return [];
    try {
        const parsed = JSON.parse(value);
        if (!Array.isArray(parsed))
            return [];
        return parsed.filter((entry) => {
            if (!entry || typeof entry !== "object")
                return false;
            const record = entry;
            return typeof record.completedAt === "string"
                && (typeof record.previousStatus === "string" || record.previousStatus === null);
        });
    }
    catch {
        return [];
    }
}
export class RepoProposalService {
    db;
    constructor(db) {
        this.db = db;
    }
    listByRepo(repoId) {
        return this.db
            .select()
            .from(automationRepoProposals)
            .where(eq(automationRepoProposals.repoId, repoId))
            .orderBy(desc(automationRepoProposals.updatedAt))
            .all();
    }
    getById(id) {
        return this.db
            .select()
            .from(automationRepoProposals)
            .where(eq(automationRepoProposals.id, id))
            .get();
    }
    create(params) {
        const message = params.message.trim();
        const now = new Date().toISOString();
        const id = uuidv7();
        const status = params.status ?? "open";
        const completionHistory = status === "done"
            ? JSON.stringify([{ completedAt: now, previousStatus: null }])
            : "[]";
        this.db.insert(automationRepoProposals).values({
            id,
            repoId: params.repoId,
            userId: params.userId ?? null,
            message,
            status,
            priority: params.priority ?? "normal",
            dueDate: params.dueDate ?? null,
            tags: JSON.stringify(params.tags ?? []),
            completedAt: status === "done" ? now : null,
            completionHistory,
            sourceThreadId: params.sourceThreadId ?? null,
            sourceThreadTitle: params.sourceThreadTitle ?? null,
            createdAt: now,
            updatedAt: now,
        }).run();
        return this.getById(id);
    }
    update(id, params) {
        const existing = this.getById(id);
        if (!existing)
            return undefined;
        const now = new Date().toISOString();
        const set = {
            updatedAt: now,
        };
        if (params.message !== undefined) {
            set.message = params.message.trim();
        }
        if (params.status !== undefined) {
            set.status = params.status;
            if (params.status === "done" && existing.status !== "done") {
                const history = parseCompletionHistory(existing.completionHistory);
                set.completedAt = now;
                set.completionHistory = JSON.stringify([
                    { completedAt: now, previousStatus: existing.status },
                    ...history,
                ].slice(0, 100));
            }
            else if (params.status !== "done" && existing.status === "done") {
                set.completedAt = null;
            }
        }
        if (params.priority !== undefined)
            set.priority = params.priority;
        if (params.dueDate !== undefined)
            set.dueDate = params.dueDate;
        if (params.tags !== undefined)
            set.tags = JSON.stringify(params.tags);
        this.db.update(automationRepoProposals).set(set).where(eq(automationRepoProposals.id, id)).run();
        return this.getById(id);
    }
    delete(id) {
        this.db.delete(automationRepoProposals).where(eq(automationRepoProposals.id, id)).run();
    }
}
//# sourceMappingURL=repo-proposals.js.map