<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

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

    public function show(User $user)
    {
        return view('admin.users.show', compact('user'));
    }

    public function create()
    {
        return view('admin.users.create');
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:users',
            'password' => 'required|min:6',
            'role' => 'required|in:admin,user',
            'balance_usd' => 'nullable|numeric|min:0',
            'balance_syr' => 'nullable|numeric|min:0',
        ]);

        $validated['password'] = bcrypt($validated['password']);
        $validated['balance_usd'] = $validated['balance_usd'] ?? 0;
        $validated['balance_syr'] = $validated['balance_syr'] ?? 0;

        User::create($validated);

        return redirect()->route('users.index')->with('success', 'تم إنشاء المستخدم بنجاح');
    }

    public function edit(User $user)
    {
        return view('admin.users.edit', compact('user'));
    }

    public function update(Request $request, User $user)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:users,email,' . $user->id,
            'password' => 'nullable|min:6',
            'role' => 'required|in:admin,user',
            'balance_usd' => 'nullable|numeric|min:0',
            'balance_syr' => 'nullable|numeric|min:0',
        ]);

        $user->name = $validated['name'];
        $user->email = $validated['email'];
        $user->role = $validated['role'];
        $user->balance_usd = $validated['balance_usd'] ?? $user->balance_usd;
        $user->balance_syr = $validated['balance_syr'] ?? $user->balance_syr;

        if (!empty($validated['password'])) {
            $user->password = bcrypt($validated['password']);
        }

        $user->save();

        return redirect()->route('users.index')->with('success', 'تم تحديث المستخدم بنجاح');
    }

    public function destroy(User $user)
    {
        if ($user->id === auth()->id()) {
            return back()->with('error', 'لا يمكنك حذف حسابك الحالي');
        }
        $user->delete();
        return redirect()->route('users.index')->with('success', 'تم حذف المستخدم');
    }

    public function updateBalance(Request $request, User $user)
    {
        $validated = $request->validate([
            'balance_usd' => 'nullable|numeric',
            'balance_syr' => 'nullable|numeric',
            'type' => 'required|in:add,subtract',
            'currency' => 'required|in:USD,SYR',
            'amount' => 'required|numeric|min:0',
        ]);

        $currency = $validated['currency'];
        $amount = $validated['amount'];
        $field = 'balance_' . strtolower($currency);

        if ($validated['type'] === 'add') {
            $user->$field += $amount;
        } else {
            if ($user->$field < $amount) {
                return back()->with('error', 'الرصيد غير كافٍ');
            }
            $user->$field -= $amount;
        }

        $user->save();

        return back()->with('success', 'تم تحديث الرصيد بنجاح');
    }
}
