<?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\HasOneThrough;
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 LeadStatus|null $leadStatus
 * @property-read LeadStatus|null $status
 * @property-read LeadSource|null $leadSource
 * @property-read LeadSource|null $source
 * @property-read User|null $assignedToUser
 * @property-read User|null $assignedTo
 * @property-read CoreCustomer|null $convertedToCustomer
 * @property-read CoreCustomer|null $customer
 * @property-read Deal|null $convertedToDeal
 * @property-read Company|null $company
 * @property-read User|null $createdBy
 * @property-read User|null $updatedBy
 */
class Lead extends Model implements AuditableContract
{
    use AuditableTrait, HasFactory, SafeHasTasks, SoftDeletes, UserActionsTrait;

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

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'code',
        'title',
        'description',
        'contact_name',
        'contact_email',
        'contact_phone',
        'company_name',
        'value',
        'lead_source_id',
        'lead_status_id',
        'assigned_to_user_id',
        'converted_at',
        'converted_to_customer_id', // Changed from contact_id to customer_id (CoreCustomer)
        'converted_to_deal_id',
        'tenant_id',
        'created_by_id',
        'updated_by_id',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'value' => 'decimal:2',
        'converted_at' => 'datetime',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
    ];

    /**
     * Get the status of the lead.
     */
    public function leadStatus(): BelongsTo
    {
        return $this->belongsTo(LeadStatus::class);
    }

    /**
     * Alias for leadStatus relationship
     */
    public function status(): BelongsTo
    {
        return $this->belongsTo(LeadStatus::class, 'lead_status_id');
    }

    /**
     * Get the source of the lead.
     */
    public function leadSource(): BelongsTo
    {
        return $this->belongsTo(LeadSource::class);
    }

    /**
     * Alias for leadSource relationship
     */
    public function source(): BelongsTo
    {
        return $this->belongsTo(LeadSource::class, 'lead_source_id');
    }

    /**
     * Get the user this lead 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 customer this lead was converted into (CoreCustomer from SystemCore).
     */
    public function convertedToCustomer(): BelongsTo
    {
        return $this->belongsTo(CoreCustomer::class, 'converted_to_customer_id');
    }

    /**
     * Alias for convertedToCustomer relationship
     */
    public function customer(): BelongsTo
    {
        return $this->belongsTo(CoreCustomer::class, 'converted_to_customer_id');
    }

    /**
     * Get the deal this lead was converted into.
     */
    public function convertedToDeal(): BelongsTo
    {
        return $this->belongsTo(Deal::class, 'converted_to_deal_id');
    }

    /**
     * Get the company associated with this lead (through customer)
     */
    public function company(): HasOneThrough
    {
        return $this->hasOneThrough(Company::class, CoreCustomer::class, 'id', 'id', 'converted_to_customer_id', 'company_id');
    }

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

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

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