<?php

namespace App\Http\Controllers\Site;

use App\Http\Controllers\Controller;
use App\Models\Page;
use App\Models\Service;
use Illuminate\Contracts\View\View;

class ServiceListingController extends Controller
{
    public function index(): View
    {
        $page = Page::query()
            ->published()
            ->where('route_path', '/services')
            ->with('sections')
            ->first();

        $services = Service::query()->published()->orderBy('sort_order')->get();

        // Safety fallback: if all records are unpublished in admin, still show services.
        if ($services->isEmpty()) {
            $services = Service::query()->orderBy('sort_order')->get();
        }

        return view('site.services', compact('page', 'services'));
    }
}
