<?php
/**
 * Recovery Carts Table.
 *
 * @package     EDD\Pro\CartRecovery\Database\Tables
 * @copyright   Copyright (c) 2026 Sandhills Development, LLC
 * @license     https://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       3.6.5
 */

namespace EDD\Pro\CartRecovery\Database\Tables;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

use EDD\Database\Table;

/**
 * Registers the edd_acr_carts database table.
 */
final class RecoveryCarts extends Table {

	/**
	 * Table name (without prefix).
	 *
	 * @var string
	 */
	protected $name = 'acr_carts';

	/**
	 * Database version.
	 *
	 * @var int
	 */
	protected $version = 202601090;

	/**
	 * Upgrade routines.
	 *
	 * @var array
	 */
	protected $upgrades = array();

	/**
	 * Set the table schema.
	 */
	protected function set_schema() {
		$this->schema = "id bigint(20) unsigned NOT NULL auto_increment,
			session_id varchar(255) NOT NULL,
			customer_id bigint(20) unsigned default NULL,
			email varchar(255) default NULL,
			cart_contents longtext NOT NULL,
			cart_total decimal(18,9) NOT NULL default '0',
			currency varchar(3) NOT NULL default 'USD',
			discount_applied varchar(100) default NULL,
			browser_fingerprint varchar(255) default NULL,
			timezone varchar(50) default NULL,
			date_expired datetime default NULL,
			cart_version int(11) NOT NULL default '1',
			status varchar(20) NOT NULL default 'tracking',
			date_abandoned datetime default NULL,
			date_recovered datetime default NULL,
			order_id bigint(20) unsigned default NULL,
			recovery_email_count int(11) NOT NULL default '0',
			last_recovery_email datetime default NULL,
			user_agent text,
			ip_address varchar(45) default NULL,
			referrer_url text,
			fail_count int(11) NOT NULL default '0',
			date_unsubscribed datetime default NULL,
			date_created datetime NOT NULL default CURRENT_TIMESTAMP,
			date_modified datetime NOT NULL default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			KEY session_id (session_id),
			KEY idx_session_abandoned (session_id, date_abandoned),
			KEY customer_id (customer_id),
			KEY email (email),
			KEY status (status),
			KEY idx_email_status (email, status),
			KEY date_abandoned (date_abandoned),
			KEY date_expired (date_expired),
			KEY cart_version (cart_version),
			KEY order_id (order_id)
		";
	}
}
