<?php
/**
 * app/models/routes.php
 *
 * This class is used for the manipulation of the routes database table.
 *
 * @version 3.0
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject\Models;

use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Canary\Bin\Canary as Debug;
use TheTempusProject\Classes\DatabaseModel;

class Routes extends DatabaseModel {
    public $tableName = 'routes';
    public $modelVersion = '1.0';
    public $databaseMatrix = [
        [ 'nickname', 'varchar', '32' ],
        [ 'redirect_type', 'varchar', '32' ],
        [ 'original_url', 'varchar', '32' ],
        [ 'forwarded_url', 'text', '' ],
    ];
    public $resourceMatrix = [
        [
            'original_url' => 'fb',
            'redirect_type' => 'external',
            'nickname' => 'Facebook',
            'forwarded_url' => 'https://www.facebook.com/thetempusproject',
        ],
        [
            'original_url' => 'twitter',
            'redirect_type' => 'external',
            'nickname' => 'Twitter',
            'forwarded_url' => 'https://twitter.com/ProjectTempus',
        ],
        [
            'original_url' => 'in',
            'redirect_type' => 'external',
            'nickname' => 'LinkedIn',
            'forwarded_url' => 'https://www.linkedin.com/company/the-tempus-project/',
        ],
        [
            'original_url' => 'youtube',
            'redirect_type' => 'external',
            'nickname' => 'YouTube',
            'forwarded_url' => 'https://www.youtube.com/channel/UCWy5mgBdvp8-nLJrvhnzC4w',
        ],
        [
            'original_url' => 'git',
            'redirect_type' => 'external',
            'nickname' => 'GitHub',
            'forwarded_url' => 'https://github.com/TheTempusProject',
        ],
    ];
    public $permissionMatrix = [
        'addRoute' => [
            'pretty' => 'Add Custom Routes',
            'default' => false,
        ],
    ];

    public function create( $original_url, $forwarded_url, $nickname = '', $type = 'external' ) {
        if ( empty( self::$log ) ) {
            self::$log = new Log;
        }
        if ( empty( $forwarded_url ) || empty( $original_url ) ) {
            Debug::info( 'Missing some parts' );
            return false;
        }
        if ( !Check::simpleName( $nickname ) ) {
            Debug::warn( 'Invalid route nickname: ' . $nickname );
            return false;
        }
        if ( 'external' == $type && !Check::url( $forwarded_url ) ) {
            Debug::info( 'Routes: illegal forwarded_url.' );
            return false;
        }
        $fields = [
            'nickname' => $nickname,
            'redirect_type' => $type,
            'original_url' => $original_url,
            'forwarded_url' => $forwarded_url,
        ];
        if ( self::$db->insert( $this->tableName, $fields ) ) {
            self::$log->admin( "Created Route: $nickname" );
            return true;
        }
        return false;
    }

    public function update( $id, $original_url, $forwarded_url, $nickname, $type ) {
        if ( empty( self::$log ) ) {
            self::$log = new Log;
        }
        if ( !Check::simpleName( $nickname ) ) {
            Debug::warn( "Invalid route nickname: $name" );
            return false;
        }
        if ( empty( $forwarded_url ) || empty( $original_url ) ) {
            Debug::info( 'Missing some parts' );
            return false;
        }
        if ( 'external' == $type && !Check::url( $forwarded_url ) ) {
            Debug::info( 'Routes: illegal forwarded_url.' );
            return false;
        }
        $fields = [
            'nickname' => $nickname,
            'redirect_type' => $type,
            'original_url' => $original_url,
            'forwarded_url' => $forwarded_url,
        ];
        if ( self::$db->update( $this->tableName, $id, $fields ) ) {
            self::$log->admin( "Updated Route: $id" );
            return true;
        }
        return false;
    }

    public function findByName( $name ) {
        if ( !Check::simpleName( $name ) ) {
            Debug::warn( "Invalid route nickname: $name" );
            return false;
        }
        $routeData = self::$db->get( $this->tableName, [ 'nickname', '=', $name ] );
        if ( !$routeData->count() ) {
            Debug::info( "Routes:findByName: Could not find a route named: $name" );
            return false;
        }
        return $this->filter( $routeData->first() );
    }

    public function findByOriginalUrl( $url ) {
        $routeData = self::$db->get( $this->tableName, [ 'original_url', '=', $url ] );
        if ( !$routeData->count() ) {
            Debug::info( "Routes:findByOriginalUrl: Could not find route by original url: $url" );
            return false;
        }
        return $this->filter( $routeData->first() );
    }

    public function findByforwardedUrl( $url ) {
        if ( !Check::url( $url ) ) {
            Debug::warn( "Routes:findByforwardedUrl: Invalid forwarded_url: $url" );
            return false;
        }
        $routeData = self::$db->get( $this->tableName, [ 'forwarded_url', '=', $url ] );
        if ( !$routeData->count() ) {
            Debug::info( "Routes:findByforwardedUrl: Could not find route by forwarded url: $url" );
            return false;
        }
        return $this->filter( $routeData->first() );
    }
}
