import { getPocketBase } from '../lib/pocketbase.js';

export async function applyVersion(newVersionId: string): Promise<void> {
  const pb = await getPocketBase();

  // Get all employees
  const employees = await pb.collection('employee_curriculum_state').getFullList();

  for (const emp of employees) {
    await pb.collection('employee_curriculum_state').update(emp.id, {
      active_version: newVersionId,
    });
  }

  // Supersede old active version
  const activeVersions = await pb.collection('curriculum_versions').getFullList({
    filter: 'status = "active"',
  });
  for (const v of activeVersions) {
    await pb.collection('curriculum_versions').update(v.id, { status: 'superseded' });
  }

  // Activate new version
  await pb.collection('curriculum_versions').update(newVersionId, { status: 'active' });
}
