<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;

class Offer extends Model
{
    use HasFactory;

    protected $fillable = [
        'uuid',
        'title',
        'slug',
        'category',
        'excerpt',
        'description',
        'location_label',
        'lat',
        'lng',
        'price_from',
        'duration_minutes',
        'image_url',
        'status',
        'is_featured',
        'priority_score',
        'available_now',
    ];

    protected $casts = [
        'lat' => 'decimal:7',
        'lng' => 'decimal:7',
        'price_from' => 'decimal:2',
        'is_featured' => 'boolean',
        'available_now' => 'boolean',
    ];

    protected static function booted(): void
    {
        static::creating(function (Offer $offer) {
            if (blank($offer->uuid)) {
                $offer->uuid = (string) Str::uuid();
            }

            if (blank($offer->slug)) {
                $offer->slug = Str::slug($offer->title);
            }
        });
    }

    public function recommendations(): HasMany
    {
        return $this->hasMany(RideRecommendation::class);
    }

    public function bookings(): HasMany
    {
        return $this->hasMany(Booking::class);
    }

    public function events(): HasMany
    {
        return $this->hasMany(Event::class);
    }
}
