<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
use Spatie\Translatable\HasTranslations;

class Album extends Model
{
    use HasFactory, SoftDeletes, HasTranslations;

    protected $casts = [
        'on_top' => 'boolean',
        "list_audit_state" => "string"
    ];

    protected $guarded = ['id'];
    protected $appends = ["album_img_pc_url", "album_img_pc_url", "album_img_banner_url", "album_img_mobile_url", "album_meta_img", "album_upload_type", "album_video_link"];

    public $translatable = ['title', 'description', 'meta_title', 'meta_description', 'meta_keyword'];

    public function albumCategory(){
        return $this->belongsTo(AlbumCategory::class);
    }
    protected function albumImgPcUrl(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => is_null($this->news_img_pc) ? null :Storage::url($this->news_img_pc),
        );
    }
    protected function albumImgMobileUrl(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => is_null($this->news_img_mobile) ? null :Storage::url($this->news_img_mobile),
        );
    }
    protected function albumMetaImg(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => is_null($this->meta_img) ? null :Storage::url($this->meta_img),
        );
    }
    protected function albumImgBannerUrl(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => is_null($this->news_banner) ? null :Storage::url($this->news_banner),
        );
    }
    protected function albumUploadType(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => $this->attributes["upload_type"] == 1 ? "url" : "upload",
        );
    }

    protected function listAuditState(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => $this->attributes["visible"] == 1 ? "已發佈" : "暫存",
        );
    }
    protected function albumVideoLink(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => ($this->attributes["upload_type"] == 2) ? Storage::url($this->attributes["link"]) : $this->attributes["link"],
        );
    }
}
