<?php

namespace Modules\CRMCore\App\Models;

use App\Models\User;
use App\Traits\UserActionsTrait;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use OwenIt\Auditing\Auditable as AuditableTrait;
use OwenIt\Auditing\Contracts\Auditable as AuditableContract;

/**
 * @property-read Collection<int, DealStage> $stages
 * @property-read Collection<int, Deal> $deals
 * @property-read User|null $createdBy
 * @property-read User|null $updatedBy
 */
class DealPipeline extends Model implements AuditableContract
{
    use AuditableTrait, UserActionsTrait;

    protected $table = 'deal_pipelines';

    protected $fillable = [
        'name',
        'description',
        'is_default',
        'is_active',
        'position',
        'tenant_id',
        'created_by_id',
        'updated_by_id',
    ];

    protected $casts = [
        'is_default' => 'boolean',
        'is_active' => 'boolean',
        'position' => 'integer',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    /**
     * Get the stages associated with this pipeline, ordered by position.
     */
    public function stages(): HasMany
    {
        return $this->hasMany(DealStage::class, 'pipeline_id')->orderBy('position');
    }

    /**
     * Get all deals that belong to this pipeline.
     */
    public function deals(): HasMany
    {
        return $this->hasMany(Deal::class, 'pipeline_id');
    }

    // createdBy() and updatedBy() relationships handled by UserActionsTrait
}
