import { getPrismaClient } from '../db/prisma.js';
function toPrismaStatus(status) {
    switch (status) {
        case 'created': return 'CREATED';
        case 'queued': return 'QUEUED';
        case 'running': return 'RUNNING';
        case 'completed': return 'COMPLETED';
        case 'failed': return 'FAILED';
        case 'killed': return 'KILLED';
        default: return 'CREATED';
    }
}
function fromPrismaStatus(status) {
    const statusStr = String(status).toUpperCase();
    switch (statusStr) {
        case 'CREATED': return 'created';
        case 'QUEUED': return 'queued';
        case 'RUNNING': return 'running';
        case 'COMPLETED': return 'completed';
        case 'SUCCEEDED': return 'completed';
        case 'FAILED': return 'failed';
        case 'KILLED': return 'killed';
        default: return 'created';
    }
}
function toRun(dbRun) {
    return {
        id: dbRun.id,
        name: dbRun.name,
        status: fromPrismaStatus(dbRun.status),
        input: dbRun.input,
        output: dbRun.output,
        logs: Array.isArray(dbRun.logs) ? dbRun.logs : [],
        clientToken: dbRun.clientToken,
        tags: Array.isArray(dbRun.tags) ? dbRun.tags : [],
        startedAt: dbRun.startedAt?.toISOString() || null,
        completedAt: dbRun.completedAt?.toISOString() || null,
        createdAt: dbRun.createdAt.toISOString(),
        updatedAt: dbRun.updatedAt.toISOString(),
    };
}
export class PostgresRunStore {
    prisma;
    prismaPromise = null;
    constructor(prisma) {
        if (prisma) {
            this.prisma = prisma;
        }
    }
    async ensurePrisma() {
        if (this.prisma) {
            return this.prisma;
        }
        if (this.prismaPromise) {
            return await this.prismaPromise;
        }
        this.prismaPromise = getPrismaClient().then(client => {
            this.prisma = client;
            return client;
        });
        return await this.prismaPromise;
    }
    async createRun(data) {
        const prisma = await this.ensurePrisma();
        const runId = data.id || undefined;
        const dbRun = await prisma.run.create({
            data: {
                id: runId,
                name: data.name || 'Test Run',
                status: toPrismaStatus('created'),
                input: data.input || null,
                logs: [],
                clientToken: data.clientToken || null,
                tags: data.tags || [],
            },
        });
        return toRun(dbRun);
    }
    async getRunById(id) {
        const prisma = await this.ensurePrisma();
        const dbRun = await prisma.run.findUnique({
            where: { id },
        });
        return dbRun ? toRun(dbRun) : null;
    }
    async getRunByClientToken(clientToken) {
        const prisma = await this.ensurePrisma();
        const dbRun = await prisma.run.findUnique({
            where: { clientToken },
        });
        return dbRun ? toRun(dbRun) : null;
    }
    async listRuns(filter) {
        const prisma = await this.ensurePrisma();
        const where = {};
        if (filter?.tag) {
            where.tags = {
                has: filter.tag,
            };
        }
        if (filter?.status) {
            where.status = toPrismaStatus(filter.status);
        }
        const dbRuns = await prisma.run.findMany({
            where,
            orderBy: { createdAt: 'desc' },
            take: filter?.limit,
        });
        return dbRuns.map(toRun);
    }
    async updateRunStatus(id, status, extra) {
        const updateData = {
            status: toPrismaStatus(status),
        };
        if (extra) {
            if (extra.input !== undefined)
                updateData.input = extra.input;
            if (extra.output !== undefined)
                updateData.output = extra.output;
            if (extra.logs !== undefined)
                updateData.logs = extra.logs;
            if (extra.startedAt !== undefined) {
                updateData.startedAt = extra.startedAt ? new Date(extra.startedAt) : null;
            }
            if (extra.completedAt !== undefined) {
                updateData.completedAt = extra.completedAt ? new Date(extra.completedAt) : null;
            }
        }
        if (status === 'running' && !updateData.startedAt) {
            updateData.startedAt = new Date();
        }
        if ((status === 'completed' || status === 'failed') && !updateData.completedAt) {
            updateData.completedAt = new Date();
        }
        const prisma = await this.ensurePrisma();
        await prisma.run.update({
            where: { id },
            data: updateData,
        });
    }
    async deleteRun(id) {
        const prisma = await this.ensurePrisma();
        await prisma.run.delete({
            where: { id },
        });
    }
}
//# sourceMappingURL=postgresRunStore.js.map