<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Package;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class ServiceController extends Controller
{
    public function index()
    {
        $packages = Package::orderBy('tier')->get();
        return view('admin.services.index', compact('packages'));
    }

    public function create()
    {
        return view('admin.services.form', ['package' => new Package()]);
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name'              => 'required|string|max:255',
            'tier'              => 'required|integer|min:1',
            'price'             => 'required|numeric|min:0',
            'short_description' => 'required|string|max:500',
            'full_description'  => 'required|string',
            'is_custom'         => 'boolean',
            'is_active'         => 'boolean',
        ]);

        $data['slug']      = Str::slug($data['name']);
        $data['is_custom'] = $request->boolean('is_custom');
        $data['is_active'] = $request->boolean('is_active');

        // Ensure slug is unique
        $base = $data['slug'];
        $i    = 1;
        while (Package::where('slug', $data['slug'])->exists()) {
            $data['slug'] = $base . '-' . $i++;
        }

        Package::create($data);

        return redirect()->route('admin.services.index')
            ->with('success', 'Service "' . $data['name'] . '" created successfully.');
    }

    public function edit(Package $service)
    {
        return view('admin.services.form', ['package' => $service]);
    }

    public function update(Request $request, Package $service)
    {
        $data = $request->validate([
            'name'              => 'required|string|max:255',
            'tier'              => 'required|integer|min:1',
            'price'             => 'required|numeric|min:0',
            'short_description' => 'required|string|max:500',
            'full_description'  => 'required|string',
            'is_custom'         => 'boolean',
            'is_active'         => 'boolean',
        ]);

        $newSlug = Str::slug($data['name']);
        if ($newSlug !== $service->slug) {
            $base = $newSlug;
            $i    = 1;
            while (Package::where('slug', $newSlug)->where('id', '!=', $service->id)->exists()) {
                $newSlug = $base . '-' . $i++;
            }
            $data['slug'] = $newSlug;
        }

        $data['is_custom'] = $request->boolean('is_custom');
        $data['is_active'] = $request->boolean('is_active');

        $service->update($data);

        return redirect()->route('admin.services.index')
            ->with('success', 'Service "' . $service->name . '" updated.');
    }

    public function toggle(Package $service)
    {
        $service->update(['is_active' => !$service->is_active]);

        return back()->with('success', '"' . $service->name . '" ' . ($service->is_active ? 'activated' : 'deactivated') . '.');
    }

    public function destroy(Package $service)
    {
        $name = $service->name;
        $service->delete();

        return redirect()->route('admin.services.index')
            ->with('success', 'Service "' . $name . '" deleted.');
    }
}
