<?php

namespace App\Models;

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

use App\Utilities\Paginate;

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

use App\Models\Course;

use App\Contracts\SearchResultContract;
use App\Utilities\SearchResult;

class CourseList extends Model implements SearchResultContract
{
    use HasFactory;
    use ContentElementTrait;
    use TagsTrait;
    use AppendAttributesTrait;
    use PhotosTrait;

    protected $with = ['tags'];

    protected $append_attributes = [
        //'courses',
        'staff_profiles',
    ];

    protected $casts = [
        'show_profiles' => 'boolean',
    ];

    public function saveContent(array $input, $id = null)
    {
        if ($id >= 1) {
            $course_list = CourseList::findOrFail($id);
        } else {
            $course_list = new CourseList();
        }

        $course_list->header = Arr::get($input, 'header');
        $course_list->body = Arr::get($input, 'body');
        $course_list->layout = Arr::get($input, 'layout');
        $course_list->show_profiles = Arr::get($input, 'show_profiles');
        $course_list->profiles_title = Arr::get($input, 'profiles_title');
        $course_list->save();

        $course_list->saveTags($input);

        cache()->tags([cache_name($course_list)])->flush();
        $course_list->refresh();
        foreach ($course_list->courses as $course) {
            cache()->tags([cache_name($course)])->flush();
        }
        return $course_list;
    }

    public function getCoursesAttribute()
    {
        if (!$this->tags->count()) {
            return collect();
        }

        return (new Course())->getByTags(['tag_mode' => 'and', 'tags' => $this->tags, 'sort_by' => 'name', 'hide_unlisted' => true]);
    }

    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(strip_tags($this->body) ? $this->body : $this->header, $terms);
    }

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

    public function getStaffProfilesAttribute()
    {
        if (!$this->show_profiles) {
            return collect();
        }

        return (new StaffProfile())->getByTags(['tag_mode' => 'and', 'tags' => $this->tags])
                                ->load('user', 'tags', 'photos')
                                ->each->append('actions')
                                 ->sortBy('last_name')
                                 ->values();
    }
}
