<?php

namespace Modules\SystemCore\App\Models;

use App\Traits\UserActionsTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class CorePaymentTerm extends Model
{
    use SoftDeletes, UserActionsTrait;

    protected $table = 'core_payment_terms';

    protected $fillable = [
        'name',
        'code',
        'days',
        'description',
        'is_active',
        'created_by_id',
        'updated_by_id',
    ];

    protected function casts(): array
    {
        return [
            'days' => 'integer',
            'is_active' => 'boolean',
        ];
    }

    /**
     * Get customers using this payment term
     */
    public function customers(): HasMany
    {
        return $this->hasMany(CoreCustomer::class, 'payment_term_id');
    }

    /**
     * Get suppliers using this payment term
     */
    public function suppliers(): HasMany
    {
        return $this->hasMany(CoreSupplier::class, 'payment_term_id');
    }

    /**
     * Scope for active terms
     */
    public function scopeActive(Builder $query): Builder
    {
        return $query->where('is_active', true);
    }
}
