<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index(): string
    {
        return view('welcome_message');
    }

    public  function promoIndex($promo,$promoOwner=''){

         $httpHost  = $_SERVER['HTTP_HOST'];

        if ( $httpHost == 'https://promo.wrenchboard.com' || $httpHost == 'promo.wrenchboard.com' ){
            $_SERVER['CI_ENV'] = 'production';
            $_SERVER['CI_ENVIRONMENT'] = 'production';
        }

        $in["promo"] = $promo;
        $in["promo_owner"] = $promoOwner;
        $in["host_server"] = $httpHost;
        $out  = $this->APIcall('POST', $this->wrenchApiServer(). 'start',$in);
        if ( !isset($out) || !isset($out["status_message"]) ||  $out["status_message"] != "VALID_LINK_FOUND"){
            return redirect()->to($out["user_server_name"]);
        }

        $in["owner_uid"] = $out["owner_uid"]; //we need this to load jobs
        $out["task"]  = $this->APIcall('POST', $this->wrenchApiServer(). 'tasks',$in)["result_list"];
        return view('welcome_message', $out);
    }
}
