<?php
/**
 * @brief		Achievement Action Extension
 * @author		<a href='https://www.scriptz-team'>Invision Power Services, Inc.</a>
 * @copyright	(c) Invision Power Services, Inc.
 * @license		https://www.scriptz-team/legal/standards/
{subpackage}
 * @since		{date}
 */

namespace IPS\{app}\extensions\core\AchievementAction;

/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
	header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
	exit;
}

/**
 * Achievement Action Extension
 */
class _{class} extends \IPS\core\Achievements\Actions\AbstractAchievementAction // NOTE: Other classes exist to provided bases for common situations, like where node-based filters will be required
{	
	/**
	 * Get filter form elements
	 *
	 * @param	array|NULL		$filters	Current filter values (if editing)
	 * @param	\IPS\Http\Url	$url		The URL the form is being shown on
	 * @return	array
	 */
	public function filters( ?array $filters, \IPS\Http\Url $url ): array
	{
		return [];
	}
	
	/**
	 * Format filter form values
	 *
	 * @param	array	$values	The values from the form
	 * @return	array
	 */
	public function formatFilterValues( array $values ): array
	{
		return [];
	}
	
	/**
	 * Work out if the filters applies for a given action
	 *
	 * Important note for milestones: consider the context. This method is called by \IPS\Member::achievementAction(). If your code 
	 * calls that BEFORE making its change in the database (or there is read/write separation), you will need to add
	 * 1 to the value being considered for milestones
	 *
	 * @param	\IPS\Member	$subject	The subject member
	 * @param	array		$filters	The value returned by formatFilterValues()
	 * @param	mixed		$extra		Any additional information about what is happening (e.g. if a post is being made: the post object)
	 * @return	bool
	 */
	public function filtersMatch( \IPS\Member $subject, array $filters, $extra = NULL ): bool
	{
		return TRUE;
	}
	
	/**
    	 * Get the labels for the people this action might give awards to
    	 *
    	 * @param	array|NULL		$filters	Current filter values
    	 *
    	 * @return	array
    	 */
    //	public function awardOptions( ?array $filters ): array
	// {
	// 	return [
	// 		'subject'	=> 'achievement_filter_{class}_receiver',
	// 		'other'		=> 'achievement_filter_{class}_giver'
	// 	];
	// }
	
	/**
     * Get the "other" people we need to award =stuff to
     *
	 * @param	array|NULL	$filters	Current filter values
	 * @param	mixed		$extra		Any additional information about what is happening (e.g. if a post is being made: the post object)
     * @return	array
     */
    // public function awardOther(?array $filters, $extra = NULL ): array
	// {
	// 	return [ \IPS\Member::loggedIn() ];
	// }
	
	/**
	 * Get identifier to prevent the member being awarded points for the same action twice
	 * Must be unique within within of this domain, must not exceed 32 chars.
	 *
	 * @param	\IPS\Member	$subject	The subject member
	 * @param	mixed		$extra		Any additional information about what is happening (e.g. if a post is being made: the post object)
	 * @return	string
	 */
	public function identifier( \IPS\Member $subject, $extra = NULL ): string
	{
		return (string) $extra->id;
	}
	
	/**
	 * Return a description for this action to show in the log
	 *
	 * @param	string	$identifier	The identifier as returned by identifier()
	 * @param	array	$actor		If the member was the "subject", "other", or both
	 * @return	string
	 */
	public function logRow( string $identifier, array $actor ): string
	{
		return "{class}";
	}
	
	/**
	 * Get "description" for rule
	 *
	 * @param	\IPS\core\Achievements\Rule	$rule	The rule
	 * @return	string|NULL
	 */
	public function ruleDescription( \IPS\core\Achievements\Rule $rule ): ?string
	{
		return "{class}";
	}

	/**
	 * Get rebuild data
	 *
	 * @return	array
	 */
	static public function rebuildData()
	{
		// This method returns an array of tables that this extension uses to award achievements, so for example
		// the follow extension uses 'core_follow'.
		// We also need to know the primary key for this table. It should be unique but does not need to be an integer
		// The date is optional but useful if available for those that want to partially rebuild.
		// Where shouldn't attempt to limit based on filters or milestones, but just remove data that you don't need to 
		// look at, such as banned members, archived items and so on. You can return an empty array if you do not need
		// a where statement.
		//return [ [
		//	'table' => 'table_name',
		//	'pkey'  => 'field_id',
		//	'date'  => 'field_date',
		//	'where' => [ [ 'banned != 1' ] ],
		//] ];
	}

	/**
	 * Process the rebuild row
	 *
	 * @param array		$row	Row from database
	 * @param array		$data	Data collected when starting rebuild [table, pkey...]
	 * @return void
	 */
	public static function rebuildRow( $row, $data )
	{
		// This method runs the achievementAction on the table row from the table (or tables) you specified in rebuildData()
		// You are welcome to load any classes, etc you need here.
		//IPS\Member::loggedIn()->achievementAction( '{app}', '{class}', $row['field_id'] ), \IPS\DateTime::ts( $row['field_date'] ) );
	}

}