<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ShopBranch;
use App\Models\ShopDepartment;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Inertia\Inertia;

class BranchController extends Controller
{
    public function index()
    {
        $branches = ShopBranch::with('department')
            ->orderBy('sort_order')
            ->paginate(15);

        return Inertia::render('Admin/Branches/Index', [
            'branches' => $branches,
        ]);
    }

    public function create()
    {
        $departments = ShopDepartment::orderBy('sort_order')->get();
        
        return Inertia::render('Admin/Branches/Create', [
            'departments' => $departments,
        ]);
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'title' => 'required|string|max:255',
            'slug' => ['nullable', 'string', Rule::unique('shop_branches', 'slug')->where(fn ($query) => $query->where('department_id', $request->input('department_id')))],
            'department_id' => 'required|exists:shop_departments,id',
            'label' => 'nullable|string|max:160',
            'description' => 'nullable|string',
            'image' => 'nullable|string|max:2000',
            'sort_order' => 'nullable|integer|min:0',
            'is_active' => 'boolean',
        ]);

        if (empty($validated['slug'])) {
            $validated['slug'] = Str::slug($validated['title']);
        }

        ShopBranch::create($validated);

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

    public function edit(ShopBranch $branch)
    {
        $departments = ShopDepartment::orderBy('sort_order')->get();
        
        return Inertia::render('Admin/Branches/Edit', [
            'branch' => $branch,
            'departments' => $departments,
        ]);
    }

    public function update(Request $request, ShopBranch $branch)
    {
        $validated = $request->validate([
            'title' => 'required|string|max:255',
            'slug' => ['nullable', 'string', Rule::unique('shop_branches', 'slug')->where(fn ($query) => $query->where('department_id', $request->input('department_id')))->ignore($branch->id)],
            'department_id' => 'required|exists:shop_departments,id',
            'label' => 'nullable|string|max:160',
            'description' => 'nullable|string',
            'image' => 'nullable|string|max:2000',
            'sort_order' => 'nullable|integer|min:0',
            'is_active' => 'boolean',
        ]);

        if (empty($validated['slug'])) {
            $validated['slug'] = Str::slug($validated['title']);
        }

        $branch->update($validated);

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

    public function destroy(ShopBranch $branch)
    {
        $branch->delete();
        return redirect()->route('branches.index')
            ->with('success', 'Branch deleted successfully!');
    }
}
