<?php
/**
 * Dashboard Columns Module
 *
 * Unlocks 1–4 column dashboard layouts via Screen Options (based on add-dashboard-columns).
 *
 * @package DashboardColumns
 * @since 1.0.0
 */

namespace DashboardColumns;

use WP_PowerSuite\Core\AbstractModule;

class Module extends AbstractModule {

	private const MODULE_ID = 'dashboard-columns';
	private const MODULE_VERSION = '1.1.1';

	public function getId(): string {
		return self::MODULE_ID;
	}

	public function getName(): string {
		return __( 'Dashboard Columns', 'wp-powersuite' );
	}

	public function getVersion(): string {
		return self::MODULE_VERSION;
	}

	public function getDescription(): string {
		return __( 'Choose how many columns the dashboard home uses for widgets.', 'wp-powersuite' );
	}

	public function getContext(): string|array {
		return 'admin';
	}

	protected function registerHooks(): void {
		if ( ! is_admin() ) {
			return;
		}

		add_action( 'load-index.php', [ $this, 'onDashboardLoad' ] );
	}

	public function onDashboardLoad(): void {
		if ( ! current_user_can( 'read' ) ) {
			return;
		}

		add_screen_option(
			'layout_columns',
			[
				'max'     => 4,
				'default' => 2,
			]
		);

		add_action( 'admin_enqueue_scripts', [ $this, 'enqueueAssets' ], 999 );
	}

	public function enqueueAssets( string $hook ): void {
		if ( $hook !== 'index.php' ) {
			return;
		}
		if ( ! current_user_can( 'read' ) ) {
			return;
		}

		$url = $this->assetBaseUrl();

		wp_enqueue_style(
			'wpps-dashboard-columns',
			$url . 'dashboard-columns.css',
			[],
			self::MODULE_VERSION
		);

		wp_enqueue_script(
			'wpps-dashboard-columns',
			$url . 'dashboard-columns.js',
			[ 'jquery' ],
			self::MODULE_VERSION,
			true
		);
	}

	private function assetBaseUrl(): string {
		if ( defined( 'WP_POWERSUITE_PLUGIN_URL' ) ) {
			return WP_POWERSUITE_PLUGIN_URL . 'modules/' . self::MODULE_ID . '/assets/';
		}

		return plugin_dir_url( dirname( __DIR__, 2 ) . '/wp-powersuite.php' ) . 'modules/' . self::MODULE_ID . '/assets/';
	}
}
