<?php

namespace App\Entities;

use CodeIgniter\Entity\Entity;

class Producto extends Entity
{
    protected $casts = [
        'id_producto' => 'integer',
        'precio'      => 'float',
        'disponible'  => 'integer',
    ];

    public function getImagenUrl(): string
    {
        $url = $this->attributes['imagen_url'] ?? '';
        if (empty($url)) {
            return base_url('img/placeholder-poster.jpg'); // Fallback placeholder
        }

        if (preg_match('#^https?://#i', $url)) {
            return $url;
        }

        return base_url('uploads/productos/' . $url);
    }

    public function estaDisponible(): bool
    {
        return isset($this->attributes['disponible']) && (int)$this->attributes['disponible'] === 1;
    }
}
