<?php
/**
 * Quick-start checklist for the plays list screen.
 *
 * @package CartCue
 */

namespace CartCue;

defined( 'ABSPATH' ) || exit;

final class QuickStart {

	private const DISMISS_OPTION = 'cartcue_quickstart_dismissed';

	/**
	 * @return array<int, array{ id: string, label: string, done: bool, url: string }>
	 */
	public static function items(): array {
		$settings = MarketingConsent::get_settings();
		$mail     = MailDelivery::get_status();
		$plugin   = Plugin::instance();

		$plays        = $plugin->play_count();
		$active_plays = $plugin->active_play_count();

		$items = [
			[
				'id'    => 'create_play',
				'label' => __( 'Add your first automation', 'cartcue-sales-automation' ),
				'done'  => $plays > 0,
				'url'   => add_query_arg( [ 'page' => 'cartcue-recipes' ], admin_url( 'admin.php' ) ),
			],
			[
				'id'    => 'activate_play',
				'label' => __( 'Activate an automation', 'cartcue-sales-automation' ),
				'done'  => $active_plays > 0,
				'url'   => add_query_arg( [ 'page' => 'cartcue' ], admin_url( 'admin.php' ) ),
			],
			[
				'id'    => 'sender',
				'label' => __( 'Set your sender name & email', 'cartcue-sales-automation' ),
				'done'  => '' !== trim( (string) ( $settings['from_email'] ?? '' ) )
					|| '' !== trim( (string) ( $settings['from_name'] ?? '' ) ),
				'url'   => add_query_arg( [ 'page' => 'cartcue-settings' ], admin_url( 'admin.php' ) ),
			],
			[
				'id'    => 'delivery_test',
				'label' => __( 'Send a test email', 'cartcue-sales-automation' ),
				'done'  => ! empty( $mail['tested_at'] ) && ! empty( $mail['success'] ),
				'url'   => add_query_arg( [ 'page' => 'cartcue-settings' ], admin_url( 'admin.php' ) ),
			],
		];

		return $items;
	}

	public static function is_complete(): bool {
		foreach ( self::items() as $item ) {
			if ( ! $item['done'] ) {
				return false;
			}
		}
		return true;
	}

	public static function is_dismissed(): bool {
		return (bool) get_option( self::DISMISS_OPTION, false );
	}

	public static function dismiss(): void {
		update_option( self::DISMISS_OPTION, true, false );
	}

	public static function should_show(): bool {
		return ! self::is_dismissed() && ! self::is_complete();
	}

}
