<?php
namespace App\Http\Controllers\Api\V1;
use App\Models\{AddressCollection,Asset,TreasuryWallet,WalletAddress};
use App\Support\ApiResponse;
use Illuminate\Http\Request;
final class TreasuryDashboardController {
 public function __invoke(Request $request){
  $environment=(string)$request->input('environment',config('gateway.environment'));
  return ApiResponse::success([
   'environment'=>$environment,
   'assets'=>Asset::where('enabled',true)->get(['id','code','name','network','provider_currency','deposit_enabled','payout_enabled','provider_synced_at']),
   'addresses'=>[
    'active'=>WalletAddress::where('environment',$environment)->where('status','active')->count(),
    'disabled'=>WalletAddress::where('environment',$environment)->where('status','disabled')->count(),
    'provider_balance_atomic'=>WalletAddress::where('environment',$environment)->sum('provider_balance_atomic'),
   ],
   'collections'=>[
    'pending'=>AddressCollection::where('environment',$environment)->whereIn('status',['requesting','submitted'])->count(),
    'failed'=>AddressCollection::where('environment',$environment)->where('status','failed')->count(),
    'recent'=>AddressCollection::with(['address','asset'])->where('environment',$environment)->latest()->limit(25)->get(),
   ],
   'treasury_wallets'=>TreasuryWallet::where('environment',$environment)->get(),
  ]);
 }
}
