/**
 * AgentQuestion Database Mapper
 *
 * Maps between AgentQuestion domain objects and SQLite rows for the
 * agent_questions table. Dates are stored as INTEGER unix milliseconds.
 */
function toMillis(value) {
    if (value instanceof Date)
        return value.getTime();
    if (typeof value === 'number')
        return value;
    if (typeof value === 'string')
        return new Date(value).getTime();
    return 0;
}
export function toDatabase(question) {
    return {
        id: question.id,
        app_id: question.appId ?? '',
        feature_id: question.featureId ?? null,
        agent_run_id: question.agentRunId,
        kind: question.kind,
        prompt: question.prompt,
        options_json: question.optionsJson ?? null,
        default_answer: question.defaultAnswer ?? null,
        answerer: question.answerer,
        status: question.status,
        answer: question.answer ?? null,
        answered_by: question.answeredBy ?? null,
        answered_at: question.answeredAt ? toMillis(question.answeredAt) : null,
        expires_at: question.expiresAt ? toMillis(question.expiresAt) : null,
        created_at: toMillis(question.createdAt),
        updated_at: toMillis(question.updatedAt),
    };
}
export function fromDatabase(row) {
    return {
        id: row.id,
        appId: row.app_id,
        featureId: row.feature_id ?? undefined,
        agentRunId: row.agent_run_id,
        kind: row.kind,
        prompt: row.prompt,
        optionsJson: row.options_json ?? undefined,
        defaultAnswer: row.default_answer ?? undefined,
        answerer: row.answerer,
        status: row.status,
        answer: row.answer ?? undefined,
        answeredBy: row.answered_by ?? undefined,
        answeredAt: row.answered_at ? new Date(row.answered_at) : undefined,
        expiresAt: row.expires_at ? new Date(row.expires_at) : undefined,
        createdAt: new Date(row.created_at),
        updatedAt: new Date(row.updated_at),
    };
}
