<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\DeliveryInfo;
use App\Models\Product;
use Illuminate\Http\Request;

class DeliveryInfoController extends Controller
{
    public function index()
    {
        $deliveryInfos = DeliveryInfo::with('product')
            ->orderBy('is_global', 'desc')
            ->orderBy('order')
            ->paginate(20);

        return view('admin.delivery-info.index', compact('deliveryInfos'));
    }

    public function create()
    {
        $products = Product::active()->orderBy('name')->get();
        return view('admin.delivery-info.create', compact('products'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'product_id' => 'nullable|exists:products,id',
            'title' => 'required|string|max:255',
            'icon' => 'required|string|max:10',
            'items' => 'required|array|min:1',
            'items.*' => 'required|string',
            'is_active' => 'boolean',
            'is_global' => 'boolean',
            'order' => 'integer|min:0',
        ]);

        // If is_global is true, product_id must be null
        if ($request->has('is_global') && $request->is_global) {
            $validated['product_id'] = null;
        }

        DeliveryInfo::create($validated);

        return redirect()->route('admin.delivery-info.index')
            ->with('success', 'Delivery information created successfully.');
    }

    public function edit(DeliveryInfo $deliveryInfo)
    {
        $products = Product::active()->orderBy('name')->get();
        return view('admin.delivery-info.edit', compact('deliveryInfo', 'products'));
    }

    public function update(Request $request, DeliveryInfo $deliveryInfo)
    {
        $validated = $request->validate([
            'product_id' => 'nullable|exists:products,id',
            'title' => 'required|string|max:255',
            'icon' => 'required|string|max:10',
            'items' => 'required|array|min:1',
            'items.*' => 'required|string',
            'is_active' => 'boolean',
            'is_global' => 'boolean',
            'order' => 'integer|min:0',
        ]);

        // If is_global is true, product_id must be null
        if ($request->has('is_global') && $request->is_global) {
            $validated['product_id'] = null;
        }

        $deliveryInfo->update($validated);

        return redirect()->route('admin.delivery-info.index')
            ->with('success', 'Delivery information updated successfully.');
    }

    public function destroy(DeliveryInfo $deliveryInfo)
    {
        $deliveryInfo->delete();

        return redirect()->route('admin.delivery-info.index')
            ->with('success', 'Delivery information deleted successfully.');
    }

    public function toggleStatus(DeliveryInfo $deliveryInfo)
    {
        $deliveryInfo->update([
            'is_active' => !$deliveryInfo->is_active
        ]);

        return redirect()->route('admin.delivery-info.index')
            ->with('success', 'Status updated successfully.');
    }
}
