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

import { withAuth } from '@/lib/api/helpers';
import { db } from '@/lib/db';
import { tags } from '@/lib/db/schema';
import { errorResponse } from '@/lib/errors';

export const GET = withAuth(async (_req, ctx) => {
  try {
    const data = await db
      .select({ id: tags.id, name: tags.name, color: tags.color })
      .from(tags)
      .where(eq(tags.portId, ctx.portId))
      .orderBy(tags.name);

    return NextResponse.json({ data });
  } catch (error) {
    return errorResponse(error);
  }
});
