<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class FeedPurchaseItem extends Model
{
    use SoftDeletes;

    protected $guarded = ['id'];

    protected function casts(): array
    {
        return [
            'quantity' => 'integer',
            'unit_price' => 'integer',
            'subtotal' => 'integer',
        ];
    }

    protected function formattedUnitPrice(): Attribute
    {
        return Attribute::make(
            get: fn () => number_format($this->unit_price, 0, ',', '.'),
        );
    }

    protected function formattedSubtotal(): Attribute
    {
        return Attribute::make(
            get: fn () => number_format($this->subtotal, 0, ',', '.'),
        );
    }

    public function purchase(): BelongsTo
    {
        return $this->belongsTo(FeedPurchase::class, 'feed_purchase_id');
    }

    public function feed(): BelongsTo
    {
        return $this->belongsTo(Feed::class);
    }
}
