<?php

declare(strict_types=1);

namespace OCA\RePod\Controller;

use OCA\RePod\AppInfo\Application;
use OCA\RePod\Core\PodcastData\PodcastData;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Http\Client\IClientService;
use OCP\IRequest;

class PodcastController extends Controller
{
	public function __construct(
		IRequest $request,
		private IClientService $clientService
	) {
		parent::__construct(Application::APP_ID, $request);
	}

	public function index(string $url): JSONResponse {
		$client = $this->clientService->newClient();
		$feed = $client->get($url);
		$podcast = PodcastData::parseRssXml((string) $feed->getBody());

		return new JSONResponse($podcast->toArrayWithExtras(), $feed->getStatusCode());
	}
}