<?php

namespace Daylight\Core\Models;

use Daylight\Core\Modules\Multilingualism\Contracts\HasTranslations;
use Daylight\Core\Modules\Multilingualism\Models\Concerns\InteractsWithTranslations;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;

class Category extends Model implements HasTranslations
{
    use HasRecursiveRelationships;
    use InteractsWithTranslations;

    protected $guarded = [];

    public function translatedFields(): array
    {
        return ['name', 'slug', 'meta_title', 'meta_description', 'robots'];
    }

    public function children(): HasMany
    {
        return $this->hasMany(daylightModel('category'), 'parent_id');
    }

    public function childrenRecursive(): HasMany
    {
        return $this->hasMany(daylightModel('category'), 'parent_id')->with('childrenRecursive');
    }

    public function attributes(): BelongsToMany
    {
        return $this->belongsToMany(daylightModel('attribute'));
    }

    public function products(): BelongsToMany
    {
        return $this->belongsToMany(daylightModel('product'));
    }

    public function translations(): HasMany
    {
        return $this->hasMany(daylightModel('category_translation'));
    }

    public function defaultTranslation(): HasOne
    {
        return $this->hasOne(daylightModel('category_translation'))
            ->where('locale', config('multilingualism.default_locale'));
    }
}
