<?php

namespace App\Http\Controllers;

use Helper;
use App\Models\Device;
use App\Models\Package;
use Illuminate\Support\Facades\Artisan;

class WelcomeController extends Controller
{
  public function index()
  {
    if (Helper::isEx()) {
      $packages = Package::select('name', 'rate_monthly', 'max_outgoing', 'max_device', 'max_phonebook', 'max_autoreply', 'global_senders', 'webhook')
        ->where('name', '<>', 'super')
        ->where('name', '<>', 'trial')
        ->get();
    } else {
      $packages = null;
    }

    if (!empty(Helper::setting('landingPage'))) {
      if (filter_var(filter_var(Helper::setting('landingPage'), FILTER_SANITIZE_URL), FILTER_VALIDATE_URL)) {
        return redirect()->to(filter_var(Helper::setting('landingPage'), FILTER_SANITIZE_URL));
      } else if (file_exists(public_path(Helper::setting('landingPage')))) {
        $base64 = base64_encode($packages);
        return redirect()->to(
          url(
            Helper::setting('landingPage') .
              '?packages=' .
              $base64 .
              '&currencyCode=' .
              Helper::setting('currencyCode') .
              '&decimals=' .
              Helper::setting('nfDecimals') .
              '&decimalPoint=' .
              Helper::setting('nfDecimalPoint') .
              '&separator=' .
              Helper::setting('nfSeparator')
          )
        );
      } else if (Helper::setting('landingPage') === 'login') {
        if (empty(auth()->user()->id)) {
          return view('auth/login');
        } else {
          return redirect()->route('user.show');
        }
      } else {
        return view('/welcome', compact('packages'));
      }
    } else {
      return view('/welcome', compact('packages'));
    }
  }
}
?>
