<?php

namespace App\Controllers\Mobile\Auth;

use App\Controllers\Mobile\BC;
use CodeIgniter\API\ResponseTrait;
use Predis\Client as PredisClient;

class Logout extends BC
{
    use ResponseTrait;
    public function index()
    {
        $cn = MOBILECN();
        if (!get_cookie($cn)) return redirect()->to('login');
        try {
            if (redison()) {
                $redisConfig = json_decode(options('redis-config'), true);
                $redisClient = new PredisClient([
                    'scheme' => 'tcp',
                    'host'   => $redisConfig['host'],
                    'port'   => $redisConfig['port'],
                    'password' => $redisConfig['password'],
                    'database' => $redisConfig['database'],
                ]);
                $redisClient->connect();
                $redisClient->del("mobile-user-account-" . user()->id);
            }
        } catch (\Throwable $th) {
            //throw $th;
        }
        if (strpos($cn, "rwno") !== false) {
            setcookie($cn, '', time() - 3600, '/'); // empty value and old timestamp
            return view('Panel/Auth/switch-account', ['redirect_url' => base_url()]);
        }
        if (!logged_in())  return redirect()->to('login');
        setLog("Keluar akun");
        setcookie($cn, '', time() - 3600, '/'); // empty value and old timestamp
        return redirect()->to('login');
    }
}
