<?php

namespace App\Models;

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

use App\Utilities\SearchResult;

use App\Traits\ContentElementTrait;
use App\Traits\TagsTrait;
use App\Traits\AppendAttributesTrait;
use App\Traits\PhotosTrait;

class InquiryForm extends Model
{
    use HasFactory;
    use ContentElementTrait;
    use TagsTrait;
    use AppendAttributesTrait;
    use PhotosTrait;

    protected $with = ['tags', 'photos'];
    protected $appends = ['livestreams'];

    protected $casts = [
        'show_student_info' => 'boolean',
        'show_interests' => 'boolean',
        'show_livestreams' => 'boolean',
        'show_livestreams_first' => 'boolean',
        'show_question' => 'boolean',
        'create_password' => 'boolean',
    ];

    public function saveContent(array $input, $id = null)
    {
        if ($id >= 1) {
            $inquiry_form = InquiryForm::findOrFail($id);
            $duplicate_photos = false;
        } else {
            $inquiry_form = new InquiryForm();
            $duplicate_photos = true;
        }

        $inquiry_form->header = Arr::get($input, 'header');
        $inquiry_form->body = Arr::get($input, 'body');
        $inquiry_form->show_student_info = Arr::get($input, 'show_student_info') ? true : false;
        $inquiry_form->show_interests = Arr::get($input, 'show_interests') ? true : false;
        $inquiry_form->show_livestreams = Arr::get($input, 'show_livestreams') ? true : false;
        $inquiry_form->show_livestreams_first = Arr::get($input, 'show_livestreams_first') ? true : false;
        $inquiry_form->show_question = Arr::get($input, 'show_question') ? true : false;
        $inquiry_form->show_type = Arr::get($input, 'show_type') ? true : false;
        $inquiry_form->create_password = Arr::get($input, 'create_password') ? true : false;
        $inquiry_form->notification_email = Arr::get($input, 'notification_email');

        $inquiry_form->save();

        $inquiry_form->saveTags($input);
        $inquiry_form->saveSinglePhoto($input, $duplicate_photos);

        cache()->tags([cache_name($inquiry_form)])->flush();
        return $inquiry_form;
    }

    public function getLivestreamsAttribute()
    {
        return Livestream::where('start_date', '>=', today())
            ->get()
            ->filter(function ($livestream) {
                if ($livestream->length) {
                    if ($livestream->start_date->addMinutes((int) $livestream->length) <= now()) {
                        return false;
                    }
                }
                return true;
            })
            ->filter(function ($livestream) {
                if (!$this->tags->count()) {
                    return true;
                }
                return $livestream->tags->intersect($this->tags)->count();
            })
            ->sortBy(function ($livestream) {
                return $livestream->start_date;
            })->values();
    }

    public function getSearchFieldsAttribute()
    {
        return [
            'header',
            'body',
        ];
    }

    public function getSearchResultTitle($default = null)
    {
        if (mb_strlen($this->header) > 0) {
            return $this->header;
        }
        return $default;
    }

    public function getSearchResultPreview($terms)
    {
        return SearchResult::truncatePreview($this->body, $terms);
    }

    public function getSearchResultRank($terms)
    {
        $rank = 0;
        foreach ($terms as $term) {
            $rank += mb_substr_count($this->header, $term) * $this->headerRankMultiplier;
            $rank += mb_substr_count($this->body, $term) * $this->bodyRankMultiplier;
        }
        return $rank;
    }
}
