<?php
/**
 * app/controllers/home.php
 *
 * This is the home or 'index' controller.
 *
 * @version 3.0
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject\Controllers;

use TheTempusProject\Hermes\Functions\Redirect;
use TheTempusProject\Bedrock\Functions\Session;
use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Hermes\Functions\Route as Routes;
use TheTempusProject\Houdini\Classes\Issues;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Houdini\Classes\Template;
use TheTempusProject\Classes\Controller;
use TheTempusProject\Classes\Forms;
use TheTempusProject\TheTempusProject as App;

class Home extends Controller {
    public function index() {
        self::$title = '{SITENAME}';
        self::$pageDescription = 'Joey Kimsey is a web app developer with nearly a decade of professional experience and another decade of hands on experience.';
        $optionValues = [
            (object) [ "post" => "python-dev", "option" => "Python Developer" ],
            (object) [ "post" => "ecommerce-coder", "option" => "eCommerce Coder" ],
            (object) [ "post" => "php-experience", "option" => "PHP Developer" ],
            (object) [ "post" => "ai-experience", "option" => "GPT-Agent" ],
            (object) [ "post" => "operations-experience", "option" => "DevOps Engineer" ],
            (object) [ "post" => "lua-dev", "option" => "Lua Developer" ],
            (object) [ "post" => "version-control", "option" => "Git Goblin" ],
            (object) [ "post" => "laravel-experience", "option" => "Laravel Developer" ],
            (object) [ "post" => "database-experience", "option" => "Database Admin" ],
            (object) [ "post" => "education", "option" => "Student" ],
            (object) [ "post" => "full-stack-developer", "option" => "Full-Stack Developer" ],
            (object) [ "post" => "stripe-certified-developer", "option" => "Stripe Certified Developer" ],

        ];
        shuffle($optionValues);
        Views::view( 'index', $optionValues );
    }

    public function login() {
        self::$title = 'Portal - {SITENAME}';
        self::$pageDescription = 'Please log in to access all of the great features {SITENAME} has to offer.';
        if ( App::$isLoggedIn ) {
            return Issues::add( 'notice', 'You are already logged in. Please <a href="' . Routes::getAddress() . 'home/logout">click here</a> to log out.' );
        }
        if ( !Input::exists() ) {
            return Views::view( 'login' );
        }
        if ( !Forms::check( 'login' ) ) {
            Issues::add( 'error', [ 'There was an error with your login.' => Check::userErrors() ] );
            return Views::view( 'login' );
        }
        if ( !self::$user->logIn( Input::post( 'username' ), Input::post( 'password' ), Input::post( 'remember' ) ) ) {
            Issues::add( 'error', 'Username or password was incorrect.' );
            return Views::view( 'login' );
        }
        Session::flash( 'success', 'You have been logged in.' );
        if ( Input::exists( 'rurl' ) ) {
            Redirect::to( Input::post( 'rurl' ) );
        } else {
            Redirect::to( 'home/index' );
        }
    }

    public function logout() {
        self::$title = 'Log Out - {SITENAME}';
        Template::noIndex();
        if ( !App::$isLoggedIn ) {
            return Issues::add( 'notice', 'You are not logged in.' );
        }
        self::$user->logOut();
        Session::flash( 'success', 'You have been logged out.' );
        Redirect::to( 'home/index' );
    }

    public function profile( $id = null ) {
        self::$title = 'User Profile - {SITENAME}';
        self::$pageDescription = 'User Profile - {SITENAME}';
        if ( !App::$isLoggedIn ) {
            return Issues::add( 'notice', 'You must be logged in to view this page.' );
        }
        $user = self::$user->get( $id );
        if ( !$user ) {
            return Issues::add( 'notice', 'No user found.' );
        }
        self::$title = $user->username . '\'s Profile - {SITENAME}';
        self::$pageDescription = 'User Profile for ' . $user->username . ' - {SITENAME}';
        Views::view( 'profile', $user );
    }

    public function terms() {
        self::$title = 'Terms and Conditions - {SITENAME}';
        self::$pageDescription = '{SITENAME} Terms and Conditions of use. Please use {SITENAME} safely.';
        Components::set( 'TERMS', Views::simpleView( 'terms' ) );
        Views::view( 'termsPage' );
    }

    public function about() {
        self::$title = 'About - {SITENAME}';
        self::$pageDescription = 'Just a bit more info on me.';
        Views::view( 'about' );
    }

    public function hire() {
        self::$title = 'Consulting and Freelance - {SITENAME}';
        self::$pageDescription = 'More details on how to hire me for consulting or freelance work.';
        Views::view( 'hire' );
    }
}
