<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use App\Models\Setting;

class CheckCampaignActive
{
    public function handle(Request $request, Closure $next)
    {
        $enabled = Setting::get('campaign_enabled', '1');

        if ($enabled !== '1') {
            return response()->view('public.campaign-closed', [], 200);
        }

        $start = Setting::get('campaign_start');
        $end = Setting::get('campaign_end');

        if ($start && $end) {
            $now = now();
            if ($now->lt(\Carbon\Carbon::parse($start)) || $now->gt(\Carbon\Carbon::parse($end))) {
                return response()->view('public.campaign-closed', [
                    'start' => $start,
                    'end' => $end,
                ], 200);
            }
        }

        return $next($request);
    }
}
