<?php

namespace Yuvayana\AcadlixImporter\Models;

use Illuminate\Database\Eloquent\Model;

defined('ABSPATH') || exit();

if (!class_exists('WpAdvQuizMaster')) {
    class WpAdvQuizMaster extends Model
    {
        protected $table;

        public function __construct($attributes = [])
        {
            parent::__construct($attributes);
            $this->table = acadlix()->helper()->acadlix_wp_prefix('wp_adv_quiz_master');
        }

        public function getResultTextAttribute($value)
        {
            if ($this->result_grade_enabled) {
                $data = [];
                $result = maybe_unserialize($value);
                $prozent = $result['prozent'] ?? [];
                $text = $result['text'] ?? [];
                foreach ($prozent as $key => $pro) {
                    $data[$key] = [
                        'percent' => $pro,
                        'text' => $text[$key],
                    ];
                }
                return $data;
            }
            return $value;
        }

        public function getAdminEmailAttribute($value)
        {
            if (class_exists('WpProQuiz_Model_QuizMapper')) {
                $quizMapper = new \WpProQuiz_Model_QuizMapper();
                $quiz = $quizMapper->fetch($this->id);
                $admin_email = $quiz->getAdminEmail();

                // Check if $admin_email is not null before accessing its methods
                if ($admin_email) {
                    return [
                        'to' => $admin_email->getTo() ?? '',
                        'from' => $admin_email->getFrom() ?? '',
                        'subject' => $admin_email->getSubject() ?? '',
                        'message' => $admin_email->getMessage() ?? '',
                    ];
                }
            }

            // Return empty values if $admin_email is null or if class doesn't exist
            return [
                'to' => '',
                'from' => '',
                'subject' => '',
                'message' => '',
            ];
        }

        public function getUserEmailAttribute($value)
        {
            if (class_exists('WpProQuiz_Model_QuizMapper')) {
                $quizMapper = new \WpProQuiz_Model_QuizMapper();
                $quiz = $quizMapper->fetch($this->id);
                $user_email = $quiz->getUserEmail();
                if ($user_email) {
                    return [
                        'to' => $user_email->getTo() ?? '',
                        'from' => $user_email->getFrom() ?? '',
                        'subject' => $user_email->getSubject() ?? '',
                        'message' => $user_email->getMessage() ?? '',
                    ];
                }
            }
            // Return empty values if $user_email is null or if class doesn't exist
            return [
                'to' => '',
                'from' => '',
                'subject' => '',
                'message' => '',
            ];
        }

        public function getToplistDataAttribute($value)
        {
            return maybe_unserialize($value);
        }
    }
}
