<?php

namespace App\Http\Controllers;

use App\Models\User;
use Carbon\Carbon;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Hash;

class AdminController extends Controller
{
    public function show() : View 
    {
        return view('admin.admin_main');
    }

    public function deleteAdmin($ID)
    {
        sleep(2);
        $admin = User::find($ID);

        if (empty($admin)) {
            return $this->generateResponse('Error', 'Admin not found.', []);
        }

        if ($admin->role == 1 && User::fullAdminsCount() == 1) {
            return $this->generateResponse('Error', 'Can not delete last Full Administrator.', []);
        }

        if ($admin->id == Auth::user()->id) {
            return $this->generateResponse('Error', 'Can not delete yourself.', []);
        }

        $admin->delete();
        return $this->generateResponse('Success', '', []);
    }

    public function editAdmin($ID, Request $request)
    {
        $admin = User::find($ID);

        if (empty($admin)) {
            return $this->generateResponse('Error', 'Admin not found.', []);
        }

        $validator = Validator::make($request->all(), [
            'newAdminFirstname' => 'required|string|max:255',
            'newAdminLastname'  => 'required|string|max:255',
            'newAdminEmail'     => 'required|email',
            'newAdminPassword'  => 'string|max:255|nullable',
            'newAdmindRole'     => 'required|int',
        ],[], [
            'newAdminFirstname' => 'Firstname',
            'newAdminLastname'  => 'Lastname',
            'newAdminEmail'     => 'Email',
            'newAdminPassword'  => 'Password',
            'newAdmindRole'     => 'Admin role',
        ]);

        if ($validator->fails()) {
            return $this->generateResponse('Error', 'Validation Error.', [$validator->errors()]);
        }

        $admin->firstname            = $request->input('newAdminFirstname');
        $admin->lastname             = $request->input('newAdminLastname');
        $admin->email                = $request->input('newAdminEmail');
        $admin->jobRole              = 0;
        $admin->role                 = $request->input('newAdmindRole');

        if (!empty($request->input('newAdminPassword'))) {
            $admin->password = Hash::make($request->input('newAdminPassword'));
        }

        $admin->save();

        return $this->generateResponse('Success', '', []);
    }

    public function saveAdmin(Request $request) 
    {
        $validator = Validator::make($request->all(), [
            'newAdminFirstname' => 'required|string|max:255',
            'newAdminLastname'  => 'required|string|max:255',
            'newAdminEmail'     => 'required|email|unique:users,email',
            'newAdminPassword'  => 'required|string|max:255',
            'newAdmindRole'     => 'required|int',
        ],[], [
            'newAdminFirstname' => 'Firstname',
            'newAdminLastname'  => 'Lastname',
            'newAdminEmail'     => 'Email',
            'newAdminPassword'  => 'Password',
            'newAdmindRole'     => 'Admin role',
        ]);

        if ($validator->fails()) {
            return $this->generateResponse('Error', 'Validation Error.', [$validator->errors()]);
        }

        $user = new User();
        $user->firstname            = $request->input('newAdminFirstname');
        $user->lastname             = $request->input('newAdminLastname');
        $user->email                = $request->input('newAdminEmail');
        $user->password             = Hash::make($request->input('newAdminPassword'));
        $user->jobRole              = 0;
        $user->role                 = $request->input('newAdmindRole');
        $user->save();

        return $this->generateResponse('Success', '', []);
    }

    public function loadAdmins()
    {
        $formattedArray = [];

        foreach (User::admins() as $user) {

            switch ($user->role) {
                case 1:     $role = 'Full Administrator';   break;
                case 2:     $role = 'Internal IT';          break;
                case 3:     $role = 'Moderator';            break;
                default:    $role = 'None';
            }

            $formattedArray[] = [
                'id'        => $user->id,
                'firstname' => $user->firstname,
                'lastname'  => $user->lastname,
                'name'      => $user->firstname.' '.$user->lastname,
                'email'     => $user->email,
                'role'      => $role,
                'roleID'    => $user->role,
                'created'   => Carbon::parse($user->created_at)->format('l, M d, Y'),
                'updated'   => Carbon::parse($user->updated_at)->format('l, M d, Y'),
            ];
        }

        return $this->generateResponse('Success', '', $formattedArray);
    }
}
