<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Models\Offerwall;
use App\Support\OfferCurator;
use App\Support\ProviderOfferImporter;

class ProvidersController extends Controller
{
    public function test(Offerwall $offerwall, ProviderOfferImporter $importer)
    {
        return response()->json(['data' => $importer->testConnection($offerwall->sdk_data ?: [])]);
    }

    public function sync(Offerwall $offerwall, ProviderOfferImporter $importer, OfferCurator $curator)
    {
        $result = $importer->sync($offerwall);

        if ($result['success'] ?? false) {
            $curator->curate($offerwall->id);
        }

        return response()->json(['data' => $result], ($result['success'] ?? false) ? 200 : 422);
    }
}
