<?php

namespace App\Traits;

use App\Models\Files;
use App\Models\Image;
use App\Models\ImageSetting;
use Illuminate\Database\Eloquent\Collection;

trait HasFiles
{
    public function file($key = null)
    {
        $result = Files::where('archivable_id', $this->id)
            ->where('archivable_type', $this->getMorphClass())
            ->where('key', $key)
            ->first();
        return $result ? $result->id : null;
    }

    public function fileDelete()
    {
        Files::where('archivable_id', $this->id)
            ->where('archivable_type', $this->getMorphClass())
            ->get()
            ->each(function ($item) {
                $item->delete();
            });
    }

    public function getFiles($key = null)
    {
        $result = Files::where('archivable_id', $this->id)
            ->where('archivable_type', $this->getMorphClass())
            ->where('key', $key)
            ->get();
        return $result ? $result : [];
    }

    public function deleteFiles()
    {
        Files::where('archivable_id', $this->id)
            ->where('archivable_type', $this->getMorphClass())
            ->get()
            ->each(function ($item) {
                $item->delete();
            });
    }
}
