<?php
/** 
 * @package     VikRestaurants
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

namespace E4J\VikRestaurants\Metadata\Addon;

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

use E4J\VikRestaurants\Metadata\Metadata;

/**
 * Backup details metadata representation.
 * 
 * @since 1.10
 */
class BackupDetails implements Metadata
{
    /** @var string */
    protected $file;

    /** @var array */
    protected $options;

    /**
     * Class constructor.
     * 
     * @param  string  $file     The full path of the backup archive.
     * @param  array   $options  An array of options.
     */
    public function __construct(string $file, array $options = [])
    {
        $this->file = $file;
        $this->options = array_merge([
            'download' => true,
        ], $options);
    }

    /**
     * @inheritDoc
     */
    public function describe()
    {
        // obtain backup details
        $backup = \JModelVRE::getInstance('backup')->getItem($this->file);

        if (!$backup)
        {
            // the backup no longer exists
            return '<span style="color: #900;">The backup archive no longer exists.</span><br><strong>' . $this->file . '</strong>';
        }

        // prepare layout widget data
        $data = [
            'title' => \JText::translate('VRECONFIGTABBACKUP'),
            'icon'  => 'fas fa-file-archive',
            'class' => 'backup-archive',
        ];

        // render backup widget body
        $data['summary'] = \JLayoutHelper::render('addons.components.backup', [
            'backup' => $backup,
        ]);

        if (!empty($this->options['download']))
        {
            // display the button to download the backup archive
            $data['footer'] = '<a class="btn btn-success" href="' . $backup->url . '" target="_blank">' . \JText::translate('VRDOWNLOAD') . '</a>';
        }

        // render add-on widget
        return \JLayoutHelper::render('addons.widget', $data);
    }
}
