<?php

namespace Twnt4All\Support;

class Paths
{
    public static function get_allowed_subdirs()
    {
        return [
            'locks',
            'logs',
            'temp'
        ];
    }

    public static function get_public_subdirs()
    {
        return [];
    }

    public static function get_options()
    {
        return get_option( T4A_PREFIX . '_options_group' );
    }

    public static function update_options( $data )
    {
        $options                  = static::get_options();
        $options                  = array_merge( $options, $data );
        $options[ 'no_sanitize' ] = true;
        // Careful: password cslashes
        update_option( T4A_PREFIX . '_options_group', $options );
    }

    public static function get_gateway_settings()
    {
        // WooCommerce runtime lives in pay4all-pro. This helper is only meaningful when WC is present.
        if ( ! function_exists( 'WC' ) ) { return []; }
        $wc              = call_user_func( 'WC' );
        $payment_gateway = $wc->payment_gateways->payment_gateways()[ 't4a_twint' ] ?? null;
        return $payment_gateway ? $payment_gateway->settings : [];
    }

    public static function get_license_options()
    {
        return get_option( T4A_PREFIX . '_license_options' );
    }

    public static function update_license_options( $key, $status )
    {
        update_option( T4A_PREFIX . '_license_options', [ 'license_key' => $key, 'license_status' => $status ] );
    }

    public static function get_plugin_uploads_dir_path()
    {
        return static::get_or_create_dir( T4A_UPLOAD_DIR );
    }

    /**
     * Returns the plugin  uploads directory with optional subdirectory. Includes trailing slash.
     *
     * @param null $subdir
     * @return string
     */
    public static function get_upload_path( $subdir = null )
    {
        $dir = T4A_UPLOAD_DIR;

        if ( $subdir ) {

            if ( in_array( $subdir, static::get_allowed_subdirs() ) ) {

                $dir .= $subdir . DIRECTORY_SEPARATOR;

            }
        }

        return $dir;
    }

    public static function create_dirs()
    {
        $upload_path = T4A_UPLOAD_DIR;

        if ( !file_exists( $upload_path ) ) {
            wp_mkdir_p( $upload_path );
        }
        self::protect_dir( $upload_path );

        $folders = static::get_allowed_subdirs();
        foreach ( $folders as $folder ) {
            $path = $upload_path . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR;
            if ( !file_exists( $path ) ) {
                wp_mkdir_p( $path );
            }
            self::protect_dir( $path );
        }
    }

    /**
     * Belt-and-suspenders directory protection : `.htaccess` for Apache
     * + empty `index.php` so nginx (which ignores `.htaccess`) can't
     * serve a directory listing or an unlisted file the moment the
     * merchant's server config skips its usual « no autoindex » block.
     * The audit specifically flagged this : `.htaccess` alone was a
     * silent no-op on the majority of shared-hosting nginx setups.
     */
    private static function protect_dir( string $path ) : void
    {
        $path = rtrim( $path, DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR;
        $ht   = $path . '.htaccess';
        if ( ! file_exists( $ht ) ) {
            file_put_contents( $ht, "Order allow,deny\nDeny from all\nRequire all denied\n" );
        }
        $idx = $path . 'index.php';
        if ( ! file_exists( $idx ) ) {
            file_put_contents( $idx, "<?php\n// Silence is golden.\n" );
        }
    }


    /**
     * Returns the path to the log file. Creates a new log file if log file is too large.
     *
     * @return string
     */
    public static function get_log_file_path()
    {
        $name = gmdate( 'd-m-Y' );
        $path = static::get_upload_path( 'logs' ) . $name . '.log';

        if ( !file_exists( $path ) ) {
            file_put_contents( $path, '' );
        }

        return $path;
    }

    private static function get_or_create_dir( $path )
    {
        if ( !file_exists( $path ) ) {
            wp_mkdir_p( $path );
        }
        self::protect_dir( $path );

        return $path;
    }
}
