<?php
declare(strict_types=1);

namespace App\Modules\Sessions\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\App;
use App\Modules\Sessions\Models\SessionFeedback;
use App\Modules\Sessions\Services\SessionFeedbackService;

class SessionFeedbackController extends Controller
{
    public function create(Request $request, int $sessionId): mixed
    {
        $db = App::getInstance()->db();
        $session = $db->selectOne(
            "SELECT ts.*, tg.name_ar AS group_name, c.full_name_ar AS coach_name
             FROM training_sessions ts
             LEFT JOIN training_groups tg ON tg.id = ts.group_id
             LEFT JOIN coaches c ON c.id = ts.coach_id
             WHERE ts.id = ?",
            [$sessionId]
        );
        if (!$session) {
            return $this->redirect('/sessions')->withError('الجلسة غير موجودة');
        }

        return $this->view('Sessions.Views.feedback_form', compact('session', 'sessionId'));
    }

    public function store(Request $request, int $sessionId): mixed
    {
        $data = $_POST;
        $data['session_id'] = $sessionId;
        $data['player_id'] = (int) ($data['player_id'] ?? $_SESSION['player_id'] ?? 0);

        if (!$data['player_id']) {
            return $this->redirect("/sessions/{$sessionId}/feedback")->withError('يجب تحديد اللاعب');
        }

        try {
            SessionFeedbackService::submitFeedback($data);
            return $this->redirect("/sessions/{$sessionId}")->withSuccess('تم إرسال التقييم بنجاح');
        } catch (\RuntimeException $e) {
            return $this->redirect("/sessions/{$sessionId}/feedback")->withError($e->getMessage());
        }
    }

    public function report(Request $request, int $sessionId): mixed
    {
        $this->authorize('session.view_feedback');
        $db = App::getInstance()->db();

        $session = $db->selectOne(
            "SELECT ts.*, tg.name_ar AS group_name, c.full_name_ar AS coach_name
             FROM training_sessions ts
             LEFT JOIN training_groups tg ON tg.id = ts.group_id
             LEFT JOIN coaches c ON c.id = ts.coach_id
             WHERE ts.id = ?",
            [$sessionId]
        );
        if (!$session) {
            return $this->redirect('/sessions')->withError('الجلسة غير موجودة');
        }

        $report = SessionFeedbackService::getSessionReport($sessionId);
        $averages = $report['averages'];
        $feedback = $report['feedback'];

        return $this->view('Sessions.Views.feedback_report', compact('session', 'sessionId', 'averages', 'feedback'));
    }

    public function coachSummary(Request $request, int $coachId): mixed
    {
        $this->authorize('session.view_feedback');
        $dateFrom = $_GET['from'] ?? date('Y-m-01');
        $dateTo = $_GET['to'] ?? date('Y-m-d');

        $summary = SessionFeedback::getCoachSummary($coachId, $dateFrom, $dateTo);

        $db = App::getInstance()->db();
        $coach = $db->selectOne("SELECT * FROM coaches WHERE id = ?", [$coachId]);

        return $this->view('Sessions.Views.feedback_coach_summary', compact('coach', 'summary', 'dateFrom', 'dateTo', 'coachId'));
    }
}
