<?php

namespace App\Domains\Bio\Models;

use App\Domains\Domain\Models\Domain;
use App\Domains\Workspace\Models\Workspace;
use Database\Factories\BioPageFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
use Spatie\Translatable\HasTranslations;

class BioPage extends Model
{
    /** @use HasFactory<BioPageFactory> */
    use HasFactory, HasTranslations, SoftDeletes;

    protected $guarded = ['id'];

    /** @var array<int, string> */
    public array $translatable = ['title', 'description'];

    protected $casts = [
        'theme' => 'array',
        'is_published' => 'boolean',
        'ulid' => 'string',
    ];

    protected static function newFactory(): BioPageFactory
    {
        return BioPageFactory::new();
    }

    protected static function booted(): void
    {
        static::creating(function (self $model) {
            if (empty($model->ulid)) {
                $model->ulid = (string) Str::ulid();
            }
        });
    }

    /** @return HasMany<BioBlock, $this> */
    public function blocks(): HasMany
    {
        return $this->hasMany(BioBlock::class)->orderBy('position');
    }

    /** @return BelongsTo<Workspace, $this> */
    public function workspace(): BelongsTo
    {
        return $this->belongsTo(Workspace::class);
    }

    /** @return BelongsTo<Domain, $this> */
    public function domain(): BelongsTo
    {
        return $this->belongsTo(Domain::class);
    }
}
