<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Validator;
use App\Responses;
use App\Helper;
use App\Models\Users;
use App\Models\Clients;
use App\Models\Vehicles;
use App\Models\Banks;
use App\Models\UsersMenuPermissions;

class RolesController_bak extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function view(Request $req)
    {
        $permissions = \Spatie\Permission\Models\Permission::all();

        $data = [
            "roles" => Users::listRoles($req->auth->role),
            "pernus" => UsersMenuPermissions::listPermissionsMenus([
                "is_active" => UsersMenuPermissions::IS_ACTIVE,
            ]),
            "vehicles" => Vehicles::getVehicles(),
            "banks" => Banks::listBanks(["is_active" => Banks::IS_ACTIVE]),
            "permission" => $permissions->groupBy(function ($perm) {
                return explode(".", $perm->name)[0]; // ambil modul: user, transaction, dst
            }),
        ];

        return view("menu_v1.roles", $data);
    }

    /**
     * API
     */

    public function api_list(Request $req)
    {
        try {
            $list = DB::table("roles")->get();
            foreach ($list as $key => $row) {
                $list[$key]->DT_RowIndex = $key + 1;
                $list[$key]->count_trx = 0;
                $list[$key]->action = "-";
            }

            $apiResp = Responses::success("success list users");
            $apiResp["data"] = $list;
            $apiResp["count"] = count($list);
            return new Response($apiResp, $apiResp["meta"]["code"]);
        } catch (\Exception $e) {
            $apiResp = Responses::error($e->getMessage());
            return new Response($apiResp, $apiResp["meta"]["code"]);
        }
    }

    public function api_show(Request $req, $uid)
    {
        try {
            $now = time();

            $input = [
                "uid" => $uid,
            ];
            $rulesInput = [
                "uid" => "required|integer|not_in:0",
            ];

            // validasi input
            $isValidInput = Validator::make($input, $rulesInput);
            if (!$isValidInput->passes()) {
                $apiResp = Responses::bad_input($isValidInput->messages()->first());
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }

            $role = DB::table("roles")
                ->where("roles.id", $uid)
                ->get();

            if (count($role) < 1) {
                $apiResp = Responses::not_found("role not found");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }

            $permissions = DB::table("role_has_permissions")
                ->where("role_id", $uid)
                ->pluck("permission_id") // ambil array [1, 6, ...]
                ->toArray();
            $role[0]->permissions = $permissions;

            $apiResp = Responses::success("success get detail role");
            $apiResp["data"] = $role[0];
            return new Response($apiResp, $apiResp["meta"]["code"]);
        } catch (\Exception $e) {
            $apiResp = Responses::error($e->getMessage());
            return new Response($apiResp, $apiResp["meta"]["code"]);
        }
    }

    public function api_add(Request $req)
    {
        try {
            DB::beginTransaction();
            // Users::addUser($data);

            $role_id = DB::table("roles")->insertGetId([
                "name" => $req->name,
                "guard_name" => "web",
                "created_at" => date("Y-m-d H:i:s"),
                "updated_at" => date("Y-m-d H:i:s"),
            ]);

            $role_has_permissions = [];
            foreach ($req->permissions as $_permission) {
                array_push($role_has_permissions, ["permission_id" => $_permission, "role_id" => $role_id]);
            }

            DB::table("role_has_permissions")->insert($role_has_permissions);

            $apiResp = Responses::created("success add new role");
            DB::commit();
            return new Response($apiResp, $apiResp["meta"]["code"]);
        } catch (\Exception $e) {
            DB::rollBack();
            $apiResp = Responses::error($e->getMessage());
            return new Response($apiResp, $apiResp["meta"]["code"]);
        }
    }

    public function api_edit(Request $req)
    {
        try {
            DB::beginTransaction();

            DB::table("roles")
                ->where("id", $req->uid)
                ->update([
                    "name" => $req->name, // ganti sesuai field yang mau diubah
                    "updated_at" => now(),
                ]);

            DB::table("role_has_permissions")
                ->where("role_id", $req->uid)
                ->delete();

            $role_has_permissions = [];
            foreach ($req->permissions as $pid) {
                $role_has_permissions[] = [
                    "role_id" => $req->uid,
                    "permission_id" => $pid,
                ];
            }
            DB::table("role_has_permissions")->insert($role_has_permissions);

            $apiResp = Responses::created("success update role");

            DB::commit();
            return new Response($apiResp, $apiResp["meta"]["code"]);
        } catch (\Exception $e) {
            DB::rollBack();
            $apiResp = Responses::error($e->getMessage());
            return new Response($apiResp, $apiResp["meta"]["code"]);
        }
    }

    public function api_del(Request $req)
    {
        try {
            $now = time();

            $input = [
                "uid" => $req->uid,
            ];
            $rulesInput = [
                "uid" => "required|integer|not_in:0",
            ];
            $data = [
                "id" => $req->uid,
            ];

            // validasi input
            $isValidInput = Validator::make($input, $rulesInput);
            if (!$isValidInput->passes()) {
                $apiResp = Responses::bad_input($isValidInput->messages()->first());
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }

            $role = DB::table("roles")
                ->where("id", $req->uid)
                ->get();
            if (count($role) < 1) {
                $apiResp = Responses::not_found("user not found");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }

            DB::beginTransaction();

            DB::table("role_has_permissions")
                ->where("role_id", $req->uid)
                ->delete();

            DB::table("roles")
                ->where("id", $req->uid)
                ->delete();

            $apiResp = Responses::created("success delete role");

            DB::commit();
            return new Response($apiResp, $apiResp["meta"]["code"]);
        } catch (\Exception $e) {
            DB::rollBack();
            $apiResp = Responses::error($e->getMessage());
            return new Response($apiResp, $apiResp["meta"]["code"]);
        }
    }

    public function api_search(Request $req)
    {
        try {
            $now = time();
            $roles = Users::arrRoles();
            $statuses = Users::arrStatus();

            $input = [
                "name" => $req->name,
                "roles" => $req->roles,
            ];
            $rulesInput = [
                "name" => "required|string|max:125",
                "roles" => "nullable|integer|not_in:0",
            ];

            // validasi input
            $isValidInput = Validator::make($input, $rulesInput);
            if (!$isValidInput->passes()) {
                $apiResp = Responses::bad_input($isValidInput->messages()->first());
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }

            if ($req->roles) {
                if (in_array($req->roles, $roles)) {
                } else {
                    $apiResp = Responses::bad_request("role not valid");
                    return new Response($apiResp, $apiResp["meta"]["code"]);
                }
                $users = Users::likeName($req->name, $req->roles);
            } else {
                $users = Users::likeName($req->name);
            }

            if (count($users) < 1) {
                $apiResp = Responses::not_found("user not found");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }

            foreach ($users as $k => $v) {
                // remove from array but no reindex array
                // unset($users[$k]);
                // remove from array and reindex array
                // array_splice($users, $k, 1);
                unset($users[$k]->password);
            }

            $apiResp = Responses::success("success search user by name");
            $apiResp["data"] = $users;

            DB::commit();
            return new Response($apiResp, $apiResp["meta"]["code"]);
        } catch (\Exception $e) {
            DB::rollBack();
            $apiResp = Responses::error($e->getMessage());
            return new Response($apiResp, $apiResp["meta"]["code"]);
        }
    }
}
