<?php

namespace Modules\CRMCore\App\Models;

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

/**
 * @property-read Collection<int, CoreCustomer> $customers
 * @property-read CoreCustomer|null $primaryCustomer
 * @property-read Collection<int, CoreContact> $contacts
 * @property-read CoreContact|null $primaryContact
 * @property-read User|null $assignedToUser
 * @property-read User|null $createdBy
 * @property-read User|null $updatedBy
 * @property-read Collection<int, Lead> $leads
 * @property-read Collection<int, Deal> $deals
 */
class Company extends Model implements AuditableContract
{
    use AuditableTrait, HasFactory, SafeHasTasks, SoftDeletes, UserActionsTrait;

    protected $table = 'companies';

    protected $fillable = [
        'code',
        'name',
        'website',
        'phone_office',
        'email_office',
        'address_street',
        'address_city',
        'address_state',
        'address_postal_code',
        'address_country',
        'industry',
        'description',
        'is_active',
        'tenant_id',
        'assigned_to_user_id',
        'created_by_id', // From UserActionsTrait
        'updated_by_id', // From UserActionsTrait
    ];

    protected $casts = [
        'is_active' => 'boolean',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
    ];

    /**
     * Get the customers associated with the company (CoreCustomer from SystemCore).
     */
    public function customers(): HasMany
    {
        return $this->hasMany(CoreCustomer::class);
    }

    /**
     * Get all contacts for this company (polymorphic via CoreContact).
     */
    public function contacts(): MorphMany
    {
        return $this->morphMany(CoreContact::class, 'entity');
    }

    /**
     * Get the primary contact for this company.
     */
    public function primaryContact(): MorphOne
    {
        return $this->morphOne(CoreContact::class, 'entity')->where('is_primary', true);
    }

    /**
     * Get the primary customer for the company.
     */
    public function primaryCustomer(): HasOne
    {
        return $this->hasOne(CoreCustomer::class)->oldest();
    }

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

    /**
     * Get the leads associated with the company.
     */
    public function leads(): HasMany
    {
        return $this->hasMany(Lead::class);
    }

    /**
     * Get the deals associated with the company.
     */
    public function deals(): HasMany
    {
        return $this->hasMany(Deal::class);
    }

    // Set factory path
    protected static function newFactory()
    {
        return \Modules\CRMCore\Database\Factories\CompanyFactory::new();
    }

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

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