<?php

declare(strict_types=1);

namespace App\Model;



use App\Enums\ArticleModuleEnum;
use App\Enums\ArticlePublishedStatusEnum;
use App\Enums\LocationEnum;
use App\Helpers\TitleHelper;
use Hyperf\Database\Model\Events\Saving;

/**
 * @property int $id 
 * @property string $aid 
 * @property string $title 
 * @property string $description 
 * @property string $cover 
 * @property int $year 
 * @property int $deleted_at 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property int $created_by 
 * @property int $updated_by 
 * @property int $spider_article_id 
 * @property int $style 
 * @property int $location 
 * @property int $images_count 
 * @property int $published_at 
 * @property mixed $images 
 * @property mixed $module
 * @property mixed $brand
 * @property mixed $brand_name
 * @property mixed $published_status
 */
class AppArticle extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'app_articles';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = [];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'brand' => 'integer', 'year' => 'integer', 'module' => 'integer', 'deleted_at' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'created_by' => 'integer', 'updated_by' => 'integer', 'spider_article_id' => 'integer', 'style' => 'integer', 'location' => 'integer', 'images_count' => 'integer', 'published_status' => 'integer', 'published_at' => 'integer'];

    protected ?string $dateFormat = 'U';

//    public function saving(Saving $event)
//    {
//        if ($this->getAttribute('images_count') == 0) {
//            $this->setAttribute('images_count', count(json_decode($this->getAttribute('images'), true)));
//        }
//    }

    public function setImagesAttribute($value)
    {
        if (is_string($value)) {
            $imagesValue = json_decode($value, true);
            $this->setAttribute('images_count', count($imagesValue));
        } elseif (is_array($value)) {
            $this->setAttribute('images_count', count($value));
            $value = json_encode(array_values($value));
        }

        $this->attributes['images'] = $value;
    }

    public function getImagesAttribute($value)
    {
        return $this->format('images', function (bool $isFormat) use ($value) {
            return $isFormat ? json_decode($value, true) : $value;
        });
    }

    public function getModuleAttribute($value)
    {
        return $this->format('module', function (bool $isFormat) use ($value) {
            return $isFormat ? ArticleModuleEnum::from($value)->toString() : $value;
        });
    }

    public function getLocationAttribute($value)
    {
        return $this->format('location', function (bool $isFormat) use ($value) {
            return $isFormat ? LocationEnum::from($value)->toString() : $value;
        });
    }

    public function getBrandAttribute($value)
    {
        return $this->format('brand', function (bool $isFormat) use ($value) {
            return $isFormat ? AppBrand::find($value)?->name : $value;
        });
    }

    public function getBrandNameAttribute($value)
    {
        return $this->format('brand_name', function (bool $isFormat) use ($value) {
            return $isFormat ? AppBrand::find($value)?->name : $value;
        });
    }

    public function getPublishedStatusAttribute($value)
    {
        return $this->format('published_status', function (bool $isFormat) use ($value) {
            return $isFormat ? ArticlePublishedStatusEnum::from($value)->toString() : $value;
        });
    }

    public function getTranslateTitleAttribute($value)
    {
        return $this->format('translate_title', function (bool $isFormat) use ($value) {
            return $isFormat ? TitleHelper::translate($value) : $value;
        });
    }
}
