<?php

namespace App;

use App\Helpers\ImageHelper;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class Page extends Model
{
    // Fields that mass assignment
    protected $fillable = ['image', 'status'];

    // Use soft delete
    use SoftDeletes;

    /**
     * Get description for pages
     * @return HasMany
     */
    public function descriptions() :HasMany
    {
        return $this->hasMany(PageDescription::class);
    }

//    public function setImageAttribute($image)
//    {
//        if (request('image')) {
//            if (app()->environment() !== 'testing'){
////                if (File::exists(public_path().$this->image)){
////                    File::delete(public_path().$this->image);
////                }
////                ImageHelper::resize(Input::file('image'));
////                $image = ImageHelper::upload(Input::file('image'));
////                $this->attributes['image'] = $image;
//            } else {
//                Storage::fake('local');
//                $fileName = 'avatar.png';
//                $icon = UploadedFile::fake()->image($fileName, '100', '100');
//                $image = time().'_'.Str::random(10).'.'.$icon->getClientOriginalExtension();
//                $path = $icon->storeAs('/pages', $image);
//                $this->attributes['image'] = $path;
//            }
//        }
//    }
}
