<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

class SystemController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | HALAMAN KELOLA SISTEM
    |--------------------------------------------------------------------------
    */

    public function index()
    {
        // HANYA SUPER ADMIN
        if (
            !Auth::check() ||
            Auth::user()->role !== 'super_admin'
        ) {
            abort(403, 'Akses ditolak. Hanya super admin.');
        }

        $users = User::orderBy('name')->get();

        return view('admin.system', compact('users'));
    }

    /*
    |--------------------------------------------------------------------------
    | JADIKAN USER MENJADI ADMIN
    |--------------------------------------------------------------------------
    */

    public function makeAdmin(Request $request)
    {
        // HANYA SUPER ADMIN
        if (
            !Auth::check() ||
            Auth::user()->role !== 'super_admin'
        ) {
            abort(403, 'Akses ditolak.');
        }

        $request->validate([
            'user_id' => 'required|exists:users,id'
        ]);

        $user = User::findOrFail($request->user_id);

        // Tidak bisa ubah super admin
        if ($user->role === 'super_admin') {
            return back()->with(
                'error',
                'Tidak dapat mengubah super admin.'
            );
        }

        $user->role = 'admin';
        $user->save();

        return back()->with(
            'success',
            "{$user->name} sekarang menjadi admin."
        );
    }

    /*
    |--------------------------------------------------------------------------
    | CABUT ADMIN MENJADI USER
    |--------------------------------------------------------------------------
    */

    public function removeAdmin(Request $request)
    {
        // HANYA SUPER ADMIN
        if (
            !Auth::check() ||
            Auth::user()->role !== 'super_admin'
        ) {
            abort(403, 'Akses ditolak.');
        }

        $request->validate([
            'user_id' => 'required|exists:users,id'
        ]);

        $user = User::findOrFail($request->user_id);

        // Tidak bisa ubah diri sendiri
        if ($user->id === Auth::id()) {
            return back()->with(
                'error',
                'Anda tidak dapat mencabut status Anda sendiri.'
            );
        }

        // Tidak bisa ubah super admin lain
        if ($user->role === 'super_admin') {
            return back()->with(
                'error',
                'Tidak dapat mengubah super admin.'
            );
        }

        $user->role = 'user';
        $user->save();

        return back()->with(
            'success',
            "Status admin untuk {$user->name} telah dicabut."
        );
    }

    /*
    |--------------------------------------------------------------------------
    | BLOKIR USER
    |--------------------------------------------------------------------------
    */

    public function block(Request $request)
    {
        // HANYA SUPER ADMIN
        if (
            !Auth::check() ||
            Auth::user()->role !== 'super_admin'
        ) {
            abort(403, 'Akses ditolak.');
        }

        $request->validate([
            'user_id' => 'required|exists:users,id'
        ]);

        $user = User::find($request->user_id);

        if (!$user) {
            return back()->with(
                'error',
                'User tidak ditemukan.'
            );
        }

        // Tidak bisa blokir admin
        if (
            $user->role === 'admin' ||
            $user->role === 'super_admin'
        ) {
            return back()->with(
                'error',
                'Tidak dapat memblokir admin atau super admin.'
            );
        }

        $user->blocked_at = now();
        $user->save();

        return back()->with(
            'success',
            "User {$user->name} telah diblokir."
        );
    }

    /*
    |--------------------------------------------------------------------------
    | BUKA BLOKIR USER
    |--------------------------------------------------------------------------
    */

    public function unblock(Request $request)
    {
        // HANYA SUPER ADMIN
        if (
            !Auth::check() ||
            Auth::user()->role !== 'super_admin'
        ) {
            abort(403, 'Akses ditolak.');
        }

        $request->validate([
            'user_id' => 'required|exists:users,id'
        ]);

        $user = User::find($request->user_id);

        if (!$user) {
            return back()->with(
                'error',
                'User tidak ditemukan.'
            );
        }

        $user->blocked_at = null;
        $user->save();

        return back()->with(
            'success',
            "Blokir untuk {$user->name} telah dibuka."
        );
    }
}

