<?php

namespace Modules\CRMCore\App\Models;

use App\Models\User;
use App\Traits\SafeHasTasks;
use App\Traits\UserActionsTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Modules\SystemCore\App\Models\CoreCustomer;
use OwenIt\Auditing\Auditable as AuditableTrait;
use OwenIt\Auditing\Contracts\Auditable as AuditableContract;

/**
 * @property-read DealPipeline|null $pipeline
 * @property-read DealStage|null $dealStage
 * @property-read DealStage|null $stage
 * @property-read Company|null $company
 * @property-read CoreCustomer|null $customer
 * @property-read User|null $assignedToUser
 * @property-read User|null $assignedTo
 * @property-read Lead|null $lead
 * @property-read User|null $createdBy
 * @property-read User|null $updatedBy
 */
class Deal extends Model implements AuditableContract
{
    use AuditableTrait, HasFactory, SafeHasTasks, SoftDeletes, UserActionsTrait;

    protected $table = 'deals';

    protected $fillable = [
        'code',
        'title',
        'description',
        'value',
        'currency',
        'expected_close_date',
        'actual_close_date',
        'probability',
        'pipeline_id',
        'deal_stage_id',
        'company_id',
        'customer_id', // Changed from contact_id to customer_id (CoreCustomer)
        'assigned_to_user_id',
        'lost_reason',
        'won_at',
        'lost_at',
        'closed_at',
        'tenant_id',
        'created_by_id',
        'updated_by_id',
    ];

    protected $casts = [
        'value' => 'decimal:2',
        'expected_close_date' => 'date',
        'actual_close_date' => 'date',
        'probability' => 'integer',
        'pipeline_id' => 'integer',
        'deal_stage_id' => 'integer',
        'company_id' => 'integer',
        'customer_id' => 'integer',
        'assigned_to_user_id' => 'integer',
        'won_at' => 'datetime',
        'lost_at' => 'datetime',
        'closed_at' => 'datetime',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
    ];

    /**
     * Get the pipeline this deal belongs to.
     */
    public function pipeline(): BelongsTo
    {
        return $this->belongsTo(DealPipeline::class, 'pipeline_id');
    }

    /**
     * Get the current stage of the deal.
     */
    public function dealStage(): BelongsTo
    {
        return $this->belongsTo(DealStage::class);
    }

    /**
     * Alias for dealStage relationship
     */
    public function stage(): BelongsTo
    {
        return $this->belongsTo(DealStage::class, 'deal_stage_id');
    }

    /**
     * Get the company associated with this deal.
     */
    public function company(): BelongsTo
    {
        return $this->belongsTo(Company::class);
    }

    /**
     * Get the customer for this deal (CoreCustomer from SystemCore).
     */
    public function customer(): BelongsTo
    {
        return $this->belongsTo(CoreCustomer::class);
    }

    /**
     * Get the user this deal is assigned to.
     */
    public function assignedToUser(): BelongsTo
    {
        return $this->belongsTo(User::class, 'assigned_to_user_id');
    }

    /**
     * Alias for assignedToUser relationship
     */
    public function assignedTo(): BelongsTo
    {
        return $this->belongsTo(User::class, 'assigned_to_user_id');
    }

    /**
     * Get the lead this deal was converted from.
     */
    public function lead(): HasOne
    {
        return $this->hasOne(Lead::class, 'converted_to_deal_id');
    }

    protected static function newFactory()
    {
        return \Modules\CRMCore\Database\Factories\DealFactory::new();
    }

    /**
     * Get the user who created this deal.
     */
    public function createdBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by_id');
    }

    /**
     * Get the user who last updated this deal.
     */
    public function updatedBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'updated_by_id');
    }
}
