<?php
namespace App\Http\Controllers\Api\V1;
use App\Application\Services\QuoteService;
use App\Http\Controllers\Controller;
use App\Models\Asset;
use App\Support\ApiResponse;
use Illuminate\Http\Request;
use RuntimeException;
final class EstimateController extends Controller { public function __invoke(Request $r,QuoteService $quotes){$d=$r->validate(['amount'=>['required','string','regex:/^(?:0|[1-9]\d*)(?:\.\d{1,8})?$/'],'currency_from'=>['required','string','in:USD'],'currency_to'=>['required','string','max:20']]);$a=Asset::where('code',strtoupper($d['currency_to']))->where('enabled',true)->first();if(!$a)return ApiResponse::error('UNSUPPORTED_CURRENCY','The requested asset or network is not enabled.',422);try{$q=$quotes->quote($d['amount'],$d['currency_from'],$a);}catch(RuntimeException){return ApiResponse::error('PROVIDER_UNAVAILABLE','The rate provider is temporarily unavailable.',503);}return ApiResponse::success(['amount_from'=>$d['amount'],'currency_from'=>'USD','estimated_amount'=>$q['amount'],'currency_to'=>$a->code,'rate'=>$q['rate'],'rate_expires_at'=>$q['expires_at']->utc()->toIso8601String()]);} }
