<?php 

namespace App\Services;
use App\Models\RecentWorks;
use App\Services\Base\BaseService;
use App\Services\ImageService;
class RecentWorksService extends BaseService {

    public function __construct() 
    {
        parent::__construct(RecentWorks::class);
    }
    public function create($data)
    {
        if (isset($data['image']))
    {
            $image = ImageService::upload_image($data['image'], 'recent_works');
        } else {
            $image = null;
        }

        $recent_works = RecentWorks::create([
            'image' => $image,
            'title' => $data['title'],
            'description' => $data['description'],
        ]);
        $recent_works->save();

        return [];
    }

    public function updateRecentWorks(int $id, $data)
    {

        $services = RecentWorks::find($id);

        if (isset($data['image']))
         {
               $image = ImageService::update_image($data['image'], $services->image, 'services');
               $data['image'] = $image;
            
        }
        return $this->update($id,$data);

    }
    public function delete(int $id)
    {

        $recent_works = RecentWorks::find($id);
        
        $recent_works->delete();
        
        ImageService::delete_image($recent_works->image);
        
        return [];
    }

}
