<?php

namespace App\Http\Controllers\MasterData;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use PhpParser\Node\Expr\FuncCall;

class UserController extends Controller
{
    public function index()
    {
        $users = User::orderBy('created_at', 'desc')->get();
        return view('master-data.pengguna.index', compact('users'));
    }

    public function store(Request $request)
    {

        $customMessage = [
            'fullname.required' => 'Nama wajib diisi',
            'fullname.max' => 'Nama maksimal 255 karakter',
            'fullname.string' => 'Nama harus berupa string',

            'username.required' => 'Username wajib diisi',
            'username.max' => 'Username maksimal 12 karakter',
            'username.string' => 'Username harus berupa string',

            'email.required' => 'Email wajib diisi',
            'email.email' => 'Email tidak valid',
            'email.unique' => 'Email sudah terdaftar',

            'role.required' => 'Role wajib diisi',
            'role.in' => 'Role tidak valid',

            'status.required' => 'Status wajib diisi',
            'status.in' => 'Status tidak valid',
        ];

        $validator = Validator::make($request->all(), [
            'fullname' => 'required|string|max:255',
            'username' => 'required|string|max:12',
            'email' => 'required|string|email|max:255|unique:users',
            'role' => 'required|in:admin,user',
            'status' => 'required|in:1,0',
        ], $customMessage);

        if ($validator->fails()) {
            toast($validator->messages()->all()[0], 'error')->position('top')->autoclose(3000);
            return redirect()->back()->withInput();
        }

        $user = new User();
        $user->name = $request->fullname;
        $user->username = $request->username;
        $user->email = $request->email;
        $user->password = Hash::make("12344321");
        $user->role = $request->role;

        if ($request->status == 0) {
            $activationCode = Str::random(4);
            $user->activation_code = $activationCode;
            $user->is_active = 0;
        } else {
            $user->is_active = $request->status;
        }


        try {
            $user->save();
            toast('Pengguna berhasil ditambahkan', 'success')->position('top-right')->autoclose(3000);
            return redirect()->back();
        } catch (\Throwable $th) {
            toast('Terjadi kesalahan', 'error')->position('top')->autoclose(3000);
            return redirect()->back();
        }
    }

    public function update(Request $request, $id)
    {
        $customMessage = [
            'fullname.required' => 'Nama wajib diisi',
            'fullname.max' => 'Nama maksimal 255 karakter',
            'fullname.string' => 'Nama harus berupa string',

            'username.required' => 'Username wajib diisi',
            'username.max' => 'Username maksimal 12 karakter',
            'username.string' => 'Username harus berupa string',

            'email.required' => 'Email wajib diisi',
            'email.email' => 'Email tidak valid',
            'email.unique' => 'Email sudah terdaftar',

            'role.required' => 'Role wajib diisi',
            'role.in' => 'Role tidak valid',

            'status.required' => 'Status wajib diisi',
            'status.in' => 'Status tidak valid',
        ];

        $validator = Validator::make($request->all(), [
            'fullname' => 'required|string|max:255',
            'username' => 'required|string|max:12',
            'email' => 'required|string|email|max:255|unique:users,email,' . $id,
            'role' => 'required|in:admin,user',
            'status' => 'required|in:1,0',
        ], $customMessage);

        if ($validator->fails()) {
            toast($validator->messages()->all()[0], 'error')->position('top')->autoclose(3000);
            return redirect()->back()->withInput();
        }

        $user = User::find($id);
        $user->name = $request->fullname;
        $user->username = $request->username;
        $user->email = $request->email;
        $user->role = $request->role;
        $user->is_active = $request->status;

        try {
            $user->save();
            toast('Pengguna berhasil diubah', 'success')->position('top-right')->autoclose(3000);
            return redirect()->back();
        } catch (\Throwable $th) {
            toast('Terjadi kesalahan', 'error')->position('top')->autoclose(3000);
            return redirect()->back();
        }
    }

    public function destroy($id)
    {
        $user = User::find($id);

        if ($user == null) {
            toast('Pengguna tidak ditemukan', 'error')->position('top')->autoclose(3000);
            return redirect()->back();
        }

        try {
            $user->delete();
            toast('Pengguna berhasil dihapus', 'success')->position('top-right')->autoclose(3000);
            return redirect()->back();
        } catch (\Throwable $th) {
            toast('Terjadi kesalahan', 'error')->position('top')->autoclose(3000);
            return redirect()->back();
        }
    }
}
