<?php

namespace App\Domains\Leads\Models;

use App\Domains\Contacts\Models\Contact;
use App\Domains\Conversations\Models\Conversation;
use App\Domains\Tenancy\Models\Branch;
use App\Domains\Tenancy\Models\Tenant;
use App\Models\User;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class Lead extends Model
{
    use HasFactory;
    use HasUuids;
    use SoftDeletes;

    protected $fillable = [
        'tenant_id',
        'branch_id',
        'contact_id',
        'conversation_id',
        'pipeline_id',
        'stage_id',
        'title',
        'assigned_user_id',
        'source_platform',
        'source_type',
        'estimated_value',
        'status',
        'lost_reason_id',
        'won_at',
        'lost_at',
    ];

    protected $casts = [
        'estimated_value' => 'decimal:2',
        'won_at' => 'datetime',
        'lost_at' => 'datetime',
    ];

    public function tenant(): BelongsTo
    {
        return $this->belongsTo(Tenant::class);
    }

    public function branch(): BelongsTo
    {
        return $this->belongsTo(Branch::class);
    }

    public function contact(): BelongsTo
    {
        return $this->belongsTo(Contact::class);
    }

    public function conversation(): BelongsTo
    {
        return $this->belongsTo(Conversation::class);
    }

    public function pipeline(): BelongsTo
    {
        return $this->belongsTo(Pipeline::class);
    }

    public function stage(): BelongsTo
    {
        return $this->belongsTo(PipelineStage::class, 'stage_id');
    }

    public function assignee(): BelongsTo
    {
        return $this->belongsTo(User::class, 'assigned_user_id');
    }

    public function lostReason(): BelongsTo
    {
        return $this->belongsTo(LostReason::class);
    }
}
