/**
 * P1-UX-3 — add Google Meet / Classroom link columns to lesson_plans.
 *
 * Adds two optional URL columns:
 *   - meet_url      — Google Meet link for that lesson's live session
 *   - classroom_url — Google Classroom link for the assignment/unit
 *
 * Both default to NULL (no link set yet). The teacher-side form collects
 * them via the TeacherTools.tsx lesson-plan modal; the server validates
 * the URLs to be http(s) before persisting.
 *
 * Idempotent: each ADD COLUMN is wrapped in a try/catch swallow so re-
 * applying this migration against an already-migrated DB is a no-op,
 * matching the convention used by the rest of the migrations folder.
 *
 * Scoped to P1-UX-3 from the readiness audit (closed via §3 of the UI/UX
 * feedback PDF). The student-side "Join Class" surface belongs to
 * P1-UX-7 / §9 Course Scheme/Plan; this migration just gives the column.
 */

const logger = (line) => (typeof console !== 'undefined' ? console.log(line) : null);

exports.up = async function (knex) {
  try {
    await knex.schema.raw('ALTER TABLE lesson_plans ADD COLUMN meet_url TEXT');
    logger('[migrate:2026-07-17] lesson_plans.meet_url added');
  } catch (err) {
    // Column may already exist from a previous run — ignore.
    if (!/duplicate column|already exists/i.test(String(err && err.message))) {
      throw err;
    }
  }
  try {
    await knex.schema.raw('ALTER TABLE lesson_plans ADD COLUMN classroom_url TEXT');
    logger('[migrate:2026-07-17] lesson_plans.classroom_url added');
  } catch (err) {
    if (!/duplicate column|already exists/i.test(String(err && err.message))) {
      throw err;
    }
  }
};

exports.down = async function (knex) {
  // SQLite supports `ALTER TABLE ... DROP COLUMN` since 3.35 (March 2021).
  // better-sqlite3 bundles a recent version, so this is safe.
  await knex.schema.raw('ALTER TABLE lesson_plans DROP COLUMN meet_url');
  await knex.schema.raw('ALTER TABLE lesson_plans DROP COLUMN classroom_url');
};
