<?php

namespace App\Http\Controllers\Portal;

use App\Http\Controllers\Controller;
use App\Http\Requests\Portal\StoreStaffRequest;
use App\Http\Requests\Portal\UpdateStaffRequest;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\View\View;

class StaffController extends Controller
{
    public function index(Request $request): View
    {
        $owner = auth()->user();
        $branchId = $owner->active_branch_id;

        $query = User::where('parent_id', $owner->id)
            ->whereIn('role', ['waiter', 'biller', 'kitchen']);

        if ($request->filled('role')) {
            $query->where('role', $request->role);
        }

        if ($request->filled('search')) {
            $query->where(function ($q) use ($request) {
                $q->where('name', 'like', '%'.$request->search.'%')
                    ->orWhere('email', 'like', '%'.$request->search.'%')
                    ->orWhere('phone_number', 'like', '%'.$request->search.'%');
            });
        }

        // Only show staff for the currently active branch
        $query->where('active_branch_id', $branchId);

        $staff = $query->latest()->paginate(10);

        return view('portal.staff.index', compact('staff'));
    }

    public function create(): View
    {
        return view('portal.staff.create');
    }

    public function store(StoreStaffRequest $request): RedirectResponse
    {
        $owner = auth()->user();

        $validated = $request->validated();
        $validated['parent_id'] = $owner->id;
        $validated['active_branch_id'] = $owner->active_branch_id;
        $validated['password'] = Hash::make($validated['password']);
        $validated['active'] = true;

        User::create($validated);

        return redirect()->route('portal.staff.index')->with('success', 'Staff member added successfully.');
    }

    public function edit(User $staff): View
    {
        $this->authorizeAccess($staff);

        return view('portal.staff.edit', compact('staff'));
    }

    public function update(UpdateStaffRequest $request, User $staff): RedirectResponse
    {
        $this->authorizeAccess($staff);

        $validated = $request->validated();

        if ($request->filled('password')) {
            $validated['password'] = Hash::make($validated['password']);
        } else {
            unset($validated['password']);
        }

        $staff->update($validated);

        return redirect()->route('portal.staff.index')->with('success', 'Staff information updated.');
    }

    public function destroy(User $staff): RedirectResponse
    {
        $this->authorizeAccess($staff);

        $staff->delete();

        return redirect()->route('portal.staff.index')->with('success', 'Staff member removed.');
    }

    protected function authorizeAccess(User $staff): void
    {
        if ($staff->parent_id !== auth()->id()) {
            abort(403, 'Unauthorized access to staff record.');
        }
    }
}
