<?php

namespace App\Http\Controllers;

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

class UserController extends Controller
{
    public function admin()
    {
        $users = User::role('admin')->get(); // Only admins
        return view('backend.super.admin_list', compact('users'));
    }
    public function user(){
        $users = User::role('user')->get(); 
        return view('backend.super.user_list', compact('users'));
        
    }
    public function create()
    {
        return view('backend.super.create');
    }
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string',
            'email' => 'required|email|unique:users',
            'password' => 'required|string|min:6',
            'role' => 'required|exists:roles,name',
        ]);

        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'can_sell' => $request->input('can_sell', 0),
            'can_lease' => $request->input('can_lease', 0),
            'can_rent' => $request->input('can_rent', 0),
            'password' => Hash::make($request->password),
            'role' => $request->role,
        ]);
        //dd($request->all());
        $user->assignRole($request->role);

        return redirect()->route('user.create')->with('success', 'User created successfully!');
    }

    public function edit($id)
    {
        $user = User::findOrFail($id);
        return view('backend.super.edit_user', compact('user'));
    }
    public function update(Request $request, $id)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:users,email,' . $id,
            'role' => 'required|exists:roles,name',
        ]);

        $user = User::findOrFail($id);

        $user->name = $request->name;
        $user->email = $request->email;
        $user->can_sell = $request->input('can_sell', 0);
        $user->can_lease = $request->input('can_lease', 0);
        $user->can_rent = $request->input('can_rent', 0);

        $user->save();
        
        // Update role
        $user->syncRoles([$request->role]);

        //return redirect()->route('super.admin')->with('success', 'User updated successfully.');
        return redirect()->back()->with('success', 'User updated successfully.');
    }

    public function destroy($id)
    {
        $user = User::findOrFail($id);
        //$user->delete();
        $user->forceDelete();

        return redirect()->back()->with('success', 'User deleted successfully.');
    }

    //User Create by admin
    public function NewUsers()
    {
        return view('backend.admin.users.create_user');
    }
    public function UserStore(Request $request)
    {
        $request->validate([
            'name' => 'required|string',
            'email' => 'required|email|unique:users',
            'password' => 'required|string|min:6',
            'role' => 'required|exists:roles,name',
        ]);

        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'can_sell' => $request->input('can_sell', 0),
            'can_lease' => $request->input('can_lease', 0),
            'can_rent' => $request->input('can_rent', 0),
            'password' => Hash::make($request->password),
            'role' => $request->role,
        ]);
        //dd($request->all());
        $user->assignRole($request->role);

        return redirect()->route('new.user')->with('success', 'User created successfully!');
    }
    public function ManageAdmin()
    {
         $users = User::whereHas('roles', function ($query) {
            $query->whereIn('name', ['admin', 'user']);
        })->get();
        
        return view('backend.admin.users.list_admin', compact('users'));
    }
    public function editUser($id)
    {
        $user = User::findOrFail($id);
        return view('backend.admin.users.edit_user', compact('user'));
    }
    public function updateUser(Request $request, $id)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:users,email,' . $id,
            'role' => 'required|exists:roles,name',
        ]);

        $user = User::findOrFail($id);

        $user->name = $request->name;
        $user->email = $request->email;
        $user->can_sell = $request->input('can_sell', 0);
        $user->can_lease = $request->input('can_lease', 0);
        $user->can_rent = $request->input('can_rent', 0);

        $user->save();
        
        // Update role
        $user->syncRoles([$request->role]);

        return redirect()->back()->with('success', 'User updated successfully.');
    }

    //delete
    public function UserDestroy($id)
    {
        $user = User::findOrFail($id);

        if (auth()->id() === $user->id) {
            return redirect()->back()->with('error', 'You cannot delete your own account.');
        }

        if ($user->hasRole('admin')) {
            return redirect()->back()->with('error', 'You cannot delete a Super Admin.');
        }

        // Force delete if needed
        $user->forceDelete(); // permanently remove

        return redirect()->back()->with('success', 'User deleted successfully.');
    }
    
    //lead
    public function Lead()
    {
        $contacts = \App\Models\ContactUs::latest()->paginate(10);

        $contacts->transform(function ($contact) {
            $contact->matched_property = \App\Models\Property::where('property_name', $contact->property_name)->first();
            return $contact;
        });

        return view('backend.admin.lead', compact('contacts'));
    }

}
