<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Kód banky Fio (vždy 2010).
 */
const ELEMENTSELL_FIO_BANK_CODE = '2010';

add_filter( 'pre_update_option_fio_account_number', 'elementsell_sanitize_fio_account_number' );

/**
 * Normalizuje vstup na kanonické číslo Fio účtu (bez kódu banky).
 *
 * Podporované formáty:
 * - samotné číslo: 2900708273, 002900708273
 * - domácí: 2900708273/2010, 2900708273 / 2010
 * - s předčíslím: 123456-789/2010
 * - IBAN: CZ75 2010 0000 0029 0070 8273
 * - kód banky na začátku: 2010-2900708273, 2010/2900708273
 *
 * @param string $input
 * @return string
 */
function elementsell_normalize_fio_account_number( $input ) {
    $input = trim( (string) $input );

    if ( $input === '' ) {
        return '';
    }

    $iban_candidate = strtoupper( preg_replace( '/\s+/', '', $input ) );

    if ( preg_match( '/^CZ\d{22}$/', $iban_candidate ) ) {
        return elementsell_normalize_fio_account_from_iban( $iban_candidate );
    }

    $domestic = preg_replace( '/\s+/', '', $input );

    if ( preg_match( '/^2010[\/\-](.+)$/i', $domestic, $matches ) ) {
        return elementsell_normalize_fio_account_body( $matches[1] );
    }

    if ( preg_match( '/\/2010$/i', $domestic ) ) {
        return elementsell_normalize_fio_account_from_domestic( strtoupper( $domestic ) );
    }

    if ( preg_match( '/\/\d{4}$/i', $domestic ) ) {
        return '';
    }

    return elementsell_normalize_fio_account_body( $input );
}

/**
 * @param string $iban Kompaktní IBAN (CZ + 22 číslic).
 * @return string
 */
function elementsell_normalize_fio_account_from_iban( $iban ) {
    $bank_code = substr( $iban, 4, 4 );

    if ( $bank_code !== ELEMENTSELL_FIO_BANK_CODE ) {
        return '';
    }

    $bban_part = substr( $iban, 8, 16 );
    $prefix    = ltrim( substr( $bban_part, 0, 6 ), '0' );
    $account   = ltrim( substr( $bban_part, 6, 10 ), '0' );

    if ( $prefix === '' && $account === '' ) {
        return '';
    }

    if ( $prefix !== '' ) {
        return $prefix . '-' . ( $account !== '' ? $account : '0' );
    }

    return $account !== '' ? $account : '0';
}

/**
 * @param string $compact Domácí formát s /2010 (bez mezer).
 * @return string
 */
function elementsell_normalize_fio_account_from_domestic( $compact ) {
    if ( ! preg_match( '/^(.+)\/2010$/', $compact, $matches ) ) {
        return '';
    }

    return elementsell_normalize_fio_account_body( $matches[1] );
}

/**
 * @param string $body Část účtu (číslo nebo předčíslí-číslo).
 * @return string
 */
function elementsell_normalize_fio_account_body( $body ) {
    $body = trim( (string) $body );

    if ( $body === '' ) {
        return '';
    }

    if ( preg_match( '/^(\d+)\-(\d+)$/', $body, $matches ) ) {
        $prefix  = ltrim( $matches[1], '0' );
        $account = ltrim( $matches[2], '0' );

        if ( $prefix === '' && $account === '' ) {
            return '';
        }

        if ( $prefix !== '' ) {
            return $prefix . '-' . ( $account !== '' ? $account : '0' );
        }

        return $account !== '' ? $account : '0';
    }

    $digits = preg_replace( '/\D+/', '', $body );

    if ( $digits === '' || ! preg_match( '/^\d+$/', $digits ) ) {
        return '';
    }

    $account = ltrim( $digits, '0' );

    return $account !== '' ? $account : '0';
}

/**
 * Sanitize callback pro WordPress option fio_account_number.
 *
 * @param mixed $value
 * @return string
 */
function elementsell_sanitize_fio_account_number( $value ) {
    return elementsell_normalize_fio_account_number( $value );
}

/**
 * Vrátí normalizované číslo Fio účtu z WP option (zpětná kompatibilita se starými hodnotami).
 *
 * @return string
 */
function elementsell_get_fio_account_number() {
    $raw = get_option( 'fio_account_number', '' );

    return elementsell_normalize_fio_account_number( (string) $raw );
}
