import { NextRequest, NextResponse } from 'next/server';
import { Redis } from 'ioredis';

// TAHAP 3: Kill-Switch API Endpoint (IAM)
export async function POST(req: NextRequest) {
  try {
    const { email } = await req.json();

    if (!email) {
      return NextResponse.json({ success: false, error: 'Email required' }, { status: 400 });
    }

    // Publish event ke Redis untuk dibaca oleh cluster JUMPA Chat
    const pubClient = new Redis(process.env.REDIS_URL || 'redis://127.0.0.1:6379');
    
    const payload = JSON.stringify({
      action: 'force_logout',
      email: email,
      timestamp: new Date().toISOString()
    });

    await pubClient.publish('admin_commands', payload);
    pubClient.disconnect();

    return NextResponse.json({ success: true, message: 'Kill-Switch event published' });

  } catch (error) {
    const message = error instanceof Error ? error.message : "Kill-Switch Error";
    console.error('Kill-Switch Error:', error);
    return NextResponse.json({ success: false, error: message }, { status: 500 });
  }
}
