<?php
class BeginnerController
{
    public function register(): void
    {
        View::render('beginner/register');
    }

    public function doRegister(): void
    {
        CSRF::check();
        $result = BeginnerInstructor::register($_POST);
        flash($result['ok'] ? 'success' : 'danger', $result['message']);
        redirect($result['ok'] ? 'beginner/login' : 'beginner/register');
    }

    public function login(): void
    {
        View::render('beginner/login');
    }

    public function doLogin(): void
    {
        CSRF::check();
        if (!BeginnerInstructor::login(trim((string)($_POST['email'] ?? '')), (string)($_POST['password'] ?? ''))) {
            flash('danger', 'E-mail ou senha incorretos, ou cadastro bloqueado.');
            redirect('beginner/login');
        }
        flash('success', 'Bem-vindo à área de formação para instrutores.');
        redirect('beginner/panel');
    }

    public function logout(): void
    {
        BeginnerInstructor::logout();
        flash('success', 'Você saiu da área de formação.');
        redirect('home');
    }

    public function panel(): void
    {
        $beginner = BeginnerInstructor::requireLogin();
        $orders = TrainingOrder::forBeginner((int)$beginner['id']);
        $orderedIds = [];
        foreach ($orders as $order) {
            $orderedIds[(int)$order['content_id']] = true;
        }
        View::render('beginner/panel', [
            'beginner' => $beginner,
            'contents' => TrainingContent::all(true),
            'orders' => $orders,
            'orderedIds' => $orderedIds,
            'videoCount' => count(VideoLesson::forActor('beginner', (int)$beginner['id'])),
            'portalCharges' => PaymentCenter::forActor('beginner', (int)$beginner['id']),
            'unreadMessages' => Communication::unreadCount('beginner', (int)$beginner['id']),
        ]);
    }

    public function requestContent(): void
    {
        CSRF::check();
        $beginner = BeginnerInstructor::requireLogin();
        $result = TrainingOrder::request((int)$beginner['id'], (int)($_POST['content_id'] ?? 0));
        flash($result['ok'] ? 'success' : 'danger', $result['message']);
        redirect('beginner/panel');
    }

    public function uploadProof(): void
    {
        CSRF::check();
        $beginner = BeginnerInstructor::requireLogin();
        $result = TrainingOrder::submitProof((int)$beginner['id'], (int)($_POST['order_id'] ?? 0), $_FILES['proof_file'] ?? []);
        flash($result['ok'] ? 'success' : 'danger', $result['message']);
        redirect('beginner/panel');
    }

    public function downloadContent(): void
    {
        $beginner = BeginnerInstructor::requireLogin();
        $content = TrainingOrder::accessibleContent((int)$beginner['id'], (int)($_GET['id'] ?? 0));
        if (!$content || empty($content['file_path'])) {
            http_response_code(404);
            exit('Conteúdo não encontrado ou ainda não liberado.');
        }
        TrainingContent::stream($content);
    }
}
