<?php

namespace App\Models\Tenant;

use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class Deal extends Model
{
    use BelongsToTenant, SoftDeletes;

    public const STAGES = [
        'new' => 'Nou',
        'contact' => 'Contact',
        'agree' => 'Aprobare',
        'scheduled' => 'Programat',
        'arrived' => 'Sosit',
        'in_work' => 'În lucru',
        'done' => 'Finalizat',
        'lost' => 'Pierdut',
    ];

    protected $fillable = [
        'company_id', 'client_id', 'vehicle_id',
        'name', 'price', 'stage', 'source', 'note',
        'assigned_to', 'won_at', 'lost_at', 'lost_reason',
    ];

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

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

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

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

    public function isOpen(): bool
    {
        return ! in_array($this->stage, ['done', 'lost'], true);
    }
}
