<?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;

/**
 * GalleryType Model
 *
 * Represents categories/types of galleries (e.g., "Events", "Team", "Achievements").
 * Provides organization and filtering for gallery displays.
 *
 * @property int $id
 * @property string $name
 * @property string|null $description
 * @property string $color
 * @property string $icon
 * @property bool $is_active
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 */
class GalleryType extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'gallery_types';

    // =========================================================================
    // ATTRIBUTES & CASTING
    // =========================================================================

    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,
    ];

    // =========================================================================
    // RELATIONSHIPS
    // =========================================================================

    /**
     * Get all galleries of this type.
     */
    public function galleries(): HasMany
    {
        return $this->hasMany(Gallery::class, 'gallery_type_id');
    }

    // =========================================================================
    // QUERY SCOPES
    // =========================================================================

    /**
     * Scope to filter only active gallery types.
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * Scope to search by name or description.
     */
    public function scopeSearch($query, string $search)
    {
        return $query->where('name', 'like', "%{$search}%")
                     ->orWhere('description', 'like', "%{$search}%");
    }

    /**
     * Scope to order by name.
     */
    public function scopeOrdered($query)
    {
        return $query->orderBy('name');
    }

    // =========================================================================
    // HELPER METHODS
    // =========================================================================

    /**
     * Get the count of galleries for this type.
     */
    public function getGalleryCount(): int
    {
        return $this->galleries()->count();
    }
}
