<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Province;
use App\Models\Store;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class StoreAdminController extends Controller
{
    public function index()
    {
        $stores = Store::with('province', 'municipality')->orderBy('sort_order')->get();
        return view('admin.stores.index', compact('stores'));
    }

    public function create()
    {
        $provinces = Province::orderBy('name')->get();
        return view('admin.stores.create', compact('provinces'));
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name' => 'required|string|max:255',
            'slug' => 'required|string|max:255|unique:stores,slug',
            'type' => 'required|string|in:hiper,mix',
            'description' => 'nullable|string',
            'province_id' => 'required|exists:provinces,id',
            'municipality_id' => 'nullable|exists:municipalities,id',
            'neighborhood' => 'nullable|string|max:255',
            'address_line' => 'nullable|string|max:255',
            'reference_point' => 'nullable|string|max:255',
            'latitude' => 'nullable|numeric',
            'longitude' => 'nullable|numeric',
            'phone' => 'nullable|string|max:50',
            'whatsapp' => 'nullable|string|max:50',
            'email' => 'nullable|email|max:255',
            'website' => 'nullable|url|max:255',
            'photo' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:2048',
            'is_active' => 'boolean',
            'status' => 'required|string|in:open,closed,renovation',
            'sort_order' => 'integer|nullable',
        ]);

        if ($request->hasFile('photo')) {
            $data['photo'] = $request->file('photo')->store('stores', 'public');
        }

        $data['sort_order'] = $data['sort_order'] ?? 0;
        $data['is_active'] = $request->boolean('is_active');

        Store::create($data);

        return redirect()->route('admin.stores.index')->with('success', 'Loja criada com sucesso.');
    }

    public function edit($id)
    {
        $store = Store::findOrFail($id);
        $provinces = Province::orderBy('name')->get();
        return view('admin.stores.edit', compact('store', 'provinces'));
    }

    public function update(Request $request, $id)
    {
        $store = Store::findOrFail($id);

        $data = $request->validate([
            'name' => 'required|string|max:255',
            'slug' => 'required|string|max:255|unique:stores,slug,' . $id,
            'type' => 'required|string|in:hiper,mix',
            'description' => 'nullable|string',
            'province_id' => 'required|exists:provinces,id',
            'municipality_id' => 'nullable|exists:municipalities,id',
            'neighborhood' => 'nullable|string|max:255',
            'address_line' => 'nullable|string|max:255',
            'reference_point' => 'nullable|string|max:255',
            'latitude' => 'nullable|numeric',
            'longitude' => 'nullable|numeric',
            'phone' => 'nullable|string|max:50',
            'whatsapp' => 'nullable|string|max:50',
            'email' => 'nullable|email|max:255',
            'website' => 'nullable|url|max:255',
            'photo' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:2048',
            'is_active' => 'boolean',
            'status' => 'required|string|in:open,closed,renovation',
            'sort_order' => 'integer|nullable',
        ]);

        if ($request->hasFile('photo')) {
            if ($store->photo) {
                Storage::disk('public')->delete($store->photo);
            }
            $data['photo'] = $request->file('photo')->store('stores', 'public');
        }

        $data['sort_order'] = $data['sort_order'] ?? 0;
        $data['is_active'] = $request->boolean('is_active');

        $store->update($data);

        return redirect()->route('admin.stores.index')->with('success', 'Loja atualizada com sucesso.');
    }

    public function destroy($id)
    {
        $store = Store::findOrFail($id);

        if ($store->photo) {
            Storage::disk('public')->delete($store->photo);
        }

        $store->delete();

        return redirect()->route('admin.stores.index')->with('success', 'Loja removida com sucesso.');
    }
}
