import { NextResponse } from 'next/server';
import { and, eq } from 'drizzle-orm';

import { withAuth } from '@/lib/api/helpers';
import { db } from '@/lib/db';
import { systemSettings } from '@/lib/db/schema/system';
import { errorResponse, ValidationError } from '@/lib/errors';

export const GET = withAuth(async (req, ctx) => {
  try {
    const key = req.nextUrl.searchParams.get('key');
    if (!key) throw new ValidationError('key query parameter is required');

    const setting = await db.query.systemSettings.findFirst({
      where: and(eq(systemSettings.key, key), eq(systemSettings.portId, ctx.portId)),
    });

    return NextResponse.json({ enabled: setting?.value === true });
  } catch (error) {
    return errorResponse(error);
  }
});
