<?php

declare(strict_types=1);

namespace OCA\RePod\Controller;

use OCA\RePod\AppInfo\Application;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IRequest;

class PageController extends Controller
{
	public function __construct(
		IRequest $request
	) {
		parent::__construct(Application::APP_ID, $request);
	}

	/**
	 * @return TemplateResponse<Http::STATUS_OK, array{}>
	 */
	#[NoAdminRequired]
	#[NoCSRFRequired]
	#[FrontpageRoute(verb: 'GET', url: '/')]
	public function index(): Response {
		$csp = new ContentSecurityPolicy();
		$csp->addAllowedImageDomain('*');
		$csp->addAllowedMediaDomain('*');

		$response = new TemplateResponse(Application::APP_ID, 'main');
		$response->setContentSecurityPolicy($csp);

		return $response;
	}

	/**
	 * @return TemplateResponse<Http::STATUS_OK, array{}>
	 */
	#[NoAdminRequired]
	#[NoCSRFRequired]
	#[FrontpageRoute(verb: 'GET', url: '/discover')]
	public function discover(): Response {
		return $this->index();
	}

	/**
	 * @return TemplateResponse<Http::STATUS_OK, array{}>
	 */
	#[NoAdminRequired]
	#[NoCSRFRequired]
	#[FrontpageRoute(verb: 'GET', url: '/feed/{path}', requirements: ['path' => '.+'])]
	public function feed(): Response {
		return $this->index();
	}
}
