<?php
// app/Models/Supplier.php

namespace App\Models;

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

class Supplier extends Model
{
    use HasFactory;

    protected $fillable = [
        'name', 'email', 'phone', 'address', 'company', 'tax_number', 
        'contact_person', 'contact_phone', 'notes', 'is_active'
    ];

    protected $casts = [
        'is_active' => 'boolean',
    ];

    public function purchases()
    {
        return $this->hasMany(Purchase::class);
    }

    public function getTotalPurchasesAttribute()
    {
        return $this->purchases()->sum('total');
    }

    public function getPurchaseCountAttribute()
    {
        return $this->purchases()->count();
    }

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