<?php
/**
 * Handles the creation of the Events table in the database.
 *
 * @package Awesomemotive\Rewardswp\Database\Tables
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Database\Tables;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractDatabaseTable;

/**
 * Class VisitsTable
 *
 * This class creates the `rewardswp_events` table.
 *
 * @since 1.0.0
 */
class EventsTable extends AbstractDatabaseTable {
	/**
	 * Returns the name of the events table (without prefix).
	 *
	 * @since 1.0.0
	 * @return string Table name.
	 */
	public function get_table_name(): string {
		return 'rewardswp_events';
	}

	/**
	 * Retrieve the columns and labels.
	 *
	 * @since 1.0.0
	 * @return array
	 */
	public function get_columns(): array {
		return [
			'id'           => __( 'ID', 'rewardswp' ),
			'member_id'    => __( 'Member ID', 'rewardswp' ),
			'type'         => __( 'Type', 'rewardswp' ),
			'context'      => __( 'Context', 'rewardswp' ),
			'ip_address'   => __( 'IP Address', 'rewardswp' ),
			'referrer_url' => __( 'Referrer URL', 'rewardswp' ),
			'landing_url'  => __( 'Landing URL', 'rewardswp' ),
			'created_at'   => __( 'Created At', 'rewardswp' ),
		];
	}


	/**
	 * Creates the `rewardswp_advocates` table in the database.
	 *
	 * @since 1.0.0
	 * @return void
	 */
	public function create_or_update_table(): void {
		// Creates or updates the table.
		$this->db_delta(
			sprintf(
				'CREATE TABLE %s (
                    id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
                    member_id bigint(20) unsigned NULL,
                    type varchar(50) NOT NULL,
                    context varchar(50) DEFAULT NULL,
                    ip_address varchar(45) DEFAULT NULL,
                    referrer_url text DEFAULT NULL,
                    landing_url text DEFAULT NULL,
                    created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP(),
                    PRIMARY KEY (id),
                    KEY type (type),
                    KEY context (context),
                    KEY type_context (type, context),
                    KEY member_id (member_id),
                    KEY created_at (created_at)
                ) %s;',
				$this->get_prefixed_table_name(),
				$this->global_config->get_db()->get_charset_collate()
			)
		);
	}
}
