<?php


namespace Config;

use App\Filters\JWTAuthenticationFilter;
use CodeIgniter\Config\BaseConfig;
use CodeIgniter\Filters\CSRF;
use CodeIgniter\Filters\DebugToolbar;
use CodeIgniter\Filters\Honeypot;
use App\Utils\SystemGateways;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;

// use CodeIgniter\Filters\JWTAuthenticationFilter;
class Filters extends BaseConfig {
	// Makes reading things below nicer,
	// and simpler to change out script that's used.
	public $aliases = [ 
		'csrf' => CSRF::class,
		'toolbar' => DebugToolbar::class,
		'honeypot' => Honeypot::class,
		'auth' => JWTAuthenticationFilter::class
	];

	// Always applied before every request
	public $globals = [ 
		'before' => [ // 'honeypot'
		// 'csrf',
		],
		'after' => [ 
			'toolbar'
			// 'honeypot'
		]
	];

	// Works on all of a particular HTTP method
	// (GET, POST, etc) as BEFORE filters only
	// like: 'post' => ['CSRF', 'throttle'],
	public $methods = [ ];

	// List filter aliases and any before/after uri patterns
	// that they should run on, like:
	// 'isLoggedIn' => ['before' => ['account/*', 'profiles/*']],
	public $filters = [ 
		'auth' => [ 
			'before' => [ ]
		]
	];

	public function __construct() {

		parent::__construct();
		$routes = SystemGateways::routes;
		$resources = array_unique( array_map( function (array $route) {
			return explode( '/', $route ['url'] ) [0];
		}, $routes ) );
		foreach ( $resources as $resource ) {
			if ($resource !== 'auth' && $resource !== 'cep' && $resource !== 'pagseguro-webhook') {
				$this->filters ['auth'] ['before'] [] = "$resource/*";
				$this->filters ['auth'] ['before'] [] = "$resource";
			}
		}

	}

}
