<?php

namespace Modules\PurchaseRequisition\Entities;

use Illuminate\Database\Eloquent\Model;
use App\Transaction;
use Modules\PurchaseRequisition\Entities\PrPurchaseChange;

class PrPurchaseShare extends Model
{
    protected $table = 'pr_purchase_shares';

    protected $fillable = [
        'business_id',
        'transaction_id',
        'pr_request_ids',
        'share_token',
        'supplier_order_note',
        'row_notes',
        'images',
    ];

    protected $casts = [
        'pr_request_ids' => 'array',
        'row_notes'      => 'array',
        'images'         => 'array',
    ];

    public function transaction()
    {
        return $this->belongsTo(Transaction::class);
    }

    public function prRequests()
    {
        return PrRequest::whereIn('id', $this->pr_request_ids ?? [])->get();
    }

    public function changes()
    {
        return $this->hasMany(PrPurchaseChange::class, 'pr_purchase_share_id');
    }

    public function payments()
    {
        return $this->hasMany(PrPurchasePayment::class, 'pr_purchase_share_id')->orderBy('created_at');
    }
}
