<?php

namespace Src\Modules\Auth\Laravel\Controllers;

use App\Providers\AppServiceProvider;
use Illuminate\Support\Facades\View;
use Illuminate\Http\Request;
use Src\Modules\Auth\Interfaces\IFactory as IModuleFactory;

class LoginController {

    private IModuleFactory $moduleFactory;
    
    public function __construct()
    {
        View::addLocation(__DIR__ . '/../views');
        $this->moduleFactory = app()->get(AppServiceProvider::ADMIN_MODULES)->getAuthFactory();
    }

    public function loginForm()
    {
        $page = $this->moduleFactory->getPagesFactory()->getAdminFactory()->creteLoginFormPage();
        return view('admin.loginForm', ['page' => $page]);
    }

    public function loginRequest(Request $request)
    {
        $data = $request->all();
        $domain = $this->moduleFactory->getApplicationFactory()->getAdminFactory()->getDomain();
        $resp = [
            'success' => $domain->login($data),
            'errors' => $domain->getErrors(),
        ];
        return response()->json($resp, $domain->getResponseCode());
    }
}