<?php
/**
 * app/classes/database_model.php
 *
 * This is the main TempusProject database model.
 *
 * @version 3.0
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject\Classes;

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

class DatabaseModel extends BedrockDatabaseModel {
    public $preferenceMatrix;
    public $permissionMatrix;
    public static $installFlags = MODEL_INSTALL_FLAGS;
    protected static $user;
    protected static $log;
    protected static $group;
    protected static $session;
    protected static $message;
    protected static $routes;

    public function __construct() {
        parent::__construct();
    }

    public function uninstall() {
        Debug::log( 'Uninstalling Model: ' . get_class($this) );

        parent::uninstall();
        $this->uninstallPreferences();
        $this->uninstallPermissions();
        return true;
    }

    public function installPreferences() {
        if ( empty( $this->preferenceMatrix ) ) {
            Debug::log( 'preferenceMatrix is empty' );
            return true;
        }
        $prefs = new Preferences();
        foreach ( $this->preferenceMatrix as $name => $details ) {
            $prefs->add( $name, $details );
        }
        return $prefs->save( true );
    }

    public function uninstallPreferences() {
        if ( empty( $this->preferenceMatrix ) ) {
            Debug::log( 'preferenceMatrix is empty' );
            return true;
        }
        $prefs = new Preferences();
        foreach ( $this->preferenceMatrix as $name => $details ) {
            $prefs->remove( $name, true );
        }
    }

    public function installPermissions() {
        if ( empty( $this->permissionMatrix ) ) {
            Debug::log( 'permissionMatrix is empty' );
            return true;
        }
        $perms = new Permissions();
        foreach ( $this->permissionMatrix as $name => $details ) {
            $perms->add( $name, $details );
        }
        return $perms->save( true );
    }

    public function uninstallPermissions() {
        $perms = new Permissions();
        if ( empty( $this->permissionMatrix ) ) {
            Debug::log( 'permissionMatrix is empty' );
            return true;
        }
        foreach ( $this->permissionMatrix as $name => $details ) {
            $perms->remove( $name, true );
        }
    }

    public function delete( $idArray ) {
        if ( empty( self::$log ) ) {
            self::$log = new Log;
        }
        if ( !is_array( $idArray ) ) {
            $idArray = [ $idArray ];
        }
        foreach ( $idArray as $id ) {
            if ( !Check::id( $id ) ) {
                Debug::info( "invalid ID: $id." );
                $error = true;
                continue;
            }
            $result = parent::delete( $id );
            if ( true !== $result ) {
                Debug::info( ucfirst( $this->tableName ) . " did not delete properly: $id" );
                $error = true;
                continue;
            }
            self::$log->admin( 'Deleted ' . ucfirst( $this->tableName ) . ": $id" );
            Debug::info( ucfirst( $this->tableName ) . " successfully deleted: $id" );
        }
        if ( !empty( $error ) ) {
            Debug::error( 'One or more rows were not deleted.' );
            return false;
        }
        return true;
    }

    public function install( $options ) {
        Debug::log( 'Installing Database Model');
        $module_data = [];
        $errors = [];
        foreach ( self::$installFlags as $flag_name ) {
            if ( empty( $options[$flag_name] ) ) {
                $module_data[ $flag_name ] = INSTALL_STATUS_SKIPPED;
                continue;
            }

            $result = $this->$flag_name();

            if ( empty( $result ) ) {
                $errors[] = ['errorInfo' => get_class($this) . " Failed to execute $flag_name properly."];
                $module_data[ $flag_name ] = INSTALL_STATUS_FAIL;
                continue;
            }

            if ( 'installResources' === $flag_name ) {
                $module_data['installedResources'] = $result;
            }

            $module_data[ $flag_name ] = INSTALL_STATUS_SUCCESS;
            continue;
        }
        return [ $module_data, $errors ];
    }
}
