<?php

namespace App\Http\Controllers\Api;

use App\Models\Downloads;
use App\Models\Hentai;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use App\Http\Controllers\Controller;

class HentaiApiController extends Controller
{
    public function index(Request $request)
    {
        // Cache for 10 minutes
        $data = Cache::remember('api_hentai_list', now()->addMinutes(10), function () {
            return Hentai::with('episodes')
                ->orderBy('created_at', 'desc')
                ->get()
                ->map(function ($hentai) {
                    return [
                        'title'     => $hentai->episodes[0]->title,
                        'title_jpn' => $hentai->episodes[0]->title_jpn,
                        'slug'      => $hentai->slug,
                        'episodes'  => $hentai->episodes->map(function ($ep) {
                            return [
                                'episode'   => $ep->episode,
                                'slug'      => $ep->slug,
                            ];
                        }),
                    ];
                });
        });

        return response()->json($data);
    }
}
