<?php

namespace App\Http\Controllers\Admin\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\CreateUserRequest;
use App\Http\Requests\Admin\UpdateUserRequest;
use App\Models\Role;
use App\Services\Admin\AdminService;
use App\libs\Response\GlobalApiResponse;
use App\libs\Response\GlobalApiResponseCodeBook;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class AdminsController extends Controller
{
    private $adminService;

    public function __construct(AdminService $adminService) {
        $this->adminService = $adminService;
    }

    public function adminsList(){
        $data = $this->adminService->adminsList();
        return (new GlobalApiResponse())->success('Admins list',1,$data);
    }

    public function getAdminDetail($id){
        $data = $this->adminService->getAdminDetail($id);
        return (new GlobalApiResponse())->success('Admin Details',1,$data);
    }

    public function createAdmin(CreateUserRequest $request){
        $data = $this->adminService->createAdmin($request->all());
        if($this->adminService->hasError()) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, $this->adminService->getErrors()[0]['message'], $this->adminService->getErrors());
        }
        return (new GlobalApiResponse())->success('Admin successfully created');
    }

    public function updateAdmin(UpdateUserRequest $request, $id){
        $data = $this->adminService->updateAdmin($request->all(), $id);
        if($this->adminService->hasError()) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, $this->adminService->getErrors()[0]['message'], $this->adminService->getErrors());
        }
        return (new GlobalApiResponse())->success('Admin successfully updated');
    }

    public function deleteAdmin(Request $request){
        if (empty($request->admin_ids)) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, "Admin IDs not found!");
        }
        User::whereIn('id', $request->admin_ids)->delete();
        return (new GlobalApiResponse())->success("ADmin successfully deleted");
    }

    public function getAdminRoles($id){
        $data = $this->adminService->getAdminRoles($id);
        return (new GlobalApiResponse())->success('Admin roles list',1,$data);
    }

    public function getAdminRolesExcept($id){
        $data = array();
        $data['adminRolesList'] = $this->adminService->getAdminRolesExcept($id);
        return (new GlobalApiResponse())->success('Admin roles list',1,$data);
    }

    public function assignRolesToAdmin(Request $request){
        $admin = User::findOrFail($request->admin_id);
        $roles = Role::whereIn('id', $request->role_ids)->get();
        if (!empty($roles)) {
            $admin->attachRoles($roles);
            return (new GlobalApiResponse())->success("Role(s) successfully assigned to admin",1);
        } else {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::RECORD_NOT_EXIST, "Admin not found!");
        }
    }

    public function removeRoleFromAdmin(Request $request){
        $admin = User::findOrFail($request->admin_id);
        $role = Role::findOrFail($request->role_id);
        if ($admin) {
            $admin->detachRole($role);
            return (new GlobalApiResponse())->success("Role successfully removed from admin",1);
        } else {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::RECORD_NOT_EXIST, "Admin not found!");
        }
    }

    public function removeAllRolesFromAdmin(Request $request){
        $admin = User::findOrFail($request->admin_id);
        if ($admin) {
            $admin->roles()->detach();
            return (new GlobalApiResponse())->success("All roles successfully removed from admin",1);
        } else {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::RECORD_NOT_EXIST, "Admin not found!");
        }
    }

    public function emailAdmin(EmailUserRequest $request)
    {
        $email = $request->email;
        $subject = $request->subject;
        $body = $request->body;
        Mail::send(array(), array(), function ($message) use ($email, $subject, $body) {
            $message->to($email)
                ->subject($subject)
                ->setBody($body, 'text/html');
        });
        return (new GlobalApiResponse())->success('Email sent successfully');
    }
}
