<?php

namespace App\Helpers;

class Asset
{
    public static function avatar(?string $filename, string $username = 'U', int $size = 48): string
    {
        if ($filename && $filename !== 'default.png') {
            if (file_exists(__DIR__ . '/../../public/assets/uploads/avatars/' . $filename)) {
                return '/assets/uploads/avatars/' . $filename;
            }
            if (file_exists(__DIR__ . '/../../public/assets/img/' . $filename)) {
                return '/assets/img/' . $filename;
            }
        }
        if ($filename === 'default.png') return '/assets/img/default.png';
        
        $initial = mb_strtoupper(mb_substr($username, 0, 1));
        return "https://ui-avatars.com/api/?name={$initial}&size={$size}&background=6366f1&color=fff&bold=true";
    }

    public static function cover(?string $filename): string
    {
        if ($filename && $filename !== 'cover_default.jpg' && file_exists(__DIR__ . '/../../public/assets/uploads/covers/' . $filename)) {
            return '/assets/uploads/covers/' . $filename;
        }
        return 'linear-gradient(135deg, #6366f1, #818cf8)';
    }

    public static function url(string $path): string
    {
        return '/' . ltrim($path, '/');
    }
}
