<?php
namespace App\Http\Controllers\Api\V1;use App\Http\Controllers\Controller;use App\Http\Requests\CreateTransferRequest;use App\Http\Resources\TransferResource;use App\Models\Transfer;use App\Services\TransferService;use Illuminate\Http\Request;
class TransferController extends Controller {public function store(CreateTransferRequest $r,TransferService $s){$result=$s->create($r->user(),$r->validated(),$r->header('Idempotency-Key'));return response()->json($result['body'],$result['status']);}public function index(Request $r){$q=Transfer::query()->with('corridor');if(!$r->user()->hasRole('admin'))$q->where(fn($x)=>$x->where('send_merchant_id',$r->user()->merchant_id)->orWhere('receive_merchant_id',$r->user()->merchant_id));$q->when($r->status,fn($x,$v)=>$x->where('status',$v))->when($r->date_from,fn($x,$v)=>$x->whereDate('created_at','>=',$v))->when($r->date_to,fn($x,$v)=>$x->whereDate('created_at','<=',$v))->when($r->corridor_id,fn($x,$v)=>$x->where('corridor_id',$v))->when($r->q,fn($x,$v)=>$x->where(fn($y)=>$y->where('reference','like',"%$v%")->orWhere('sender_name','like',"%$v%")->orWhere('recipient_name','like',"%$v%")));return TransferResource::collection($q->latest()->paginate(min($r->integer('per_page',20),100)));}public function show(Request $r,Transfer $transfer){$this->authorize('view',$transfer);return new TransferResource($transfer->load('events'));}public function invoice(Request $r,Transfer $transfer){$this->authorize('view',$transfer);return ['data'=>(new TransferResource($transfer))->resolve($r)];}}
