<?php

namespace Yuvayana\Acadlix\Common\Models;

use WP_Query;
use Illuminate\Database\Eloquent\Model;

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

if (!class_exists(class: 'CourseSectionContent')) {
  class CourseSectionContent extends Model
  {
    protected $table;
    protected $primaryKey = 'ID';
    protected $with = ['author', 'metas'];
    protected $appends = [
      'rendered_post_content',
      'rendered_metas',
      'contentable',
      'contentable_data',
    ];

    protected static $postType = ACADLIX_COURSE_SECTION_CONTENT_CPT;

    public function __construct(array $attributes = [])
    {
      parent::__construct($attributes);

      $this->table = acadlix()->helper()->acadlix_wp_prefix('posts');
    }

    public function course_statistics()
    {
      return $this->hasMany(acadlix()->model()->courseStatistic(), 'course_section_content_id', 'ID');
    }

    public function scopeOfCourseSectionContent($query)
    {
      return $query->where('post_type', self::$postType);
    }

    public function section()
    {
      return $this->belongsTo(acadlix()->model()->courseSection(), 'post_parent', 'ID')->ofCourseSection();
    }

    public function getCourseAttribute()
    {
      return $this->section->course;
    }
   

    public function getRenderedPostContentAttribute()
    {
      return acadlix()->helper()->renderShortCode($this->post_content);
    }

    public function author()
    {
      return $this->belongsTo(acadlix()->model()->wpUsers(), 'post_author', 'ID');
    }

    public function metas()
    {
      return $this->hasMany(acadlix()->model()->wpPostMeta(), 'post_id', 'ID');
    }

    public function getRenderedMetasAttribute()
    {
      $metas = $this->metas;
      if (empty($metas)) {
        return [];
      }
      $keyValueArray = [];

      foreach ($metas as $meta) {
        // Ensure meta_key and meta_value exist in the object
        if (isset($meta['meta_key'], $meta['meta_value'])) {
          $key = $meta['meta_key'];
          $value = $meta['meta_value'];

          // Decode JSON if applicable
          if (is_string($value) && $decoded = json_decode($value, true)) {
            $value = $decoded;
          }

          $keyValueArray[$key] = $value;
        }
      }
      $renderedMetas = !empty($keyValueArray) && is_array($keyValueArray)
        ? acadlix()->helper()->cpt()->acadlix_remome_prefix_meta_keys($keyValueArray, 'course_section_content')
        : [];

      return $renderedMetas;
    }

    public function getByLessonId($lesson_id)
    {
      $args = [
        'post_type' => self::$postType,
        'posts_per_page' => -1, // Fetch all matching posts
        'meta_query' => [ // phpcs:ignore
          'relation' => 'AND',
          [
            'key' => '_acadlix_course_section_content_type',
            'value' => 'lesson',
          ],
          [
            'key' => '_acadlix_course_section_content_lesson_id',
            'value' => $lesson_id,
          ],
        ],
      ];
      $query = new WP_Query($args);
      return $query->posts;
    }

    public function getByQuizId($quiz_id)
    {
      $args = [
        'post_type' => self::$postType,
        'posts_per_page' => -1, // Fetch all matching posts
        'meta_query' => [ // phpcs:ignore
          'relation' => 'AND',
          [
            'key' => '_acadlix_course_section_content_type',
            'value' => 'quiz',
          ],
          [
            'key' => '_acadlix_course_section_content_quiz_id',
            'value' => $quiz_id,
          ],
        ],
      ];
      $query = new WP_Query($args);
      return $query->posts;
    }

    public function getContentableAttribute()
    {
      $renderedMetas = $this->rendered_metas;
      $contentable = [];
      $contentableType = $renderedMetas['type'];
      $contentable['type'] = $contentableType ?? null;
      switch ($contentableType) {
        case 'lesson':
          $lesson = acadlix()->model()->lesson()->ofLesson()->find($renderedMetas['lesson_id']);
          $contentableId = $renderedMetas['lesson_id'] ?? null;
          $contentableTitle = $lesson->post_title ?? "";
          break;
        case 'quiz':
          $quiz = acadlix()->model()->quiz()->ofQuiz()->find($renderedMetas['quiz_id']);
          $contentableId = $renderedMetas['quiz_id'] ?? null;
          $contentableTitle = $quiz->post_title ?? "";
          break;
        default:
          $contentableId = $renderedMetas['lesson_id'] ?? null;
          $contentableTitle = $lesson->post_title ?? "";
      }
      $contentable['id'] = $contentableId;
      $contentable['title'] = $contentableTitle;
      return $contentable;
    }

    public function getContentableDataAttribute()
    {
      $renderedMetas = $this->rendered_metas;
      $contentableType = $renderedMetas['type'];
      return match ($contentableType) {
        'lesson' => acadlix()->model()->lesson()->ofLesson()->find($renderedMetas['lesson_id']),
        'quiz' => acadlix()->model()->quiz()->ofQuiz()->find($renderedMetas['quiz_id']),
        default => null,
      };
    }

    public static function insertCourseSectionContent(array $data, array $meta = [])
    {
      $data = wp_parse_args($data, [
        'post_title' => '',
        'post_content' => '',
        'post_status' => 'publish',
        'post_type' => self::$postType,
        'post_author' => 1,
      ]);

      // Add meta data to the 'meta_input' argument for wp_insert_post.
      if (!empty($meta)) {
        $data['meta_input'] = $meta;
      }

      // Insert the post and return the ID or WP_Error.
      $postId = wp_insert_post($data);

      return $postId;
    }

    public static function updateCourseSectionContent(int $postId, array $data = [], array $meta = [])
    {
      $data = wp_parse_args($data, [
        'ID' => $postId,
      ]);

      // Add meta data to the 'meta_input' argument for wp_update_post.
      if (!empty($meta)) {
        $data['meta_input'] = $meta;
      }

      // Update the post and return the result or WP_Error.
      $result = wp_update_post($data, true);

      return $result;
    }

    public static function deleteCourseSectionContent(int $postId)
    {
      // Check if post exists
      $post = get_post($postId);
      if (!$post || $post->post_type !== self::$postType) {
        return new \WP_Error('invalid_post', __('Invalid post ID or not a course section post type.', 'acadlix'));
      }

      // Delete post
      $result = wp_delete_post($postId, true);

      if (!$result) {
        return new \WP_Error('delete_failed', __('Failed to delete course section.', 'acadlix'));
      }

      // Delete statistic
      $courseStatistics = acadlix()->model()->courseStatistic()->where('course_section_content_id', $postId)->get();
      foreach ($courseStatistics as $courseStatistic) {
        $courseStatistic->delete();
      }

      return true;
    }
  }
}