<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Branch;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class BranchController extends Controller
{
    /**
     * Display a listing of the branches.
     */
    public function index()
    {
        $branches = Branch::with('user')->latest()->paginate(10);
        return view('admin.branches.index', compact('branches'));
    }

    /**
     * Show the form for creating a new branch.
     */
    public function create()
    {
        $users = User::whereIn('role', ['admin', 'manager'])->get();
        return view('admin.branches.create', compact('users'));
    }

    /**
     * Store a newly created branch in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'branch_type_name' => ['required', 'string', 'max:255'],
            'addressline1' => ['required', 'string', 'max:500'],
            'addressline2' => ['nullable', 'string', 'max:500'],
            'phone' => ['required', 'string', 'max:20'],
            'email' => ['required', 'email', 'max:255'],
            'opening_hour' => ['required', 'string', 'max:100'],
            'opening_day' => ['required', 'string', 'max:100'],
            'maps_link' => ['required', 'url', 'max:500'],
            'user_id' => ['required', 'exists:users,id'],
        ]);

        Branch::create($request->all());

        return redirect()->route('admin.branches.index')
            ->with('success', 'Branch created successfully.');
    }

    /**
     * Show the form for editing the specified branch.
     */
    public function edit(Branch $branch)
    {
        $users = User::whereIn('role', ['admin', 'manager'])->get();
        return view('admin.branches.edit', compact('branch', 'users'));
    }

    /**
     * Update the specified branch in storage.
     */
    public function update(Request $request, Branch $branch)
    {
        $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'branch_type_name' => ['required', 'string', 'max:255'],
            'addressline1' => ['required', 'string', 'max:500'],
            'addressline2' => ['nullable', 'string', 'max:500'],
            'phone' => ['required', 'string', 'max:20'],
            'email' => ['required', 'email', 'max:255'],
            'opening_hour' => ['required', 'string', 'max:100'],
            'opening_day' => ['required', 'string', 'max:100'],
            'maps_link' => ['required', 'url', 'max:500'],
            'user_id' => ['required', 'exists:users,id'],
        ]);

        $branch->update($request->all());

        return redirect()->route('admin.branches.index')
            ->with('success', 'Branch updated successfully.');
    }

    /**
     * Remove the specified branch from storage.
     */
    public function destroy(Branch $branch)
    {
        $branch->delete();

        return redirect()->route('admin.branches.index')
            ->with('success', 'Branch deleted successfully.');
    }
}