<?php

declare(strict_types=1);

namespace App\Model;



use App\Enums\ArticleModuleEnum;
use App\Enums\ArticlePublishedStatusEnum;
use App\Enums\SpiderArticlePublishedStatusEnum;
use Carbon\Carbon;
use Hyperf\Database\Model\Builder;

/**
 * @property int $id 
 * @property string $title 
 * @property string $description 
 * @property string $cover 
 * @property int $brand 
 * @property int $year 
 * @property int $deleted_at 
 * @property Carbon $updated_at 
 * @property string $platform 
 * @property string $source_url 
 * @property int $published_status 
 * @property int $published_at 
 * @property mixed $created_at
 * @property mixed $module
 * @property mixed $images
 */
class AppSpiderArticle extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'app_spider_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', 'published_status' => 'integer', 'published_at' => 'integer'];

    public function getCreatedAtAttribute($value): false|string
    {
        return $this->format('created_at', function (bool $isFormat) use ($value) {
            return $isFormat ? date('Y-m-d H:i:s', intval($value)) : $value;
        });
    }

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

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

    public function getBrandAttribute($value)
    {
        return $this->format('brand', 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 ? SpiderArticlePublishedStatusEnum::from($value)->toString() : $value;
        });
    }

}
