<?php

namespace App\Http\Controllers;

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

class PublicController extends Controller
{
    // ============================================================
    // Home page
    // ============================================================
    public function home()
    {
        $featuredPlacements = Placement::active()
            ->featured()
            ->with(['mediaOwner', 'pricingPolicy', 'primaryMedia'])
            ->limit(6)
            ->get();

        $doohCount      = Placement::active()->dooh()->count();
        $billboardCount = Placement::active()->ooh()->count();
        $cityCount      = Placement::active()->distinct('city')->count('city');

        return view('public.home', compact(
            'featuredPlacements',
            'doohCount',
            'billboardCount',
            'cityCount'
        ));
    }

    // ============================================================
    // Explore page
    // ============================================================
    public function explore(Request $request)
    {
        $query = Placement::active()
            ->with(['mediaOwner', 'pricingPolicy', 'primaryMedia']);

        // Filters
        if ($request->filled('type')) {
            $query->where('type', $request->type);
        }

        if ($request->filled('city')) {
            $query->inCity($request->city);
        }

        if ($request->filled('area')) {
            $query->inArea($request->area);
        }

        if ($request->filled('booking_mode')) {
            if ($request->booking_mode === 'instant') {
                $query->instantBook();
            } else {
                $query->guardrailOffer();
            }
        }

        if ($request->filled('budget_min')) {
            $query->whereHas('pricingPolicy', function ($q) use ($request) {
                $q->where('list_price', '>=', $request->budget_min);
            });
        }

        if ($request->filled('budget_max')) {
            $query->whereHas('pricingPolicy', function ($q) use ($request) {
                $q->where('list_price', '<=', $request->budget_max);
            });
        }

        $placements = $query->latest()->paginate(12)->withQueryString();

        $cities = Placement::active()->distinct()->pluck('city');

        return view('public.explore', compact('placements', 'cities'));
    }

    // ============================================================
    // Placement detail — DOOH
    // ============================================================
    public function placementDooh(Placement $placement)
    {
        abort_if($placement->type !== 'dooh', 404);

        $placement->load([
            'mediaOwner',
            'pricingPolicy',
            'media',
            'availabilitySlots' => fn($q) => $q->available()
                ->where('date', '>=', now()->toDateString())
                ->orderBy('date')
                ->limit(14),
        ]);

        return view('public.placement-dooh', compact('placement'));
    }

    // ============================================================
    // Placement detail — Billboard
    // ============================================================
    public function placementBillboard(Placement $placement)
    {
        abort_if($placement->type !== 'ooh', 404);

        $placement->load([
            'mediaOwner',
            'pricingPolicy',
            'media',
            'availabilityBlocks' => fn($q) => $q->available()
                ->where('start_date', '>=', now()->toDateString())
                ->orderBy('start_date')
                ->limit(6),
        ]);

        return view('public.placement-billboard', compact('placement'));
    }
}