import { desc, eq } from "drizzle-orm";
import { automationRepoProposals } from "../db/schema.js";
import { uuidv7 } from "../db/uuidv7.js";
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();
        this.db.insert(automationRepoProposals).values({
            id,
            repoId: params.repoId,
            userId: params.userId ?? null,
            message,
            status: params.status ?? "open",
            priority: params.priority ?? "normal",
            dueDate: params.dueDate ?? null,
            tags: JSON.stringify(params.tags ?? []),
            sourceThreadId: params.sourceThreadId ?? null,
            sourceThreadTitle: params.sourceThreadTitle ?? null,
            createdAt: now,
            updatedAt: now,
        }).run();
        return this.getById(id);
    }
    update(id, params) {
        const set = {
            updatedAt: new Date().toISOString(),
        };
        if (params.message !== undefined) {
            set.message = params.message.trim();
        }
        if (params.status !== undefined)
            set.status = params.status;
        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