<?php
/**
 * Database environment collector.
 *
 * @package MonitorCat\Modules\Diagnostics\Collector
 */

declare(strict_types=1);

namespace MonitorCat\Modules\Diagnostics\Collector;

defined( 'ABSPATH' ) || exit;

/**
 * Collects database information including version, configuration, and size metrics.
 */
final class DatabaseCollector {

	/**
	 * Collect database environment data.
	 *
	 * @return array{label: string, icon: string, items: array<int, array{label: string, value: string, status?: string}>}
	 */
	public function collect(): array {
		global $wpdb;

		return array(
			'label' => __( 'Database', 'monitor-cat' ),
			'icon'  => 'fa-database',
			'items' => array(
				array(
					'label' => __( 'Server', 'monitor-cat' ),
					'value' => $this->get_server_info(),
				),
				array(
					'label' => __( 'Database Name', 'monitor-cat' ),
					'value' => $wpdb->dbname,
				),
				array(
					'label' => __( 'Table Prefix', 'monitor-cat' ),
					'value' => $wpdb->prefix,
				),
				array(
					'label' => __( 'Character Set', 'monitor-cat' ),
					'value' => $wpdb->charset ? $wpdb->charset : __( 'Not set', 'monitor-cat' ),
				),
				array(
					'label' => __( 'Collation', 'monitor-cat' ),
					'value' => $wpdb->collate ? $wpdb->collate : __( 'Not set', 'monitor-cat' ),
				),
				array(
					'label' => __( 'Max Allowed Packet', 'monitor-cat' ),
					'value' => $this->get_max_allowed_packet(),
				),
				array(
					'label' => __( 'Total Tables', 'monitor-cat' ),
					'value' => $this->get_total_tables(),
				),
				array(
					'label' => __( 'Total Database Size', 'monitor-cat' ),
					'value' => $this->get_database_size(),
				),
			),
		);
	}

	/**
	 * Get the database server information.
	 *
	 * @return string
	 */
	private function get_server_info(): string {
		global $wpdb;

		$server_info = $wpdb->db_server_info();

		if ( ! $server_info ) {
			return __( 'Unknown', 'monitor-cat' );
		}

		return $server_info;
	}

	/**
	 * Get the max allowed packet size.
	 *
	 * @return string
	 */
	private function get_max_allowed_packet(): string {
		global $wpdb;

		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		$result = $wpdb->get_row( "SHOW VARIABLES LIKE 'max_allowed_packet'" );

		if ( ! $result ) {
			return __( 'Unknown', 'monitor-cat' );
		}

		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- MySQL result property name.
		return size_format( (int) $result->Value );
	}

	/**
	 * Get the total number of tables in the database.
	 *
	 * @return string
	 */
	private function get_total_tables(): string {
		global $wpdb;

		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		$tables = $wpdb->get_results( 'SHOW TABLES', ARRAY_N );

		if ( ! $tables ) {
			return '0';
		}

		return (string) count( $tables );
	}

	/**
	 * Get the total database size.
	 *
	 * @return string
	 */
	private function get_database_size(): string {
		global $wpdb;

		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		$size = $wpdb->get_var(
			$wpdb->prepare(
				'SELECT SUM(data_length + index_length) FROM information_schema.tables WHERE table_schema = %s',
				$wpdb->dbname
			)
		);

		if ( null === $size ) {
			return __( 'Unknown', 'monitor-cat' );
		}

		return size_format( (int) $size );
	}
}
