<?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 Product extends Model
{
    use CrudTrait;
    use HasFactory;
    use HasTranslations;
    use HasUploadImgFields;

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

    protected $table = 'products';
    // protected $primaryKey = 'id';
    // public $timestamps = false;
    protected $guarded = ['id'];
    // protected $fillable = [];
    // protected $hidden = [];
    public $translatable = ['title', 'description', 'body', 'feature_overview', 'feature_spec', 'seo_keyword', 'seo_description'];
    protected $casts = [
        'photos' => 'array', 
    ];

    /*
    |--------------------------------------------------------------------------
    | FUNCTIONS
    |--------------------------------------------------------------------------
    */
    public static function boot()
    {
        parent::boot();
        static::deleting(function($obj) {
            if (count((array)$obj->photos)) {
                foreach ($obj->photos as $item) {
                    \Storage::disk('public')->delete($item['file_path']);
                }
            }
        });
    }
    /*
    |--------------------------------------------------------------------------
    | RELATIONS
    |--------------------------------------------------------------------------
    */
    public function productRelateds()
    {
        return $this->belongsToMany(Product::class, 'product_product_related', 'product_id', 'product_related_id');
    }

    public function productCatalog()
    {
        return $this->belongsTo(ProductCatalog::class, 'product_catalog_id');
    }

    public function productApplications()
    {
        return $this->belongsToMany(ProductApplication::class, 'product_product_application');
    }
    /*
    |--------------------------------------------------------------------------
    | SCOPES
    |--------------------------------------------------------------------------
    */

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

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

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