<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Attivita extends \App\Models\AbstractModels\AbstractAttivita
{
    public function logoUrl(): ?string
    {
        if (empty($this->path_logo)) {
            return null;
        }

        return str_starts_with($this->path_logo, 'http')
            ? $this->path_logo
            : asset($this->path_logo);
    }

    public function coverUrl(): ?string
    {
        if (empty($this->path_image)) {
            return null;
        }

        return str_starts_with($this->path_image, 'http')
            ? $this->path_image
            : asset($this->path_image);
    }

    public function iniziali(): string
    {
        $parts = preg_split('/\s+/', trim((string) $this->nome), -1, PREG_SPLIT_NO_EMPTY);

        if (empty($parts)) {
            return '?';
        }

        if (count($parts) === 1) {
            return mb_strtoupper(mb_substr($parts[0], 0, 2));
        }

        return mb_strtoupper(mb_substr($parts[0], 0, 1) . mb_substr($parts[1], 0, 1));
    }
}
