import { NextResponse } from 'next/server';
import { requireAdminAuth } from '@/lib/auth';
import {
    getRandomActiveStatement,
    getAllStatements,
    createStatement,
    updateStatement,
    deleteStatement,
} from '@/lib/politicalStatements';

export async function GET(request: Request) {
    try {
        const { searchParams } = new URL(request.url);
        const locale = searchParams.get('locale') || 'en';
        const admin = searchParams.get('admin') === 'true';

        if (admin) {
            const authError = await requireAdminAuth(request as any);
            if (authError) {
                return authError;
            }
            const statements = await getAllStatements(locale);
            return NextResponse.json(statements);
        }

        const statement = await getRandomActiveStatement(locale);
        return NextResponse.json(statement);
    } catch (error) {
        console.error('[political-statements] GET failed:', error);
        return NextResponse.json({ error: 'Failed to load political statements' }, { status: 500 });
    }
}

export async function POST(request: Request) {
    const authError = await requireAdminAuth(request as any);
    if (authError) {
        return authError;
    }

    try {
        const body = await request.json();
        const { locale, text, active = true, source } = body;

        if (!locale || typeof text !== 'string' || !text.trim()) {
            return NextResponse.json({ error: 'locale and text are required' }, { status: 400 });
        }

        const created = await createStatement(locale, { text: text.trim(), active, source });
        return NextResponse.json(created, { status: 201 });
    } catch (error) {
        console.error('[political-statements] POST failed:', error);
        return NextResponse.json({ error: 'Failed to create statement' }, { status: 500 });
    }
}

export async function PUT(request: Request) {
    const authError = await requireAdminAuth(request as any);
    if (authError) {
        return authError;
    }

    try {
        const body = await request.json();
        const { locale, id, text, active, source } = body;

        if (!locale || typeof id !== 'number') {
            return NextResponse.json({ error: 'locale and numeric id are required' }, { status: 400 });
        }

        const updated = await updateStatement(locale, id, {
            text: typeof text === 'string' ? text.trim() : undefined,
            active,
            source,
        });

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

        return NextResponse.json(updated);
    } catch (error) {
        console.error('[political-statements] PUT failed:', error);
        return NextResponse.json({ error: 'Failed to update statement' }, { status: 500 });
    }
}

export async function DELETE(request: Request) {
    const authError = await requireAdminAuth(request as any);
    if (authError) {
        return authError;
    }

    try {
        const body = await request.json();
        const { locale, id } = body;

        if (!locale || typeof id !== 'number') {
            return NextResponse.json({ error: 'locale and numeric id are required' }, { status: 400 });
        }

        const ok = await deleteStatement(locale, id);
        if (!ok) {
            return NextResponse.json({ error: 'Statement not found' }, { status: 404 });
        }

        return NextResponse.json({ success: true });
    } catch (error) {
        console.error('[political-statements] DELETE failed:', error);
        return NextResponse.json({ error: 'Failed to delete statement' }, { status: 500 });
    }
}


