<?php

namespace App\Models;

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

class Supplier extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'company_name',
        'email',
        'phone',
        'website',
        'address',
        'city',
        'state',
        'zip_code',
        'country',
        'contact_person',
        'payment_terms',
        'account_number',
        'tax_id',
        'notes',
        'is_active',
        'rating',
    ];

    protected $casts = [
        'is_active' => 'boolean',
        'rating' => 'decimal:1',
    ];

    public function parts(): HasMany
    {
        return $this->hasMany(Part::class);
    }

    public function purchaseOrders(): HasMany
    {
        return $this->hasMany(PurchaseOrder::class);
    }

    public function stockMovements(): HasMany
    {
        return $this->hasMany(StockMovement::class);
    }

    public function getFullNameAttribute(): string
    {
        return $this->company_name ?: $this->name;
    }

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }
}
