<?php

namespace App\Models;

use App\Traits\LogOptionsTrait;
use Illuminate\Database\Eloquent\Model;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Spatie\Activitylog\Traits\LogsActivity;

/**
 * @package App\Models
 * @property int $id
 * @property array $name
 * @property string|null $image
 * @property string $slug
 * @property int|null $parent_id
 * @property int $position
 * @property bool $popular
 * @property bool $published
 * @property array $title_seo
 * @property array $keywords
 * @property bool $is_filter_power
 * @property array $descriptions
 *
 * @property-read Brand $brands
 */
class Category extends Model
{
    use LogsActivity, LogOptionsTrait;

    /**
     * @var array
     */
    protected $guarded = ['id'];

    /**
     * @var array
     */
    protected $casts = [
        'name' => 'array',
        'parent_id' => 'integer',
        'position' => 'integer',
        'slug' => 'string',
        'image' => 'string',
        'popular' => 'boolean',
        'published' => 'boolean',
        'keywords' => 'array',
        'descriptions' => 'array',
        'title_seo' => 'array',
        'is_filter_power' => 'boolean'
    ];

    protected $hidden = [
        'created_at',
        'updated_at'
    ];

    protected static $logName = 'categories';
    protected static $logOnlyDirty = true;
    protected static $ignoreChangedAttributes = ['position'];
    protected static $logAttributes = ['name', 'parent_id', 'published', 'title_seo'];
    protected static $submitEmptyLogs = false;

    /**
     * @return int
     */
    public function getID(): int
    {
        return (int) $this->id;
    }

    /**
     * @return string
     */
    public function getRouteKeyName()
    {
        return 'slug';
    }

    /**
     * @return string|null
     */
    public function getLowerName()
    {
        return Str::lower($this->name[App::getLocale()]) ?? null;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        if (App::getLocale() == 'en') {
            return $this->name['ru'];
        }

        return $this->name[App::getLocale()] ?? null;
    }

    /**
     * @return string
     */
    public function getDescriptions(): string
    {
        if (App::isLocale('ru')) {
            if (!empty($this->descriptions['ru'])) {
                return (string) $this->descriptions['ru'];
            }
        }

        if (!empty($this->descriptions['uz'])) {
            return (string) $this->descriptions['uz'];
        }

        return (string) '';
    }

    /**
     * @return string
     */
    public function getKeywords()
    {
        if (App::isLocale('ru')) {
            if (!empty($this->keywords['ru'])) {
                return $this->keywords['ru'];
            }
        }

        if (!empty($this->keywords['uz'])) {
            return $this->keywords['uz'];
        }

        return '';
    }

    public function getImage(): string
    {
        if (!in_array($this->image, ['null', null])) {
            if (in_array(env('FILESYSTEM_DISK'), ['s3', 'minio'])) {
                return Storage::temporaryUrl(
                    $this->image,
                    Date::now()->addMinutes(5)
                );
            }

            return (string) $this->image;
        }

        return (string) 'images/nophoto.jpg';
    }

    public function getParentId(): int
    {
        return $this->parent_id;
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function parent()
    {
        return $this->belongsTo(self::class, 'parent_id', 'id')->where('published', true);
    }

    public function parents()
    {
        return $this->hasMany(self::class, 'parent_id', 'id')->where('published', true)->orderBy('position', 'asc');
    }

    public function children()
    {
        return $this->hasMany(self::class, 'parent_id', 'id')->orderBy('position', 'asc');
    }

    /**
     * @return string
     */
    public function getTitleSeo(): string
    {
        if (App::isLocale('ru')) {
            if (!empty($this->title_seo['ru'])) {
                return (string) $this->title_seo['ru'];
            }
        }

        if (!empty($this->title_seo['uz'])) {
            return (string) $this->title_seo['ru'];
        }

        return $this->getName();
    }


    /**
     * @return int
     */
    public function position(): int
    {
        return (int) $this->position;
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function brands()
    {
        return $this->belongsToMany(Brand::class, 'categories_brands');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function products()
    {
        return $this->belongsToMany(Product::class, 'categories_products');
    }

    /**
     * @param $query
     * @param $slug
     * @return mixed
     */
    public function scopeFindBySlug($query, $slug)
    {
        return $query->where('slug', $slug);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function characteristics()
    {
        return $this->hasMany(Characteristic::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function filter()
    {
        return $this->belongsToMany(Characteristic::class, 'characteristics_categories');
    }
}
