<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;

use App\Models\Episode;

use Illuminate\Http\Request;

class StreamApiController extends Controller
{
    /**
     * Get Data used by the Video Player.
     */
    public function getStream(Request $request)
    {
        $validated = $request->validate([
            'episode_id'  => 'required',
        ]);

        $episode = Episode::where('id', $request->input('episode_id'))->firstOrFail();

        $subtitles = $episode->subtitles
            ->mapWithKeys(fn($sub) => [$sub->subtitle->slug => $sub->subtitle->name])
            ->toArray();

        return response()->json([
            'title'                 => $episode->title.' - '.$episode->episode,
            'poster'                => $episode->gallery()->first()->image_url,
            'interpolated'          => $episode->interpolated,
            'interpolated_uhd'      => $episode->interpolated_uhd,
            'stream_url'            => $episode->url,
            'stream_domains'        => config('hstream.stream_domain'),
            'asia_stream_domains'   => config('hstream.asia_stream_domain'),
            'extra_subtitles'       => $subtitles
        ], 200);
    }
}
