<?php

namespace App\Controllers\Client;

use App\Controllers\BaseController;
use App\Models\ClientModel;

class AuthController extends BaseController
{
    public function showLogin()
    {
        return view('client/auth/login');
    }

    public function login()
    {
        $model = new ClientModel();
        $email = $this->request->getPost('email');
        $password = $this->request->getPost('password');

        $client = $model->where('email', $email)->first();

        if (! $client || ! password_verify($password, $client['password'])) {
            return redirect()->back()->with('error', lang('App.login_failed'));
        }

        session()->set([
            'client_id' => $client['id'],
            'client_name' => $client['name'],
            'client_company_id' => $client['company_id'],
        ]);

        return redirect()->to('/client/dashboard');
    }

    public function logout()
    {
        session()->remove(['client_id', 'client_name', 'client_company_id']);
        return redirect()->to('/client/login');
    }
}
