<?php
declare(strict_types=1);

if (!function_exists('normalizeNullableDate')) {
function normalizeNullableDate(mixed $value): ?string
{
    $value = trim((string)$value);
    return $value === '' ? null : $value;
}
}

if (!function_exists('normalizeNullableString')) {
function normalizeNullableString(mixed $value): ?string
{
    $value = trim((string)$value);
    return $value === '' ? null : $value;
}
}

if (!function_exists('normalizeNullableDecimal')) {
function normalizeNullableDecimal(mixed $value): ?string
{
    $value = trim((string)$value);
    return $value === '' ? null : $value;
}
}

if (!function_exists('normalizeNullableInt')) {
function normalizeNullableInt(mixed $value): ?int
{
    $value = trim((string)$value);
    if ($value === '') {
        return null;
    }
    return (int)$value;
}
}

if (!function_exists('normalizeNullableSignedInt')) {
function normalizeNullableSignedInt(mixed $value): ?int
{
    $value = trim((string)$value);
    if ($value === '') {
        return null;
    }
    return (int)$value;
}
}

if (!function_exists('normalizeNullableBool')) {
function normalizeNullableBool(mixed $value): ?int
{
    $value = trim((string)$value);
    return $value === '' ? null : (((int)$value) === 1 ? 1 : 0);
}
}

if (!function_exists('normalizeNullableTime')) {
function normalizeNullableTime(mixed $value): ?string
{
    $value = trim((string)$value);
    return $value === '' ? null : $value;
}
}

if (!function_exists('normalizeRollupDecimal')) {
function normalizeRollupDecimal(mixed $value): ?string
{
    if ($value === null || $value === '') {
        return null;
    }
    return number_format((float)$value, 2, '.', '');
}
}

if (!function_exists('normalizeRollupInt')) {
function normalizeRollupInt(mixed $value): ?int
{
    if ($value === null || $value === '') {
        return null;
    }
    return (int)round((float)$value);
}
}

if (!function_exists('normalizeNullableScalar')) {
function normalizeNullableScalar(mixed $value): ?string
{
    $value = trim((string)$value);
    return $value === '' ? null : $value;
}
}

if (!function_exists('normalizeStatusValue')) {
function normalizeStatusValue(mixed $value): int
{
    if (is_numeric($value)) {
        return (int)$value === 1 ? 1 : 0;
    }
    return strtolower((string)$value) === 'active' ? 1 : 0;
}
}

if (!function_exists('normalizeStatusForStorage')) {
function normalizeStatusForStorage(string $column, mixed $value): mixed
{
    return ((string)$value === '1') ? 1 : 0;
}
}