<?php
namespace App\Shared\Schema;

use App\Core\SchemaInspector;
use PDO;
use Throwable;

final class SchemaSupport
{
    public static function tableHasColumn(PDO $pdo, string $table, string $column): bool
    {
        try {
            return SchemaInspector::tableHasColumn($pdo, $table, $column);
        } catch (Throwable $e) {
            return false;
        }
    }

    public static function tableExists(PDO $pdo, string $table): bool
    {
        try {
            return SchemaInspector::tableExists($pdo, $table);
        } catch (Throwable $e) {
            return false;
        }
    }

    public static function columnExists(PDO $pdo, string $table, string $column): bool
    {
        try {
            return SchemaInspector::columnExists($pdo, $table, $column);
        } catch (Throwable $e) {
            return false;
        }
    }

    public static function ensureColumn(PDO $pdo, string $table, string $column, string $definitionSql): void
    {
        if (!self::tableExists($pdo, $table)) {
            return;
        }

        $col = str_replace('`', '', $column);
        SchemaInspector::ensureColumn($pdo, $table, $col, $definitionSql);
    }
}
