<?php

namespace kernel\app_modules\photo\service;

use kernel\app_modules\photo\models\Photo;
use kernel\FormModel;
use kernel\helpers\Debug;

class PhotoService
{
    public function create(FormModel $form_model): false|Photo
    {
        $model = new Photo();
        $model->image = $form_model->getItem('image');
        $model->entity = $form_model->getItem('entity');
        $model->entity_id = $form_model->getItem('entity_id');

        return false;
    }

    public function update(FormModel $form_model, Photo $photo): false|Photo
    {
        $photo->image = $form_model->getItem('image');
        $photo->entity = $form_model->getItem('entity');
        $photo->entity_id = $form_model->getItem('entity_id');

        if ($photo->save()){
            return $photo;
        }

        return false;
    }

    public static function getByEntity(string $entity, int $entity_id): string
    {
        $photo = Photo::where("entity", $entity)->where("entity_id", $entity_id)->first();
        return $photo->image ?? "";
    }

}