'use strict';

exports.up = async function (db) {
    const owner = await db.runSql('SELECT username FROM users WHERE role="owner" LIMIT 1');
    if (!owner[0]) return;

    const ownerUsername = owner[0].username;

    const apps = await db.runSql('SELECT * FROM apps');
    for (const app of apps) {
        if (!app.checklistJson) continue;

        const checklist = JSON.parse(app.checklistJson);
        for (const key in checklist) {
          if (!checklist[key].acknowledged) continue;

          if (!checklist[key].changedAt) checklist[key].changedAt = Date.now();
          if (!checklist[key].changedBy) checklist[key].changedBy = ownerUsername;
        }

        await db.runSql('UPDATE apps SET checklistJson=? WHERE id=?', [ JSON.stringify(checklist), app.id ]);
    }
};

exports.down = async function () {
};
