<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Lead extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'phone',
        'email',
        'interested_vehicle',
        'vehicle_id',
        'auction_vehicle_id',
        'budget_bdt',
        'source',
        'message',
        'assigned_to',
        'status',
        'sales_notes',
        'last_contacted_at',
    ];

    protected function casts(): array
    {
        return [
            'budget_bdt' => 'decimal:2',
            'last_contacted_at' => 'datetime',
        ];
    }

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

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

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

    public function getStatusBadgeClassAttribute(): string
    {
        return match ($this->status) {
            'new' => 'bg-blue-100 text-blue-800 border-blue-200',
            'contacted' => 'bg-amber-100 text-amber-800 border-amber-200',
            'interested' => 'bg-purple-100 text-purple-800 border-purple-200',
            'follow_up' => 'bg-indigo-100 text-indigo-800 border-indigo-200',
            'converted' => 'bg-emerald-100 text-emerald-800 border-emerald-200',
            'lost' => 'bg-rose-100 text-rose-800 border-rose-200',
            default => 'bg-gray-100 text-gray-700',
        };
    }
}
