<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Recommendation;
use Illuminate\Http\Request;

class AdminRekomendasiController extends Controller
{
    /**
     * Helper function untuk mengekstrak ID video dari URL YouTube.
     */
    private function extractYouTubeId(string $url): ?string
    {
        preg_match('/(v=|vi=|youtu.be\/|embed\/|\/v\/|\?v=|\&v=)(.+?)\b/i', $url, $matches);
        return $matches[2] ?? null;
    }

    public function index()
    {
        $rekomendasiMentah = Recommendation::latest()->get();

        // Menambahkan thumbnail YouTube ke setiap rekomendasi
        $semuaRekomendasi = $rekomendasiMentah->map(function ($item) {
            $videoId = $this->extractYouTubeId($item->youtube_link);
            return [
                'id' => $item->id,
                'judul' => $item->judul,
                'kategori' => $item->kategori,
                'youtube_link' => $item->youtube_link,
                'thumbnail' => $videoId ? "https://img.youtube.com/vi/{$videoId}/hqdefault.jpg" : 'https://via.placeholder.com/150?text=No+Preview',
                'deskripsi' => $item->deskripsi,
            ];
        });

        return view('admin.rekomendasi.index', [
            'pageTitle' => 'Manajemen Rekomendasi',
            'semuaRekomendasi' => $semuaRekomendasi
        ]);
    }

    public function create()
    {
        return view('admin.rekomendasi.create', ['pageTitle' => 'Tambah Rekomendasi']);
    }

    public function edit($id)
    {
        $rekomendasi = Recommendation::findOrFail($id);

        return view('admin.rekomendasi.edit', [
            'pageTitle' => 'Edit Rekomendasi: ' . $rekomendasi->judul,
            'rekomendasi' => $rekomendasi
        ]);
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'judul' => 'required|string|max:255',
            'kategori' => 'required|string|max:100',
            'youtube_link' => 'required|url',
            'deskripsi' => 'required|string',
        ]);

        Recommendation::create($validated);

        return redirect()->route('admin.rekomendasi.index')->with('success', 'Rekomendasi berhasil ditambahkan.');
    }

    public function update(Request $request, $id)
    {
        $rekomendasi = Recommendation::findOrFail($id);

        $validated = $request->validate([
            'judul' => 'required|string|max:255',
            'kategori' => 'required|string|max:100',
            'youtube_link' => 'required|url',
            'deskripsi' => 'required|string',
        ]);

        $rekomendasi->update($validated);

        return redirect()->route('admin.rekomendasi.index')->with('success', 'Rekomendasi berhasil diperbarui.');
    }

    public function destroy($id)
    {
        $rekomendasi = Recommendation::findOrFail($id);
        $rekomendasi->delete();

        return redirect()->route('admin.rekomendasi.index')->with('success', 'Rekomendasi berhasil dihapus.');
    }
}