<?php
/**
 * Plugin bootstrap.
 *
 * @package RestaurantCosting
 */

declare(strict_types=1);

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

/**
 * Main plugin singleton.
 */
final class RC_Plugin {

	private static ?self $instance = null;

	public static function instance(): self {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	public function init(): void {
		RC_Database::maybe_upgrade();

		add_action( 'init', array( $this, 'register_taxonomies' ) );
		add_filter( 'wp_mail_from_name', array( $this, 'set_mail_from_name' ) );
		add_filter( 'wp_mail_from', array( $this, 'set_mail_from' ) );

		if ( is_admin() ) {
			$this->ensure_roles();
			RC_Admin::instance()->init();
		}

		load_plugin_textdomain( 'restaurant-costing', false, dirname( plugin_basename( RC_PLUGIN_FILE ) ) . '/languages' );
	}

	public function set_mail_from_name(): string {
		return wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
	}

	public function set_mail_from( string $email ): string {
		if ( str_contains( $email, 'wordpress@' ) ) {
			$domain = wp_parse_url( home_url(), PHP_URL_HOST ) ?: 'itoversight.ca';
			$email = 'wordpress@' . $domain;
		}
		return $email;
	}

	public function ensure_roles(): void {
		if ( get_option( 'rc_roles_version' ) !== RC_ROLES_VERSION ) {
			RC_Roles::register_roles();
			update_option( 'rc_roles_version', RC_ROLES_VERSION );
		}
	}

	public function register_taxonomies(): void {
		register_taxonomy(
			'rc_menu_item',
			'attachment',
			array(
				'label'              => __( 'Menu Item Photo', 'restaurant-costing' ),
				'public'             => false,
				'show_ui'            => true,
				'show_admin_column'  => true,
				'show_in_menu'       => false,
				'hierarchical'       => true,
				'rewrite'            => false,
				'query_var'          => false,
				'capabilities'       => array(
					'manage_terms' => RC_Roles::CAP_MANAGE_SETTINGS,
					'edit_terms'   => RC_Roles::CAP_MANAGE_SETTINGS,
					'delete_terms' => RC_Roles::CAP_MANAGE_SETTINGS,
					'assign_terms' => RC_Roles::CAP_EDIT_RECIPES,
				),
			)
		);

		if ( is_admin() ) {
			add_action( 'restrict_manage_posts', array( $this, 'add_media_taxonomy_filter' ) );
			add_filter( 'pre_get_posts', array( $this, 'filter_media_by_taxonomy' ) );
		}
	}

	public function filter_media_by_taxonomy( WP_Query $query ): WP_Query {
		if ( ! is_admin() || 'attachment' !== $query->get( 'post_type' ) ) {
			return $query;
		}
		$term_id = isset( $_GET['rc_menu_item'] ) ? (int) $_GET['rc_menu_item'] : 0;
		if ( $term_id > 0 ) {
			$tax_query = array(
				array(
					'taxonomy' => 'rc_menu_item',
					'field'    => 'term_id',
					'terms'    => $term_id,
				),
			);
			$query->set( 'tax_query', $tax_query );
		}
		return $query;
	}

	public function add_media_taxonomy_filter(): void {
		$screen = get_current_screen();
		if ( ! $screen || 'upload' !== $screen->base ) {
			return;
		}
		$terms = get_terms( array( 'taxonomy' => 'rc_menu_item', 'hide_empty' => false ) );
		if ( empty( $terms ) ) {
			return;
		}
		$current = isset( $_GET['rc_menu_item'] ) ? (int) $_GET['rc_menu_item'] : 0;
		?>
		<select name="rc_menu_item">
			<option value="0"><?php esc_html_e( 'All menu items', 'restaurant-costing' ); ?></option>
			<?php foreach ( $terms as $term ) : ?>
				<option value="<?php echo (int) $term->term_id; ?>" <?php selected( $current, $term->term_id ); ?>><?php echo esc_html( $term->name ); ?></option>
			<?php endforeach; ?>
		</select>
		<?php
	}
}
