import { NextRequest, NextResponse } from 'next/server';
import { getA2PRegistration } from '@/lib/db';

export async function GET(
  _request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    const registration = getA2PRegistration(params.id);

    if (!registration) {
      return NextResponse.json(
        { error: 'Registration not found' },
        { status: 404 }
      );
    }

    const sidChain = JSON.parse(registration.sid_chain_json || '{}');

    return NextResponse.json({
      id: registration.id,
      businessName: registration.business_name,
      status: registration.status,
      sidChain,
      brandTrustScore: registration.brand_trust_score,
      failureReason: registration.failure_reason,
      attemptCount: registration.attempt_count,
      maxAttempts: registration.max_attempts,
      timestamps: {
        created: registration.created_at,
        updated: registration.updated_at,
        brandSubmitted: registration.brand_submitted_at,
        brandResolved: registration.brand_resolved_at,
        campaignSubmitted: registration.campaign_submitted_at,
        campaignResolved: registration.campaign_resolved_at,
        completed: registration.completed_at,
      },
    });
  } catch (error) {
    console.error(`GET /api/a2p/${params.id}/status error:`, error);
    return NextResponse.json(
      { error: 'Failed to fetch registration status' },
      { status: 500 }
    );
  }
}
