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

namespace Awesomemotive\Rewardswp\Database\Tables;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractDatabaseTable;

/**
 * Class NotificationsTable
 *
 * This class creates the `rewardswp_notifications` table.
 *
 * @since 1.1.0
 */
class NotificationsTable extends AbstractDatabaseTable {
	/**
	 * Returns the name of the notifications table (without prefix).
	 *
	 * @since 1.1.0
	 * @return string Table name.
	 */
	public function get_table_name(): string {
		return 'rewardswp_notifications';
	}

	/**
	 * Notifications table does not have a meta table.
	 *
	 * @since 1.1.0
	 *
	 * @return string Empty string - no meta table.
	 */
	public function get_meta_table_name(): string {
		return '';
	}

	/**
	 * Notifications table does not have a meta table.
	 *
	 * @since 1.1.0
	 *
	 * @return string Empty string - no meta table.
	 */
	public function get_meta_table_relationship_field_name(): string {
		return '';
	}

	/**
	 * Retrieve the columns and labels.
	 *
	 * @since 1.1.0
	 * @return array
	 */
	public function get_columns(): array {
		return [
			'id'         => esc_html__( 'ID', 'rewardswp' ),
			'remote_id'  => esc_html__( 'Remote ID', 'rewardswp' ),
			'title'      => esc_html__( 'Title', 'rewardswp' ),
			'content'    => esc_html__( 'Content', 'rewardswp' ),
			'buttons'    => esc_html__( 'Buttons', 'rewardswp' ),
			'type'       => esc_html__( 'Type', 'rewardswp' ),
			'conditions' => esc_html__( 'Conditions', 'rewardswp' ),
			'start'      => esc_html__( 'Start Date', 'rewardswp' ),
			'end'        => esc_html__( 'End Date', 'rewardswp' ),
			'dismissed'  => esc_html__( 'Dismissed', 'rewardswp' ),
			'created_at' => esc_html__( 'Created At', 'rewardswp' ),
			'updated_at' => esc_html__( 'Updated At', 'rewardswp' ),
		];
	}

	/**
	 * Creates the `rewardswp_notifications` table in the database.
	 *
	 * @since 1.1.0
	 * @return void
	 */
	public function create_or_update_table(): void {
		$this->db_delta(
			sprintf(
				"CREATE TABLE %s (
				id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
				remote_id varchar(255) NOT NULL,
				title text NOT NULL,
				content longtext NOT NULL,
				buttons longtext DEFAULT NULL,
				type varchar(64) DEFAULT 'success',
				conditions longtext DEFAULT NULL,
				start datetime DEFAULT NULL,
				end datetime DEFAULT NULL,
				dismissed tinyint(1) UNSIGNED NOT NULL DEFAULT 0,
				created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP(),
				updated_at datetime DEFAULT NULL,
				PRIMARY KEY (id),
				UNIQUE KEY remote_id (remote_id),
				KEY dismissed_dates (dismissed, start, end) ) %s;",
				$this->get_prefixed_table_name(),
				$this->global_config->get_db()->get_charset_collate()
			)
		);

		// Notifications table does not have a meta table.
	}
}
