<?php
declare( strict_types = 1 );

use App\Controller\Page\ActorPageController;
use App\Controller\Page\ActorsPageController;
use App\Controller\Page\AlbumPageController;
use App\Controller\Page\ArtistPageController;
use App\Controller\Page\BulletinPageController;
use App\Controller\Page\FilmPageController;
use App\Controller\Page\FilmsPageController;
use App\Controller\Page\GamePageController;
use App\Controller\Page\GamesPageController;
use App\Controller\Page\HomePageController;
use App\Controller\Page\LocationPageController;
use App\Controller\Page\NewsPageController;
use App\Controller\Page\PrivacyPolicyPageController;
use App\Controller\Page\ProducerPageController;
use App\Controller\Page\ProducersPageController;
use App\Controller\Page\SeriesesPageController;
use App\Controller\Page\SeriesPageController;
use App\Controller\Page\SystemPageController;
use App\Controller\Page\TunePageController;
use App\Controller\RobotsTxtController;
use App\Controller\SitemapXmlController;
use App\Util\AppServices;
use GRoute\RequestMethodId;
use GRoute\Route;
use GRoute\Router;
use Utils\ErrorHandler;


require __DIR__ . '/../vendor/autoload.php';  //Always check relative paths on this if not using router.

//ray()->clearScreen();
$appServices = new  AppServices();
$appContext = $appServices->getAppContext();
$errorHandler = new ErrorHandler( !$appContext::IS_LIVE, $appContext::IS_ERROR_LOG_STACK_TRACE_ON, $appContext::ERROR_LOG_FILE );

set_error_handler( [ $errorHandler, 'errorHandler' ] );
set_exception_handler( [ $errorHandler, 'exceptionHandler' ] );
register_shutdown_function( [ $errorHandler, 'fatalErrorShutdownHandler' ] );



$router = new Router();

$router->onMatch( '/', RequestMethodId::GET, fn( $route ) => new HomePageController( $appServices )->main( $route ) );
$router->onMatch( '/page/{page}', RequestMethodId::GET, fn( $route ) => new HomePageController( $appServices )->main( $route ) );
$router->onMatch( '/news/{newsTitle}', RequestMethodId::GET, fn( $route ) => new NewsPageController( $appServices )->main( $route ) );
$router->onMatch( '/film/{filmTitle}', RequestMethodId::GET, fn( $route ) => new FilmPageController( $appServices )->main( $route ) );
$router->onMatch( '/film', RequestMethodId::GET, fn( $route ) => new FilmsPageController( $appServices )->allFilms( $route ) );
$router->onMatch( '/films/year/{seasonStartYear}', RequestMethodId::GET, fn( $route ) => new FilmsPageController( $appServices )->allFilms( $route ) );
$router->onMatch( '/' . $appContext::ACTIVITY_VERB . '/films-free', RequestMethodId::GET, fn( $route ) => new FilmsPageController( $appServices )->filmsFree( $route ) );
$router->onMatch( '/' . $appContext::ACTIVITY_VERB . '/films-teaser-trailers', RequestMethodId::GET, fn( $route ) => new FilmsPageController( $appServices )->filmsWithTeasers( $route ) );
$router->onMatch( '/' . $appContext::ACTIVITY_VERB . '/film-full-parts', RequestMethodId::GET, fn( $route ) => new FilmsPageController( $appServices )->filmsWithFullParts( $route ) );
$router->onMatch( '/' . $appContext::ACTIVITY_VERB . '/films-in-4k', RequestMethodId::GET, fn( $route ) => new FilmsPageController( $appServices )->filmsIn4K( $route ) );
$router->onMatch( '/' . $appContext::ACTIVITY_VERB . '/films-in-hd', RequestMethodId::GET, fn( $route ) => new FilmsPageController( $appServices )->filmsInHd( $route ) );
$router->onMatch( '/' . $appContext::ACTIVITY_VERB . '/films-on-bluray', RequestMethodId::GET, fn( $route ) => new FilmsPageController( $appServices )->filmsOnBluRay( $route ) );
$router->onMatch( '/' . $appContext::ACTIVITY_VERB . '/films-on-netflix', RequestMethodId::GET, fn( $route ) => new FilmsPageController( $appServices )->filmsOnNetflix( $route ) );

$router->onMatch( '/bulletin/{bulletinTitle}', RequestMethodId::GET, fn( $route ) => new BulletinPageController( $appServices )->main( $route ) );

$router->onMatch( '/producer', RequestMethodId::GET, fn( $route ) => new ProducersPageController( $appServices )->allProducers( $route ) );
$router->onMatch( '/producer/{producerTitle}', RequestMethodId::GET, fn( $route ) => new ProducerPageController( $appServices )->main( $route ) );

$router->onMatch( '/location/{locationTitle}', RequestMethodId::GET, fn( $route ) => new LocationPageController( $appServices )->main( $route ) );

$router->onMatch( '/tune/{tuneTitle}', RequestMethodId::GET, fn( $route ) => new TunePageController( $appServices )->main( $route ) );
$router->onMatch( '/album/{albumTitle}', RequestMethodId::GET, fn( $route ) => new AlbumPageController( $appServices )->main( $route ) );
$router->onMatch( '/artist/{artistTitle}', RequestMethodId::GET, fn( $route ) => new ArtistPageController( $appServices )->main( $route ) );


$router->onMatch( '/series', RequestMethodId::GET, fn( $route ) => new SeriesesPageController( $appServices )->allSeries( $route ) );
$router->onMatch( '/series/{seriesTitle}', RequestMethodId::GET, fn( $route ) => new SeriesPageController( $appServices )->main( $route ) );
$router->onMatch( '/series/{seriesTitle}/{episodeId}', RequestMethodId::GET, fn( $route ) => new SeriesPageController( $appServices )->main( $route ) );


$router->onMatch( '/game/{gameTitle}', RequestMethodId::GET, fn( $route ) => new GamePageController( $appServices )->main( $route ) );
$router->onMatch( '/game', RequestMethodId::GET, fn( $route ) => new GamesPageController( $appServices )->allGames( $route ) );


$router->onMatch( '/' . $appContext::ACTOR_NOUN . '/{actorName}', RequestMethodId::GET, fn( $route ) => new ActorPageController( $appServices )->main( $route ) );


$router->onMatch( '/privacy-policy', RequestMethodId::GET, fn( $route ) => new PrivacyPolicyPageController( $appServices )->main( $route ) );

//
// All possible cor search phrases?  Improve this?  (array of param names?)
//

$router->onMatch( '/' . $appContext::ACTOR_NOUN, RequestMethodId::GET, fn( $route ) => new ActorsPageController( $appServices )->allActors( $route ) );
$router->onMatch( '/' . $appContext::ACTOR_NOUN . '/page/{page}', RequestMethodId::GET, fn( $route ) => new ActorsPageController( $appServices )->allActors( $route ) );

$router->onMatch( '/' . $appContext::ACTOR_NOUN . '/year-active/{yearActive}', RequestMethodId::GET, fn( $route ) => new ActorsPageController( $appServices )->allActors( $route ) );
$router->onMatch( '/' . $appContext::ACTOR_NOUN . '/name/{actorNameQuery}', RequestMethodId::GET, fn( $route ) => new ActorsPageController( $appServices )->allActors( $route ) );
$router->onMatch( '/' . $appContext::ACTOR_NOUN . '/year-active/{yearActive}/name/{actorNameQuery}', RequestMethodId::GET, fn( $route ) => new ActorsPageController( $appServices )->allActors( $route ) );

$router->onMatch( '/' . $appContext::ACTOR_NOUN . '/year-active/{yearActive}/page/{page}', RequestMethodId::GET, fn( $route ) => new ActorsPageController( $appServices )->allActors( $route ) );
$router->onMatch( '/' . $appContext::ACTOR_NOUN . '/name/{actorNameQuery}/page/{page}', RequestMethodId::GET, fn( $route ) => new ActorsPageController( $appServices )->allActors( $route ) );
$router->onMatch( '/' . $appContext::ACTOR_NOUN . '/year-active/{yearActive}/name/{actorNameQuery}/page/{page}', RequestMethodId::GET, fn( $route ) => new ActorsPageController( $appServices )->allActors( $route ) );


$router->onMatch( '/sitemap.xml', RequestMethodId::GET, fn( $route ) => new SitemapXmlController( $appServices )->getDoc( ) );
$router->onMatch( '/robots.txt', RequestMethodId::GET, fn( $route ) => new RobotsTxtController( $appServices )->getDoc( ) );


//
// 404 for all else
//
//$router->onMatch( new Route( Route::TOKEN_MATCH_ALL ), fn( $route ) => ( new SystemPageController( $appServices ) )->exit404Page(  ) );

$router->onMatch( Route::TOKEN_MATCH_ALL, RequestMethodId::ANY, fn( $route ) => new SystemPageController( $appServices )->exit404Page(  ) );