import { NextResponse } from 'next/server';
import { getTorrentInfo } from '@/lib/qbittorrent';
import { QbittorrentStatusResponse } from '@/types';

export async function GET(request: Request) {
    const { searchParams } = new URL(request.url);
    const hash = searchParams.get('hash');

    if (!hash) {
        return NextResponse.json({ error: 'Hash is required' }, { status: 400 });
    }

    try {
        const info = await getTorrentInfo(hash);
        if (!info) {
            return NextResponse.json({ error: 'Torrent not found' }, { status: 404 });
        }
        const response: QbittorrentStatusResponse = { success: true, info };
        return NextResponse.json(response);
    } catch (error) {
        console.error('Error getting torrent status:', error);
        const errorMessage = error instanceof Error ? error.message : String(error);
        return NextResponse.json({
            error: 'Failed to get status',
            details: errorMessage
        }, { status: 500 });
    }
}
