<?php 

namespace App\Services ;
use App\Models\Projects;
use App\Services\Base\BaseService;
class ProjectsService extends BaseService {

    public function __construct() 
    {
        parent::__construct(Projects::class);
    }
    public function create($data)
    {
        if (isset($data['image']) && isset(($data['hover_image'])))
        {
            $image = ImageService::upload_image($data['image'], 'projects');
            $hover_image = ImageService::upload_image($data['hover_image'], 'projects');
            $video = ImageService::upload_image($data['video'], 'projects');

        } else 
        {
            $image = null;
        }

        $projects = Projects::create([
            'image' => $image,
            'hover_image' => $hover_image,
            'video' => $video,
            'title' => $data['title'],
        ]);
        $projects->save();

        return [];
    }

    public function updateProjects(int $id, $data)
    {
        $projects = Projects::find($id);

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

    }
    public function delete(int $id)
    {

        $projects = Projects::find($id);
        
        $projects->delete();
        
        ImageService::delete_image($projects->image);
        ImageService::delete_image($projects->hover_image);
        ImageService::delete_image($projects->video);
        
        return [];
    }

}
