<?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 AdminFormController {

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

    public function form(Request $request)
    {
        $adminId = $request->get('admin-id') ?? 0;
        $page = $this->moduleFactory->getPagesFactory()->getAdminFactory()->createAdminFormPage($adminId);
        return view('admin.adminForm', ['page' => $page]);
    }

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

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