/**
 * Ticket Digest Config API
 * GET  /api/reports/ticket-digest/config — Get config + available channels
 * PUT  /api/reports/ticket-digest/config — Update config
 */

import { NextRequest, NextResponse } from 'next/server';
import { getTicketDigestService } from '@/lib/services/ticket-digest-service';

export async function GET() {
  try {
    const service = getTicketDigestService();
    const [config, channels] = await Promise.all([
      service.getConfig(),
      service.getAvailableChannels(),
    ]);
    return NextResponse.json({ config, channels });
  } catch (error) {
    const msg = error instanceof Error ? error.message : String(error);
    return NextResponse.json({ error: msg }, { status: 500 });
  }
}

export async function PUT(request: NextRequest) {
  try {
    const body = await request.json();
    const service = getTicketDigestService();
    const config = await service.updateConfig(body);
    return NextResponse.json({ config });
  } catch (error) {
    const msg = error instanceof Error ? error.message : String(error);
    return NextResponse.json({ error: msg }, { status: 500 });
  }
}
