<?php
/**
 * AnyCoin File Manager utilities.
 *
 * @package AnyCoin_Payment_Gateway
 */

defined( 'ABSPATH' ) || exit;

/**
 * Zano File Manager
 *
 * Handles all file and directory operations for the Zano Payment Gateway.
 * Extracted from utilities for better separation of concerns.
 */
class AnyCoin_File_Manager {

	/**
	 * Create plugin directories
	 *
	 * @return bool True on success
	 */
	public static function create_directories() {
		// Avoid creating directories inside the plugin folder at runtime.
		return true;
	}

	/**
	 * Ensure a directory exists, create if it doesn't.
	 *
	 * @param string $dir Directory path.
	 * @return bool True if directory exists or was created.
	 */
	public static function ensure_directory_exists( $dir ) {
		if ( file_exists( $dir ) ) {
			return is_dir( $dir );
		}

		return wp_mkdir_p( $dir );
	}

	/**
	 * Create default assets
	 *
	 * @return bool True on success
	 */
	public static function create_default_assets() {
		// Do not write assets into the plugin directory at runtime.
		return true;
	}

	/**
	 * Create default Zano icon if it doesn't exist
	 *
	 * @return bool True on success
	 */
	public static function create_default_icon() {
		return true; }

	/**
	 * Create FUSD icon if it doesn't exist
	 *
	 * @return bool True on success
	 */
	public static function create_fusd_icon() {
		return true; }

	/**
	 * Create asset files (CSS, JS placeholders)
	 *
	 * @return bool True on success
	 */
	public static function create_asset_files() {
		return true; }

	/**
	 * Clean up temporary files
	 *
	 * @return bool True on success
	 */
	public static function cleanup_temp_files() {
		return true; }

	/**
	 * Delete directory and all its contents recursively
	 *
	 * @param string $dir Directory path.
	 * @return bool True on success.
	 */
	public static function delete_directory_recursive( $dir ) {
		if ( ! is_dir( $dir ) ) {
			return false;
		}

		$files = array_diff( scandir( $dir ), [ '.', '..' ] );

		foreach ( $files as $file ) {
			$path = $dir . DIRECTORY_SEPARATOR . $file;

			if ( is_dir( $path ) ) {
				self::delete_directory_recursive( $path );
			} else {
				wp_delete_file( $path );
			}
		}

		if ( ! function_exists( 'WP_Filesystem' ) ) {
			require_once ABSPATH . 'wp-admin/includes/file.php';
		}
		WP_Filesystem();
		global $wp_filesystem;
		return $wp_filesystem->rmdir( $dir );
	}

	/**
	 * Get log file path
	 *
	 * @param string $log_name Log file name (without extension).
	 * @return string Full path to log file.
	 */
	public static function get_log_file_path( $log_name = 'zano-payment' ) {
		$uploads = wp_upload_dir();
		$dir     = trailingslashit( $uploads['basedir'] ) . 'anycoin-logs/';
		if ( ! file_exists( $dir ) ) {
			wp_mkdir_p( $dir );
		}
		return $dir . $log_name . '.log';
	}

	/**
	 * Write to log file
	 *
	 * @param string $message Log message.
	 * @param string $log_name Log file name.
	 * @return bool True on success.
	 */
	public static function write_log( $message, $log_name = 'zano-payment' ) {
		$log_file = self::get_log_file_path( $log_name );

		$log_dir = dirname( $log_file );
		if ( ! self::ensure_directory_exists( $log_dir ) ) {
			return false;
		}

		$timestamp   = gmdate( 'Y-m-d H:i:s' );
		$log_message = sprintf( "[%s] %s\n", $timestamp, $message );

		if ( function_exists( 'wc_get_logger' ) ) {
			$logger = wc_get_logger();
			$logger->debug( $message, [ 'source' => 'anycoin-' . $log_name ] );
			return true;
		}
		if ( ! function_exists( 'WP_Filesystem' ) ) {
			require_once ABSPATH . 'wp-admin/includes/file.php';
		}
		WP_Filesystem();
		global $wp_filesystem;
		$existing = '';
		if ( $wp_filesystem && $wp_filesystem->exists( $log_file ) ) {
			$existing_contents = $wp_filesystem->get_contents( $log_file );
			$existing          = is_string( $existing_contents ) ? $existing_contents : '';
		}
		return $wp_filesystem ? $wp_filesystem->put_contents( $log_file, $existing . $log_message, FS_CHMOD_FILE ) : false;
	}

	/**
	 * Get log file contents
	 *
	 * @param string $log_name Log file name.
	 * @param int    $lines Number of lines to read from end (0 = all).
	 * @return string Log contents.
	 */
	public static function get_log_contents( $log_name = 'zano-payment', $lines = 100 ) {
		$log_file = self::get_log_file_path( $log_name );

		if ( ! file_exists( $log_file ) ) {
			return '';
		}

		if ( $lines === 0 ) {
			if ( ! function_exists( 'WP_Filesystem' ) ) {
				require_once ABSPATH . 'wp-admin/includes/file.php';
			}
			WP_Filesystem();
			global $wp_filesystem;
			$contents = $wp_filesystem ? $wp_filesystem->get_contents( $log_file ) : '';
			return is_string( $contents ) ? $contents : '';
		}

		$file = file( $log_file );
		return implode( '', array_slice( $file, -$lines ) );
	}

	/**
	 * Clear log file
	 *
	 * @param string $log_name Log file name.
	 * @return bool True on success.
	 */
	public static function clear_log( $log_name = 'zano-payment' ) {
		$log_file = self::get_log_file_path( $log_name );

		if ( ! file_exists( $log_file ) ) {
			return true;
		}

		if ( ! function_exists( 'WP_Filesystem' ) ) {
			require_once ABSPATH . 'wp-admin/includes/file.php';
		}
		WP_Filesystem();
		global $wp_filesystem;
		return $wp_filesystem ? $wp_filesystem->put_contents( $log_file, '', FS_CHMOD_FILE ) : false;
	}

	/**
	 * Get asset URL
	 *
	 * @param string $asset_path Relative asset path.
	 * @return string Full asset URL.
	 */
	public static function get_asset_url( $asset_path ) {
		return ANYCOIN_PAYMENT_PLUGIN_URL . $asset_path;
	}

	/**
	 * Get image URL
	 *
	 * @param string $image_name Image filename.
	 * @return string Full image URL.
	 */
	public static function get_image_url( $image_name ) {
		return self::get_asset_url( AnyCoin_Constants::IMAGES_DIR . $image_name );
	}

	/**
	 * Check if file exists and is readable
	 *
	 * @param string $file_path File path.
	 * @return bool True if file exists and is readable.
	 */
	public static function is_file_readable( $file_path ) {
		return file_exists( $file_path ) && is_readable( $file_path );
	}

	/**
	 * Get file size in human readable format
	 *
	 * @param string $file_path File path.
	 * @return string File size.
	 */
	public static function get_file_size( $file_path ) {
		if ( ! self::is_file_readable( $file_path ) ) {
			return '0 B';
		}

		$bytes       = filesize( $file_path );
		$units       = [ 'B', 'KB', 'MB', 'GB' ];
		$units_count = count( $units );

		for ( $i = 0; $bytes > 1024 && $i < ( $units_count - 1 ); $i++ ) {
			$bytes /= 1024;
		}

		return round( $bytes, 2 ) . ' ' . $units[ $i ];
	}

	/**
	 * Get default Zano icon data (base64 encoded PNG)
	 *
	 * @return string Base64 encoded icon data.
	 */
	private static function get_default_zano_icon_data() {
		return 'iVBORw0KGgoAAAANSUhEUgAA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ycf0SUyv+IKZlhpTOxJT00lxBdRMFKaqJLeqixjESX9qPgvwIAqX9PQe6aXzsAAAAldEVYdGRhdGU6Y3JlYXRlADIwMjMtMTItMDRUMDc6MzA6MzArMDA6MDCI8IDHAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDIzLTEyLTA0VDA3OjMwOjMwKzAwOjAw+a04ewAAAABJRU5ErkJggg==';
	}

	/**
	 * Get default FUSD icon data (base64 encoded PNG)
	 *
	 * @return string Base64 encoded icon data.
	 */
	private static function get_default_fusd_icon_data() {
		return 'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAB+1BMVEUAAABcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcqzNcq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