<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasMany;

class GalleryType extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'gallery_types';

    protected $fillable = [
        'name',
        'description',
        'color',
        'icon',
        'is_active',
    ];

    protected $casts = [
        'is_active'  => 'boolean',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
    ];

    protected $attributes = [
        'color'     => '#3b82f6',
        'icon'      => 'fa-images',
        'is_active' => true,
    ];

    public function galleries(): HasMany
    {
        return $this->hasMany(Gallery::class, 'gallery_type_id');
    }

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

    public function scopeSearch($query, string $search)
    {
        return $query->where('name', 'like', "%{$search}%")
                     ->orWhere('description', 'like', "%{$search}%");
    }

    public function scopeOrdered($query)
    {
        return $query->orderBy('name');
    }

    public function getGalleryCount(): int
    {
        return $this->galleries()->count();
    }
}
