<?php

namespace App\Fields;

use Backpack\CRUD\app\Library\CrudPanel\CrudField;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade;

class IframeField
{
    /**
     * Create an iframe custom field.
     *
     * @param string $name  Field name (not used for display, only for form data)
     * @param string $label Field label
     * @param array  $options Additional options (url, width, height, extra_attributes, hint)
     * @return CrudField
     */
    public static function make(string $name, string $label, array $options = [])
    {
        $url = isset($options['url']) && is_callable($options['url'])
            ? $options['url'](CrudPanelFacade::getFacadeRoot())
            : ($options['url'] ?? null);

        $imageRotation90Url = str_replace('/storage/', '/storage/image-rotation90/', $url);

        $field = [
            'name' => $name,
            'label' => $label,
            'type' => 'iframe',
            'url' => $url, // URL to display in iframe
            'image-rotation-url' => $imageRotation90Url,
            'width' => $options['width'] ?? '', // Iframe width
            'height' => $options['height'] ?? '', // Iframe height
            'extra_attributes' => $options['extra_attributes'] ?? [], // Additional iframe attributes
            'hint' => $options['hint'] ?? null, // Help text
        ];

        return \Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade::field($field);
    }
}