<?php

namespace App\Http\Controllers;

use App\Models\Package;
use Illuminate\Http\Request;

class PackageController extends Controller
{
    /**
     * Display a listing of packages.
     */
    public function index()
    {
        $query = Package::query()->orderBy('driver_limit');

        if (!request()->routeIs('admin.packages.*')) {
            $query->where('is_active', true);
        }

        $packages = $query->paginate(15);

        if (request()->routeIs('admin.packages.*')) {
            return view('admin.packages.index', compact('packages'));
        }

        return view('packages.index', compact('packages'));
    }

    /**
     * Show the form for creating a new package.
     */
    public function create()
    {
        return view('admin.packages.create');
    }

    /**
     * Store a newly created package in storage.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'driver_limit' => 'required|integer|min:1|max:999',
            'truck_limit' => 'required|integer|min:1|max:999',
            'price' => 'required|numeric|min:0|decimal:0,2',
            'base_price' => 'required|numeric|min:0|decimal:0,2',
            'price_per_driver' => 'required|numeric|min:0|decimal:0,2',
            'billing_cycle' => 'required|in:weekly,monthly',
            'is_active' => 'boolean',
            'features' => 'nullable|array',
        ]);

        $validated['features'] = $request->input('features', []);
        $validated['is_active'] = $request->boolean('is_active', true);

        Package::create($validated);

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

    /**
     * Display the specified package.
     */
    public function show(Package $package)
    {
        if (request()->routeIs('admin.packages.*')) {
            return view('admin.packages.show', compact('package'));
        }

        return view('packages.show', compact('package'));
    }

    /**
     * Show the form for editing the specified package.
     */
    public function edit(Package $package)
    {
        return view('admin.packages.edit', compact('package'));
    }

    /**
     * Update the specified package in storage.
     */
    public function update(Request $request, Package $package)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'driver_limit' => 'required|integer|min:1|max:999',
            'truck_limit' => 'required|integer|min:1|max:999',
            'price' => 'required|numeric|min:0|decimal:0,2',
            'base_price' => 'required|numeric|min:0|decimal:0,2',
            'price_per_driver' => 'required|numeric|min:0|decimal:0,2',
            'billing_cycle' => 'required|in:weekly,monthly',
            'is_active' => 'boolean',
            'features' => 'nullable|array',
        ]);

        $validated['features'] = $request->input('features', []);
        $validated['is_active'] = $request->boolean('is_active', true);

        $package->update($validated);

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

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

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