<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class MaterialPrice extends Model
{
    use HasFactory;

    protected $fillable = [
        'material_id',
        'member_type',
        'sisi',
        'min_qty',
        'max_qty',
        'price',
    ];

    protected function casts(): array
    {
        return [
            'sisi' => 'integer',
            'min_qty' => 'integer',
            'max_qty' => 'integer',
            'price' => 'decimal:2',
        ];
    }

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

    /**
     * Check if a given qty falls within this price rule's range.
     */
    public function matchesQty(int $qty): bool
    {
        if ($qty < $this->min_qty) {
            return false;
        }

        if ($this->max_qty !== null && $qty > $this->max_qty) {
            return false;
        }

        return true;
    }
}
