<?php

namespace App\Providers;

use App\Contracts\CatalogProductRanker;
use App\Contracts\KeywordResearchService;
use App\Contracts\ListingScraper;
use App\Services\AI\AiCatalogProductRanker;
use App\Services\Keywords\GoogleAdsClientFactory;
use App\Services\Keywords\GoogleAdsKeywordResearchService;
use App\Services\Keywords\NullKeywordResearchService;
use App\Services\Scraping\NullListingScraper;
use App\Services\Scraping\PuppeteerListingScraper;
use Illuminate\Support\ServiceProvider;

class ScrapingServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->bind(ListingScraper::class, function (): ListingScraper {
            if (! config('scraping.puppeteer.enabled')) {
                return new NullListingScraper;
            }

            $implementation = config('scraping.listing_scraper');

            return $this->app->make(is_string($implementation) ? $implementation : PuppeteerListingScraper::class);
        });

        $this->app->bind(KeywordResearchService::class, function ($app): KeywordResearchService {
            if (! config('google-ads.enabled')) {
                return new NullKeywordResearchService;
            }

            $factory = $app->make(GoogleAdsClientFactory::class);

            if ($factory->missingConfigurationKeys() !== []) {
                return new NullKeywordResearchService;
            }

            return $app->make(GoogleAdsKeywordResearchService::class);
        });

        $this->app->bind(CatalogProductRanker::class, AiCatalogProductRanker::class);
    }
}
