<?php

namespace App\Filters;

use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;

class AuthFilter implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
        if (! auth()->loggedIn()) {
            session()->setTempdata('beforeLoginUrl', current_url(), 300);
            return redirect()->to('/login')->with('error', 'Silakan login terlebih dahulu.');
        }

        $user    = auth()->user();
        $profile = model('UserProfileModel')->findByUserId($user->id);

        if ($profile && $profile['status'] === 'inactive') {
            auth()->logout();
            return redirect()->to('/login')->with('error', 'Akun Anda tidak aktif. Hubungi administrator.');
        }
    }

    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) {}
}
