<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Traits\Auditable;

class Supplier extends Model
{
    use HasFactory, SoftDeletes;
    use Auditable;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'suppliers';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'company_name',
        'tax_id',
        'email',
        'phone',
        'address',
        'city',
        'state',
        'postal_code',
        'country',
        'website',
        'contact_person',
        'contact_phone',
        'contact_email',
        'notes',
        'is_active'
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'is_active' => 'boolean',
    ];

    /**
     * Get all inventory items from this supplier.
     */
    public function inventoryItems()
    {
        return $this->hasMany(Inventory::class, 'supplier_id');
    }

    /**
     * Get all active suppliers.
     */
    public static function getActive()
    {
        return self::where('is_active', true)->get();
    }

    /**
     * Format the phone number for display.
     */
    public function getFormattedPhoneAttribute()
    {
        // Simple formatting - adjust as needed
        if (empty($this->phone)) {
            return '';
        }
        
        return $this->phone;
    }

    /**
     * Get the full address.
     */
    public function getFullAddressAttribute()
    {
        $parts = array_filter([
            $this->address,
            $this->city,
            $this->state,
            $this->postal_code,
            $this->country
        ]);
        
        return implode(', ', $parts);
    }
} 