<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeCustomer;

class OperationUserController extends Controller
{
    public function index(Request $request)
    {
        $users = User::whereHas('role', function ($q) {
            $q->where('slug', 'operation');
        })
            ->when($request->cursor, function ($q) use ($request) {
                $q->where('id', '>', $request->cursor);
            })
            ->orderBy('id')
            ->cursorPaginate(10);

        return view('operation_users.index', compact('users'));
    }

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

    public function store(Request $request)
    {
        $request->validate([
            'email' => ['required', 'email', 'unique:users,email,NULL,id,deleted_at,NULL'],
            'phone' => ['required', 'unique:users,phone,NULL,id,deleted_at,NULL', 'regex:/^01[0-2][0-9]{8}$/'],
        ]);
        $password = Str::random(10);
        $user = User::create([
            'email' => $request->email,
            'phone' => $request->phone,
            'password' => Hash::make($password),
            'role_id' => 2, // Assuming 2 = operation
        ]);
        Mail::to($user->email)->queue(new WelcomeCustomer($user, $password));
        return redirect()->route('operation-users.index')->with('success', 'Operation user created and password sent by email.');
    }

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

    public function update(Request $request, User $user)
    {
        $request->validate([
            'email' => 'required|email|unique:users,email,' . $user->id,
            'phone' => 'required|unique:users,phone,' . $user->id,
        ]);
        $user->update($request->only('email', 'phone'));
        return redirect()->route('operation-users.index')->with('success', 'Operation user updated.');
    }

    public function resetPassword(User $user)
    {
        $password = Str::random(10);
        $user->update(['password' => Hash::make($password)]);
        Mail::to($user->email)->queue(new WelcomeCustomer($user, $password));
        return back()->with('success', 'Password reset and sent by email.');
    }

    public function toggleStatus(User $user)
    {
        if ($user->deactivated_at) {
            $user->deactivated_at = null;
            $message = 'Operation user activated successfully.';
        } else {
            $user->deactivated_at = now();
            $message = 'Operation user deactivated successfully.';
        }
        
        $user->save();
        
        return redirect()->route('operation-users.index')
            ->with('success', $message);
    }

    public function destroy(User $user)
    {
        $user->delete();
        return back()->with('success', 'User deleted.');
    }
}
