<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;

use App\Traits\PhotosTrait;
use App\Models\ContentElement;

class InlinePhoto extends Model
{
    use HasFactory;
    use PhotosTrait;

    public function saveInlinePhoto($input, $id = null)
    {
        if ($id >= 1) {
            $inline_photo = InlinePhoto::findOrFail($id);
        } else {
            $inline_photo = new InlinePhoto();
        }

        $content_element = ContentElement::findOrFail(Arr::get($input, 'content_element_id'));

        $inline_photo->content_element_id = $content_element->id;
        $inline_photo->save();
        $inline_photo->saveSinglePhoto(['photos' => [$input['photo']]]);

        return $inline_photo;
    }

    public function contentElement()
    {
        return $this->belongsTo(ContentElement::class);
    }

    public function getSearchResultTitle($default = null)
    {
        return $this->contentElement->getSearchResultTitle($default);
    }

    public function getSearchResultPreview($terms)
    {
        return $this->contentElement->getSearchResultPreview($terms);
    }

    public function getSearchResultRank($terms)
    {
        return $this->contentElement->getSearchResultRank($terms);
    }
}
