<?php
namespace App\Models;
use App\Enums\TransferStatus;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
class Transfer extends Model {
 protected $fillable=['reference','corridor_id','send_merchant_id','send_user_id','receive_merchant_id','receive_user_id','sender_name','sender_phone','sender_id_type','sender_id_number','sender_address','recipient_name','recipient_phone','recipient_country_id','recipient_city','recipient_presented_name','recipient_id_type','recipient_id_number','send_currency','send_amount_minor','fee_minor','total_collected_minor','receive_currency','payout_amount_minor','rate_used','margin_percent_used','exchange_rate_id','status','issued_at','expires_at','redeemed_at','cancelled_at','hold_reason','cancel_reason','settlement_batch_id','idempotency_key'];
 protected function casts():array{return ['send_amount_minor'=>'integer','fee_minor'=>'integer','total_collected_minor'=>'integer','payout_amount_minor'=>'integer','rate_used'=>'decimal:8','margin_percent_used'=>'decimal:3','status'=>TransferStatus::class,'issued_at'=>'datetime','expires_at'=>'datetime','redeemed_at'=>'datetime','cancelled_at'=>'datetime'];}
 protected static function booted():void{static::updating(function(self $m){foreach(['rate_used','margin_percent_used','fee_minor','payout_amount_minor','exchange_rate_id','send_currency','receive_currency','send_amount_minor'] as $f){if($m->isDirty($f)) throw new \LogicException("Immutable transfer field changed: {$f}");}});}
 public function corridor():BelongsTo{return $this->belongsTo(Corridor::class);} public function sendMerchant():BelongsTo{return $this->belongsTo(Merchant::class,'send_merchant_id');} public function receiveMerchant():BelongsTo{return $this->belongsTo(Merchant::class,'receive_merchant_id');} public function sendUser():BelongsTo{return $this->belongsTo(User::class,'send_user_id');} public function receiveUser():BelongsTo{return $this->belongsTo(User::class,'receive_user_id');} public function settlementLegs():HasMany{return $this->hasMany(SettlementLeg::class);} public function code():HasOne{return $this->hasOne(TransferCode::class);} public function events():HasMany{return $this->hasMany(TransferEvent::class)->orderBy('id');}
}
