<?php 

namespace App\Services ;
use App\Models\staticInfo;
use App\Services\Base\BaseService;
use Carbon\Carbon;
class StaticInfoService extends BaseService {

    public function __construct() 
    {
        parent::__construct(staticInfo::class);
    }
    public function create($data)
    {
        if ($data['value_type'] == 'image')
    {
            if (isset($data['value']))
    {
                $logo = ImageService::upload_image($data['value'], 'static_info');
                $static_info = staticInfo::create([
                    'key' => $data['key'],
                    'value'=>  $logo,
                    'value_type'=>  'image'
                ]);
                $static_info->save();
                return [];
            } else {
                $logo = null;
            }
        }
        $static_info = staticInfo::create([
            'key' => $data['key'],
            'value'=> $data['value'],
            'value_type'=> $data['value_type']
        ]);
        $static_info->save();

        return [];
    }

    public function updateStaticInfo(int $id, $data)
    {
        $static_info = staticInfo::find($id);

        if ($data['value_type'] == 'image')
    {
            if (isset($data['value']))
    {
                   $logo = ImageService::update_image($data['value'], $static_info->image, 'static_info');   
            }
            $response = $this->update($id,array_merge($data,[ "value" => $logo]));
            return $response ;
        }
        $static_info->update($data);
        return $static_info->update($data);
    }
    public function delete(int $id)
    {

        $static_info = staticInfo::find($id);
        if ($static_info['value_type'] == 'image')
    {
            ImageService::delete_image($static_info->image);
            $static_info->delete();
        }
        $static_info->delete();
        
        return [];
    }

}
