<?php
/**
 * `wp blaminhor favicon ...` — Favicon Generator commands.
 *
 * @package BlaminhorEssentials
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
	return;
}

/**
 * Generate / delete every favicon format from a single source image.
 */
class Blaminhor_Essentials_CLI_Favicon extends Blaminhor_Essentials_CLI_Module {

	/**
	 * @var string
	 */
	protected $module_id = 'favicon';

	/**
	 * Show the current favicon settings.
	 *
	 * @when after_wp_load
	 *
	 * @param array $args       Positional args (unused).
	 * @param array $assoc_args Associative args.
	 */
	public function get( $args, $assoc_args ) {
		$module   = $this->get_module_instance();
		$settings = $module->get_effective_settings();

		$rows = array(
			array( 'key' => 'source_image_id', 'value' => (string) (int) $settings['source_image_id'] ),
			array( 'key' => 'theme_color',     'value' => (string) $settings['theme_color'] ),
			array( 'key' => 'bg_color',        'value' => (string) $settings['bg_color'] ),
			array( 'key' => 'generated',       'value' => $settings['generated'] ? 'true' : 'false' ),
		);
		if ( ! empty( $settings['source_image_id'] ) ) {
			$src = wp_get_attachment_image_src( (int) $settings['source_image_id'], 'full' );
			if ( $src ) {
				$rows[] = array( 'key' => 'source_image_url', 'value' => (string) $src[0] );
			}
		}
		$this->format_for_output( $rows, $assoc_args, array( 'key', 'value' ) );
	}

	/**
	 * Generate every favicon format from an existing media library image.
	 *
	 * ## OPTIONS
	 *
	 * <attachment-id>
	 * : Numeric ID of an image attachment in the media library.
	 *
	 * [--theme-color=<hex>]
	 * : Theme color (default #ffffff).
	 *
	 * [--bg-color=<hex>]
	 * : Background color (default #ffffff).
	 *
	 * ## EXAMPLES
	 *
	 *     $ wp blaminhor favicon generate 42 --theme-color=#1d4ed8
	 *
	 * @when after_wp_load
	 *
	 * @param array $args       Positional args: [<attachment-id>].
	 * @param array $assoc_args Associative args.
	 */
	public function generate( $args, $assoc_args ) {
		$this->ensure_capabilities();
		if ( ! isset( $args[0] ) || ! is_numeric( $args[0] ) ) {
			WP_CLI::error( __( 'Missing or invalid attachment ID.', 'blaminhor-essentials' ) );
		}
		$attachment_id = (int) $args[0];
		$post          = get_post( $attachment_id );
		if ( ! $post || 'attachment' !== $post->post_type ) {
			WP_CLI::error( sprintf(
				/* translators: %d: attachment ID typed by the user */
				__( 'No attachment found with ID %d.', 'blaminhor-essentials' ),
				$attachment_id
			) );
		}

		$module = $this->get_module_instance();
		if ( ! method_exists( $module, 'generate_favicons' ) ) {
			WP_CLI::error( __( 'This build of the Favicon module does not expose generate_favicons().', 'blaminhor-essentials' ) );
		}

		// Persist the settings the user passed BEFORE generating, so the module
		// reads them from the option during generation.
		$current = $module->get_effective_settings();
		$data    = array(
			'source_image' => (string) $attachment_id,
			'theme_color'  => isset( $assoc_args['theme-color'] ) ? (string) $assoc_args['theme-color'] : (string) $current['theme_color'],
			'bg_color'     => isset( $assoc_args['bg-color'] ) ? (string) $assoc_args['bg-color'] : (string) $current['bg_color'],
			'generate_favicon' => '1',
		);

		$result = $module->apply_settings( $data );
		if ( is_wp_error( $result ) ) {
			WP_CLI::error( $result->get_error_message() );
		}

		// Re-read to confirm `generated` flipped to true.
		$module     = $this->get_module_instance();
		$is_done    = (bool) $module->get_setting( 'generated', false );
		if ( ! $is_done ) {
			WP_CLI::warning( __( 'apply_settings() returned OK but the "generated" flag is still false. Inspect the admin notices, or run `wp blaminhor favicon get`.', 'blaminhor-essentials' ) );
			return;
		}

		blaminhor_essentials_log( sprintf( 'Favicon generated via WP-CLI (attachment #%d).', $attachment_id ), 'info', 'cli', array( 'event' => 'favicon_generate' ) );
		WP_CLI::success( sprintf(
			/* translators: %d: attachment ID */
			__( 'Favicon set generated from attachment #%d.', 'blaminhor-essentials' ),
			$attachment_id
		) );
	}

	/**
	 * Remove every generated favicon file and clear the settings.
	 *
	 * ## OPTIONS
	 *
	 * [--yes]
	 * : Skip the interactive confirmation prompt.
	 *
	 * @when after_wp_load
	 *
	 * @param array $args       Positional args (unused).
	 * @param array $assoc_args Associative args.
	 */
	public function delete( $args, $assoc_args ) {
		$this->ensure_capabilities();
		$module = $this->get_module_instance();

		if ( ! $module->get_setting( 'generated' ) ) {
			WP_CLI::warning( __( 'No favicons have been generated.', 'blaminhor-essentials' ) );
			return;
		}

		WP_CLI::confirm(
			__( 'Delete every generated favicon file and reset the settings?', 'blaminhor-essentials' ),
			$assoc_args
		);

		$result = $module->apply_settings( array( 'remove_favicon' => '1' ) );
		if ( is_wp_error( $result ) ) {
			WP_CLI::error( $result->get_error_message() );
		}

		blaminhor_essentials_log( 'Favicon set deleted via WP-CLI.', 'info', 'cli', array( 'event' => 'favicon_delete' ) );
		WP_CLI::success( __( 'Favicon set deleted.', 'blaminhor-essentials' ) );
	}
}
