<?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 Esg extends Model
{
    use HasFactory, SoftDeletes, HasTranslations;
    //
    protected $guarded = ["id"];
    protected $casts = [
        'on_top' => 'boolean'
    ];
    protected $appends = ["banner_pc_url", "banner_mobile_url"];

    public $translatable = ['title', 'banner_alt', 'description'];

    public function paragraphs(){
        return $this->hasMany(EsgParagraph::class)->orderBy('order');
    }
    protected function bannerPcUrl(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => is_null($this->banner_pc) ? null :Storage::disk('public')->url($this->banner_pc),
        );
    }

    protected function bannerMobileUrl(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => is_null($this->banner_mobile) ? null :Storage::disk('public')->url($this->banner_mobile),
        );
    }

    protected function getContentByKeyword($keyword){
        return $this->where("keyword", $keyword)->with('paragraphs')->first();
    }
}
