<?php

namespace App\Jobs;

use App\Application\Services\Treasury\TreasuryCollectionService;
use App\Models\Asset;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

final class AutoCollectTreasuryDepositsJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function __construct(public string $environment)
    {
        $this->onQueue('payouts');
    }

    public function handle(TreasuryCollectionService $collections): void
    {
        $collections->syncSubmittedCollections($this->environment);

        if (! (bool) config('gateway.treasury.auto_collection_enabled', false)) {
            return;
        }

        Asset::where('enabled', true)
            ->where('deposit_enabled', true)
            ->where('payout_enabled', true)
            ->whereNotNull('contract_address')
            ->each(fn (Asset $asset) => $collections->collectAsset($asset, $this->environment, (int) config('gateway.treasury.collection_batch_limit', 25)));
    }
}

