<?php

namespace App\Services;

use App\Models\Partener;
use App\Traits\ImageFunctions;

class PartenersService extends BaseServices
{
    use ImageFunctions;

    protected $model;

    public function __construct()
    {
        $this->model = new Partener();
    }

    public function storePartener($data)
    {
        return $this->model->create([
            'name_en' => $data->name_en,
            'name_ar' => $data->name_ar,
            'image' => $this->storeUniqueImagePath($data->name_en,$data->image, 'parteners'),
            'image_hover' => $this->storeUniqueImagePath($data->name_en.'_hover', $data->image_hover, 'parteners'),
            'link' => $data->link,
        ]);
    }

    public function updatePartener(int $id, $data)
    {
        $partener = $this->model->findOrFail($id);
        $partener->name_en = $data->name_en;
        $partener->name_ar = $data->name_ar;
        $partener->link = $data->link;

        if($data->image) {
            $partener->image = $this->replaceImage($partener->image, $data->image, 'parteners', $data->name_en);
        }

        if($data->image_hover) {
            $partener->image_hover = $this->replaceImage($partener->image_hover, $data->image_hover, 'parteners', $data->name_en.'_hover');
        }

        if($partener->isDirty()) {
            $partener->save();
        }

        return $partener;
    }

    public function deletePartener($id)
    {
        $partener = $this->model->findOrFail($id);
        $this->deleteIfExist($partener->image);
        $this->deleteIfExist($partener->image_hover);
        $partener->delete();
    }
}
