<?php

namespace App\Http\Controllers;

use App\Models\Offerwall;
use App\Support\DirectOfferFeed;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PublicOffersController extends Controller
{
    /**
     * Guest-facing offers showcase: all active offerwalls plus the hot and
     * featured offers for the visitor's country. No full offer grid and no
     * milestone detail — those require an account. Logged-in users are sent
     * to the real earn page.
     */
    public function index(Request $request)
    {
        if (Auth::check()) {
            return redirect()->route('user.earn');
        }

        $offerwalls = Offerwall::where('is_active', true)
            ->withCount('clicks')
            ->orderByDesc('clicks_count')
            ->orderBy('name')
            ->get();

        $providerOffers = DirectOfferFeed::activeProviderOffers();
        $directOffers = DirectOfferFeed::from($offerwalls, $providerOffers);

        return view('landing.offers', [
            'offerwalls' => $offerwalls,
            'hotOffers' => DirectOfferFeed::hot($directOffers, 8),
            'featuredOffers' => DirectOfferFeed::featured($directOffers, 8),
            'country' => DirectOfferFeed::visitorCountry(),
        ]);
    }
}
