<?php

namespace App\Models;

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

class Service extends Model
{
    use HasFactory;

    protected $fillable = [
        'title',
        'slug',
        'icon',
        'icon_image',
        'short_description',
        'description',
        'full_content',
        'cover_image',
        'features',
        'sort_order',
        'is_active',
        'show_on_home',
        'meta_title',
        'meta_description',
    ];

    protected function casts(): array
    {
        return [
            'features'      => 'array',
            'is_active'     => 'boolean',
            'show_on_home'  => 'boolean',
            'sort_order'    => 'integer',
        ];
    }

    protected static function booted(): void
    {
        static::creating(function (Service $service) {
            if (empty($service->slug)) {
                $service->slug = Str::slug($service->title);
            }
        });
    }

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function scopeForHome($query)
    {
        return $query->where('is_active', true)->where('show_on_home', true)
            ->orderBy('sort_order');
    }

    public function orders()
    {
        return $this->hasMany(Order::class);
    }

    public function iconImageUrl(): ?string
    {
        return $this->icon_image ? asset('storage/' . $this->icon_image) : null;
    }

    public function coverImageUrl(): ?string
    {
        return $this->cover_image ? asset('storage/' . $this->cover_image) : null;
    }
}
