<?php

namespace App\Models;

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

class ProductShowcase extends Model
{
    use HasFactory;

    protected $fillable = [
        'title',
        'subtitle',
        'display_type',
        'products_limit',
        'theme_color',
        'sort_order',
        'is_active',
    ];

    protected $casts = [
        'is_active' => 'boolean',
    ];

    /**
     * Get showcase items
     */
    public function items()
    {
        return $this->hasMany(ProductShowcaseItem::class, 'showcase_id')->orderBy('sort_order');
    }

    /**
     * Get products
     */
    public function products()
    {
        return $this->hasManyThrough(
            Product::class,
            ProductShowcaseItem::class,
            'showcase_id',
            'id',
            'id',
            'product_id'
        )->orderBy('product_showcase_items.sort_order');
    }

    /**
     * Scope: Active showcases
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * Scope: Ordered by sort_order
     */
    public function scopeOrdered($query)
    {
        return $query->orderBy('sort_order');
    }
}
