<?php
declare(strict_types=1);

namespace ROITheme\Shared\Infrastructure\Persistence\WordPress;

use ROITheme\Shared\Domain\Contracts\WrapperVisibilityCheckerInterface;
use ROITheme\Shared\Infrastructure\Services\PageVisibilityHelper;

/**
 * Implementación de WrapperVisibilityCheckerInterface para WordPress
 *
 * Responsabilidad: Consultar BD y evaluar visibilidad de wrappers de componentes
 *
 * - Consulta tabla wp_roi_theme_component_settings para is_enabled, show_on_mobile, show_on_desktop
 * - Delega evaluación de exclusiones a PageVisibilityHelper (DRY)
 *
 * @package ROITheme\Shared\Infrastructure\Persistence\WordPress
 * @see Plan 99.15 - Fix Empty Layout Wrappers
 */
final class WordPressComponentVisibilityRepository implements WrapperVisibilityCheckerInterface
{
    private string $tableName;

    public function __construct(
        private \wpdb $wpdb
    ) {
        $this->tableName = $this->wpdb->prefix . 'roi_theme_component_settings';
    }

    /**
     * {@inheritDoc}
     */
    public function isEnabled(string $componentName): bool
    {
        $value = $this->getVisibilityAttribute($componentName, 'is_enabled');

        // Si no existe el registro, asumir habilitado por defecto
        if ($value === null) {
            return true;
        }

        return $this->toBool($value);
    }

    /**
     * {@inheritDoc}
     */
    public function isVisibleOnDevice(string $componentName, bool $isMobile): bool
    {
        $attribute = $isMobile ? 'show_on_mobile' : 'show_on_desktop';
        $value = $this->getVisibilityAttribute($componentName, $attribute);

        // Si no existe el registro, asumir visible por defecto
        if ($value === null) {
            return true;
        }

        return $this->toBool($value);
    }

    /**
     * {@inheritDoc}
     *
     * Delega a PageVisibilityHelper que ya implementa:
     * - Visibilidad por tipo de página (home, posts, pages, archives, search)
     * - Exclusiones por categoría, post ID, URL pattern
     */
    public function isNotExcluded(string $componentName): bool
    {
        return PageVisibilityHelper::shouldShow($componentName);
    }

    /**
     * Obtiene un atributo del grupo visibility desde la BD
     *
     * @param string $componentName
     * @param string $attributeName
     * @return string|null
     */
    private function getVisibilityAttribute(string $componentName, string $attributeName): ?string
    {
        $sql = $this->wpdb->prepare(
            "SELECT attribute_value
             FROM {$this->tableName}
             WHERE component_name = %s
               AND group_name = %s
               AND attribute_name = %s
             LIMIT 1",
            $componentName,
            'visibility',
            $attributeName
        );

        $result = $this->wpdb->get_var($sql);

        return $result !== null ? (string) $result : null;
    }

    /**
     * Convierte string a boolean
     *
     * @param string $value
     * @return bool
     */
    private function toBool(string $value): bool
    {
        return $value === '1' || strtolower($value) === 'true';
    }
}
