<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\PromoCode;
use Illuminate\Support\Facades\Cookie;
use App\Models\User;

class AppController extends Controller
{


  public function index(Request $request)
  {
    if (!$this->user) return redirect()->to(route('log-in'));
    return redirect()->to(route('admin.dashboard'));
  }
  public function terms(Request $request)
  {
    return view('app.terms');
  }
  public function privacy(Request $request)
  {
    return view('app.privacy');
  }

  public function unsubscribeFromEmails(Request $request, $uid)
  {
    $appInternalName = config('app.internalName');

    $entity = $this->getActorEntityByUid($uid);

    if (!$entity) abort(404);

    return redirect()->to($this->stringMappingConfig['clientUrl']. '/emails/unsubscribe/' . $uid);
  }

  public function unsubscribeFromEmailsSuccess(Request $request)
  {
    $appInternalName = config('app.internalName');

    return view('app.' . $appInternalName . '.unsubscribe-from-emails-success');
  }

  public function processUnsubscribeFromEmails(Request $request)
  {
    $uid = $request->get('uid');
    if (!$uid) return redirect()->back()->with('error', 'Invalid request!');

    $entity = User::where('uid', $uid)->first();

    if (!$entity) return redirect()->back()->with('error', 'Invalid record!');


    //TODO: call java to unsubscribe

    return redirect()->to(route('unsubscribe-from-emails-success'))->with('unsubscribeSuccess', 'You have successfully unsubscribed!');
  }

  public function searchUsers(Request $request)
  {
    $term = $request->input('term') ? trim($request->input('term')) : '';
    if (empty($term)) return '';
    $term = strtolower($term);

    $term2 = '';
    if (strpos($term, ' ') !== FALSE) {
      $terms = explode(' ', $term);
      $term = trim($terms[0]);
      $term2 = trim($terms[1]);
    }
    $users = User::query();

    $users = $users->where(function ($q) use ($term) {
      $q->orWhereRaw('LOWER(name_first::text) LIKE ?', ['%' . $term . '%'])
        ->orWhereRaw('LOWER(name_last::text) LIKE ?', ['%' . $term . '%'])
        ->orWhereRaw('LOWER(full_name::text) LIKE ?', ['%' . $term . '%'])
        ->orWhereRaw('LOWER(email::text) LIKE ?', ['%' . $term . '%'])
        ->orWhereRaw('LOWER(google_login_email::text) LIKE ?', ['%' . $term . '%'])
        ->orWhereRaw('LOWER(facebook_login_email::text) LIKE ?', ['%' . $term . '%']);
    });

    if (!empty($term2)) {
      $users = $users->where(function ($q) use ($term2) {
        $q->orWhereRaw('LOWER(name_first::text) LIKE ?', ['%' . $term2 . '%'])
          ->orWhereRaw('LOWER(name_last::text) LIKE ?', ['%' . $term2 . '%']);
      });
    }

    $users = $users->get();

    $usersJson = $users->map(function ($user) {
      return [
        "uid" => $user->uid,
        "id" => $user->id,
        "text" => $user->full_name ??  $user->name_first . ' ' . $user->name_last,
      ];
    });
    return json_encode([
      "results" => $usersJson
    ]);
  }
}
