<?php
/**
 * Handles the creation of the Advocates 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 MembersTable
 *
 * This class creates the `rewardswp_members` table.
 *
 * @since 1.0.0
 */
class MembersTable extends AbstractDatabaseTable {
	/**
	 * Returns the name of the members table (without prefix).
	 *
	 * @since 1.0.0
	 *
	 * @return string Table name.
	 */
	public function get_table_name(): string {
		return 'rewardswp_members';
	}

	/**
	 * Returns the name of the members meta table (without prefix).
	 *
	 * @since 1.0.0
	 * @return string Table name.
	 */
	public function get_meta_table_name(): string {
		return 'rewardswp_membermeta';
	}

	/**
	 * Returns the name of the column used to create the relationship between the main table and the meta table.
	 *
	 * @since 1.0.0
	 *
	 * @return string Column name.
	 */
	public function get_meta_table_relationship_field_name(): string {
		return 'member_id';
	}

	/**
	 * Retrieve the columns and labels.
	 *
	 * @since 1.0.0
	 * @return array
	 */
	public function get_columns(): array {
		return [
			'id'                  => __( 'ID', 'rewardswp' ),
			'user_id'             => __( 'User ID', 'rewardswp' ),
			'referral_code'       => __( 'Referral Code', 'rewardswp' ),
			'email'               => __( 'Email', 'rewardswp' ),
			'first_name'          => __( 'First Name', 'rewardswp' ),
			'last_name'           => __( 'Last Name', 'rewardswp' ),
			'total_referrals'     => __( 'Total Referrals', 'rewardswp' ),
			'completed_referrals' => __( 'Completed Referrals', 'rewardswp' ),
			'points_balance'      => __( 'Points Balance', 'rewardswp' ),
			'status'              => __( 'Status', 'rewardswp' ),
			'token'               => __( 'Access Token', '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 {
		$this->db_delta(
			sprintf(
				"CREATE TABLE %s (
					id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
					user_id bigint(20) unsigned NULL,
					email varchar(255) NOT NULL,
					first_name varchar(255) DEFAULT '',
					last_name varchar(255) DEFAULT '',
					referral_code varchar(20) NOT NULL,
					total_referrals int(11) NOT NULL DEFAULT 0,
					completed_referrals int(11) NOT NULL DEFAULT 0,
					points_balance int(11) NOT NULL DEFAULT 0,
					status varchar(20) NOT NULL DEFAULT 'active',
					token varchar(64) DEFAULT NULL,
					created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP(),
					PRIMARY KEY (id),
					UNIQUE KEY user_id (user_id),
					UNIQUE KEY email (email),
					UNIQUE KEY referral_code (referral_code),
					UNIQUE KEY token (token) ) %s;",
				$this->get_prefixed_table_name(),
				$this->global_config->get_db()->get_charset_collate()
			)
		);

		// Create the meta table for this model.
		$this->create_meta_table();
	}
}
