<?php

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

class Pixelmole_Followup_Plugin {
	const OPTION_KEY = 'pixelmole_followup_settings';
	const NONCE_KEY  = 'pixelmole_followup_nonce';
	const VERSION    = '1.0.5';
	const CRON_HOOK  = 'pixelmole_followup_send_email';
	const MENU_SLUG  = 'follow-up-emails';
	const ACTIVATED_AT_OPTION = 'pixelmole_followup_activated_at';
	const REVIEW_STATUS_OPTION = 'pixelmole_followup_review_status';
	const REVIEW_SNOOZE_UNTIL_OPTION = 'pixelmole_followup_review_snooze_until';

	/** @var Pixelmole_Followup_Plugin|null */
	private static $instance = null;

	/** @var Pixelmole_Followup_Logger */
	private $logger;

	/** @var Pixelmole_Followup_Email_Store */
	private $email_store;

	/** @var Pixelmole_Followup_Admin */
	private $admin;

	/** @var Pixelmole_Followup_Emails */
	private $emails;

	private function __construct() {
		$this->logger = new Pixelmole_Followup_Logger( $this );
		$this->email_store = new Pixelmole_Followup_Email_Store();
		$this->admin  = new Pixelmole_Followup_Admin( $this, $this->logger );
		$this->emails = new Pixelmole_Followup_Emails( $this, $this->logger );

		add_action( 'plugins_loaded', [ $this->logger, 'maybe_create_table' ], 5 );
		add_action( 'plugins_loaded', [ $this->email_store, 'maybe_create_table' ], 5 );
		add_action(
			'plugins_loaded',
			function () {
				$this->email_store->maybe_migrate_from_option( self::OPTION_KEY, self::get_default_settings() );
			},
			6
		);

		$this->admin->register_hooks();
		$this->emails->register_hooks();
	}

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

		return self::$instance;
	}

	public static function activate() {
		Pixelmole_Followup_Logger::create_table();
		Pixelmole_Followup_Email_Store::create_table();
		$store = new Pixelmole_Followup_Email_Store();
		$store->upsert_primary_email( self::get_default_settings() );
		if ( ! get_option( self::ACTIVATED_AT_OPTION ) ) {
			add_option( self::ACTIVATED_AT_OPTION, time() );
		}
	}

	public static function uninstall() {
		delete_option( self::OPTION_KEY );
		delete_option( self::ACTIVATED_AT_OPTION );
		delete_option( self::REVIEW_STATUS_OPTION );
		delete_option( self::REVIEW_SNOOZE_UNTIL_OPTION );
		Pixelmole_Followup_Logger::drop_table();
		Pixelmole_Followup_Email_Store::drop_table();
	}

	public function get_settings() {
		$settings = $this->email_store->get_primary_email();

		if ( empty( $settings ) ) {
			$settings = (array) get_option( self::OPTION_KEY, [] );
		}

		return wp_parse_args( $settings, self::get_default_settings() );
	}

	public static function get_default_settings() {
		return [
			'enabled'      => 0,
			'from_name'    => get_bloginfo( 'name' ),
			'from_email'   => self::get_default_from_email(),
			'subject'      => __( 'Thanks for your order! 🙌', 'pixelmole-follow-up-emails' ),
			'body'         => __( "Hi {{customer_first_name}},\n\nThanks for ordering at {{site_name}}. If you have any questions, just reply to this email.\n\n— The {{site_name}} team", 'pixelmole-follow-up-emails' ),
			'delay_amount' => 14,
			'delay_unit'   => 'days',
		];
	}

	public function get_option_key() {
		return self::OPTION_KEY;
	}

	public function get_nonce_key() {
		return self::NONCE_KEY;
	}

	public function get_cron_hook() {
		return self::CRON_HOOK;
	}

	public function get_menu_slug() {
		return self::MENU_SLUG;
	}

	public function get_version() {
		return self::VERSION;
	}

	public function get_asset_url( $relative ) {
		return trailingslashit( PIXELMOLE_FOLLOWUP_URL ) . ltrim( $relative, '/' );
	}

	public function get_asset_path( $relative ) {
		return trailingslashit( PIXELMOLE_FOLLOWUP_PATH ) . ltrim( $relative, '/' );
	}

	public function get_logger() {
		return $this->logger;
	}

	public function get_email_store() {
		return $this->email_store;
	}

	private static function get_default_from_email() {
		$wc_email = sanitize_email( (string) get_option( 'woocommerce_email_from_address', '' ) );

		if ( $wc_email ) {
			return $wc_email;
		}

		return sanitize_email( (string) get_option( 'admin_email', '' ) );
	}
}
