<?php

namespace App\Http\Controllers;

use App\Models\Promo;
use App\Models\Contact;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Carbon\Carbon;
use Illuminate\Support\Str;

class PromoController extends Controller
{
    public function index()
    {
        $promos = Cache::remember('promos_page_list', 60, function () {
            $today = Carbon::today();
            return Promo::with('media')
                ->whereDate('start_date', '<=', $today)
                ->whereDate('end_date', '>=', $today)
                ->latest()
                ->get();
        });

        return view('promo.index', compact('promos'));
    }

    public function show(Promo $promo, Request $request)
    {
        // 1. Jika request AJAX (dari sidebar), kembalikan JSON
        if ($request->ajax()) {
            return response()->json([
                'id' => $promo->id,
                'name' => $promo->name,
                'image_url' => $promo->image_url,
                'description' => $promo->description,
                'start_date_fmt' => $promo->start_date->format('d F Y'),
                'end_date_fmt' => $promo->end_date->format('d F Y'),
            ]);
        }

        // 2. Load Sidebar Promos
        $sidebarPromos = Cache::remember('promos_sidebar_all', 60, function () {
            return Promo::whereDate('start_date', '<=', now())
                ->whereDate('end_date', '>=', now())
                ->select('id', 'name')
                ->get();
        });

        // 3. AMBIL DATA CONTACT
        $contact = Cache::remember('contact_data', 60 * 24, function () {
            $contact = Contact::where('is_active', 1)->latest()->first();
            if ($contact && $contact->whatsapp) {
                $phone = preg_replace('/[^0-9]/', '', $contact->whatsapp);
                if (substr($phone, 0, 1) === '0') {
                    $phone = '62' . substr($phone, 1);
                } elseif (substr($phone, 0, 2) !== '62') {
                    $phone = '62' . $phone;
                }
                $contact->whatsapp_formatted = $phone;
            }
            return $contact;
        });

        // 4. GENERATE SCHEMA AEO UNTUK PROMO (SaleEvent)
        $schema = [
            "@context" => "https://schema.org",
            "@type" => "SaleEvent",
            "name" => $promo->name,
            "description" => Str::limit(strip_tags($promo->description), 160),
            "startDate" => $promo->start_date->toIso8601String(),
            "endDate" => $promo->end_date->toIso8601String(),
            "image" => $promo->image_url ?? asset('img/logo-dino.png'),
            "url" => url()->current(),
            "organizer" => [
                "@type" => "Organization",
                "name" => "Dino Logistics",
                "url" => url('/')
            ]
        ];

        // Kirim $contact dan $schema ke view
        return view('promo.detail', compact('promo', 'sidebarPromos', 'contact', 'schema'));
    }
}