<?php

declare(strict_types=1);

class FrontHistoryController
{
    public static function routes(): void
    {
        Router::get('/history', [self::class, 'index']);
        Router::post('/history/clear', [self::class, 'clear']);
    }

    public static function index(): void
    {
        Auth::requireAuth();
        $total = History::userHistoryCount(Auth::id());
        $pagination = Pagination::fromRequest($total, 24);

        ThemeRenderer::render('history', [
            'pageTitle' => SEO::title('Watch History'),
            'videos' => History::userHistory(Auth::id(), 24, $pagination->offset),
            'pagination' => $pagination,
        ]);
    }

    public static function clear(): void
    {
        Auth::requireAuth();
        if (Router::verifyCsrf()) {
            History::clear(Auth::id());
        }
        Router::redirect('/history');
    }
}
