import { NextResponse } from 'next/server';
import type { RowDataPacket } from 'mysql2';

import { requireAdminApi } from '@/lib/auth/apiAuth';
import { getPool } from '@/lib/db';

export const dynamic = 'force-dynamic';

export async function GET() {
  const auth = await requireAdminApi();
  if (!auth.ok) {
    return auth.response;
  }

  const pool = getPool();
  const [rows] = await pool.query<RowDataPacket[]>(
    'SELECT id, name_id, name, name_jp FROM characters ORDER BY id ASC'
  );
  return NextResponse.json({ characters: rows });
}
