<?php

namespace App\Models;

use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\app\Models\Traits\SpatieTranslatable\HasTranslations;
use Ericli1018\AwesomeFieldsForBackpack\Models\Traits\HasUploadImgFields;

class HomeCarousel extends Model
{
    use CrudTrait;
    use HasFactory;
    use HasTranslations;
    use HasUploadImgFields;

    /*
    |--------------------------------------------------------------------------
    | GLOBAL VARIABLES
    |--------------------------------------------------------------------------
    */

    protected $table = 'home_carousel';
    // protected $primaryKey = 'id';
    // public $timestamps = false;
    protected $guarded = ['id'];
    // protected $fillable = [];
    // protected $hidden = [];
    public $translatable = ['title','body'];
    protected $casts = [
        'photos' => 'array',
        'featured_photos' => 'array',
    ];

    /*
    |--------------------------------------------------------------------------
    | FUNCTIONS
    |--------------------------------------------------------------------------
    */
    public static function boot()
    {
        parent::boot();
        static::creating(function ($model) {
            $limit = 6;               
            $actualCount = HomeCarousel::query()->count();

            if ($actualCount >= $limit) {
                return false;
            }
            
        });
        static::deleting(function($obj) {
            if (count((array)$obj->photos)) {
                foreach ($obj->photos as $item) {
                    \Storage::disk('public')->delete($item['file_path']);
                }
            }
            if (count((array)$obj->featured_photos)) {
                foreach ($obj->featured_photos as $item) {
                    \Storage::disk('public')->delete($item['file_path']);
                }
            }
        });

    }
    /*
    |--------------------------------------------------------------------------
    | RELATIONS
    |--------------------------------------------------------------------------
    */
    public function parent()
    {
        return $this->belongsTo(HomeCarousel::class, 'parent_id');
    }

    public function children()
    {
        return $this->hasMany(HomeCarousel::class, 'parent_id');
    }
    /*
    |--------------------------------------------------------------------------
    | SCOPES
    |--------------------------------------------------------------------------
    */

    /*
    |--------------------------------------------------------------------------
    | ACCESSORS
    |--------------------------------------------------------------------------
    */

    /*
    |--------------------------------------------------------------------------
    | MUTATORS
    |--------------------------------------------------------------------------
    */
    public function setPhotosAttribute($value)
    {
        $attribute_name = "photos";
        $disk = "public";
        $destination_path = "home_carousel";

        $this->uploadImgMultipleFilesToDisk($value, $attribute_name, $disk, $destination_path);
    }
    public function setFeaturedPhotosAttribute($value)
    {
        $attribute_name = "featured_photos";
        $disk = "public";
        $destination_path = "home_carousel";

        $this->uploadImgMultipleFilesToDisk($value, $attribute_name, $disk, $destination_path);
    }
}
