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

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

	/**
	 * 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 'referral_id';
	}

	/**
	 * Retrieve the columns and labels.
	 *
	 * @since 1.0.0
	 * @return array
	 */
	public function get_columns(): array {
		return [
			'id'               => esc_html__( 'ID', 'rewardswp' ),
			'advocate_id'      => esc_html__( 'Advocate ID', 'rewardswp' ),
			'event_id'         => esc_html__( 'Event ID', 'rewardswp' ),
			'referred_user_id' => esc_html__( 'Referred User ID', 'rewardswp' ),
			'referred_email'   => esc_html__( 'Referred Email', 'rewardswp' ),
			'status'           => esc_html__( 'Status', 'rewardswp' ),
			'order_id'         => esc_html__( 'Order ID', 'rewardswp' ),
			'type'             => esc_html__( 'Type', 'rewardswp' ),
			'source'           => esc_html__( 'Source', 'rewardswp' ),
			'created_at'       => esc_html__( 'Created At', 'rewardswp' ),
			'converted_at'     => esc_html__( 'Converted 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,
                advocate_id bigint(20) unsigned NOT NULL,
                event_id bigint(20) unsigned NOT NULL,
                referred_user_id bigint(20) unsigned NULL,
                referred_email varchar(255) DEFAULT NULL,
                status varchar(20) NOT NULL,
                order_id bigint(20) unsigned DEFAULT NULL,
                type varchar(50) DEFAULT '',
                source varchar(50) DEFAULT 'unknown',
                created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP(),
                converted_at datetime DEFAULT NULL,
                PRIMARY KEY (id),
                KEY advocate_id (advocate_id),
                KEY referred_user_id (referred_user_id) ) %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();
	}
}
