import { NextResponse } from 'next/server';
import {
  canManageTrainingMatrix,
  getTrainingMatrixOptions
} from '@/features/training-matrix/server/training-matrix-data';
import { AuthError, requireOrganizationAccess } from '@/lib/auth/session';

export async function GET() {
  try {
    const { organization, membership } = await requireOrganizationAccess();

    if (!canManageTrainingMatrix(membership.role)) {
      return NextResponse.json({ message: 'Forbidden' }, { status: 403 });
    }

    const options = await getTrainingMatrixOptions(organization.id);
    return NextResponse.json(options);
  } catch (error) {
    if (error instanceof AuthError) {
      return NextResponse.json({ message: error.message }, { status: error.status });
    }

    return NextResponse.json({ message: 'Unable to load training matrix options' }, { status: 500 });
  }
}
