<?php

namespace App\Http\Controllers\Site;

use App\Http\Controllers\Controller;
use App\Models\Company;
use App\Models\Package;
use Illuminate\Http\Request;
use Illuminate\View\View;

class PackageSiteController extends Controller
{
    /**
     * Paket listeleme. Filtreler: Firma, Hizmet Türü, Fiyat Aralığı, İnternet Hızı, Kampanyalı Paketler.
     */
    public function index(Request $request): View
    {
        $packages = Package::query()
            ->active()
            ->with('company')
            ->when($request->filled('company_id'), fn ($q) => $q->where('company_id', $request->company_id))
            ->when($request->filled('service_type'), fn ($q) => $q->where('service_type', $request->service_type))
            ->when($request->filled('price_min'), fn ($q) => $q->where('monthly_fee', '>=', $request->price_min))
            ->when($request->filled('price_max'), fn ($q) => $q->where('monthly_fee', '<=', $request->price_max))
            ->when($request->filled('speed'), fn ($q) => $q->where('speed', 'like', "%{$request->speed}%"))
            ->when($request->boolean('campaign_only'), fn ($q) => $q->whereNotNull('campaign'))
            ->orderBy('sort_order')
            ->paginate(12)
            ->withQueryString();

        $companies = Company::active()->orderBy('sort_order')->get();

        return view('site.packages', compact('packages', 'companies'));
    }

    /** Kullanıcı birden fazla paket seçip fiyat/hız/kota/TV içeriği/kurulum ücreti karşılaştırabilir. */
    public function compare(Request $request): View
    {
        $ids = array_filter(explode(',', (string) $request->get('ids', '')));

        $packages = Package::active()->with('company')->whereIn('id', $ids)->get();

        return view('site.compare', compact('packages'));
    }

    /** Paket detay sayfası: paket bilgileri + online başvuru formu. */
    public function show(Package $package): View
    {
        $package->load('company');

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