<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * @property int $id
 * @property int $created_by
 * @property string $title
 * @property string|null $short_description
 * @property string|null $description
 * @property array|null $description_json
 * @property array|null $overview_table
 * @property string|null $category
 * @property string|null $level
 * @property string|null $language
 * @property string|null $thumbnail
 * @property int|null $duration
 * @property float|null $cpd_hours
 * @property array|null $tags
 * @property string|null $start_date
 * @property string|null $end_date
 * @property int|null $max_seats
 * @property bool $auto_evaluate
 * @property string $status
 * @property string|null $rejection_reason
 * @property \Illuminate\Support\Carbon|null $published_at
 * @property array $selected_units
 * @property array $selected_competencies
 * @property array $selected_sub_competencies
 * @property \Illuminate\Support\Carbon $created_at
 * @property \Illuminate\Support\Carbon $updated_at
 */
class LmsCourse extends Model
{
    use SoftDeletes;

    protected $table = 'lms_courses';

    protected $fillable = [
        'created_by',
        'title',
        'short_description',
        'description',
        'description_json',
        'overview_table',
        'category',
        'level',
        'language',
        'thumbnail',
        'duration',
        'cpd_hours',
        'tags',
        'start_date',
        'end_date',
        'max_seats',
        'auto_evaluate',
        'status',
        'rejection_reason',
        'published_at',
        'selected_units',
        'selected_competencies',
        'selected_sub_competencies',
    ];

    protected $casts = [
        'auto_evaluate' => 'boolean',
        'start_date' => 'date',
        'end_date' => 'date',
        'published_at' => 'datetime',
        'description_json' => 'array',
        'overview_table' => 'array',
        'tags' => 'array',
        'selected_units' => 'array',
        'selected_competencies' => 'array',
        'selected_sub_competencies' => 'array',
    ];

    // Append category_name to serialization
    protected $appends = ['category_name', 'level_name'];

    // Accessor to get the category name from the user_categories table
    public function getCategoryNameAttribute(): ?string
    {
        if (!$this->category) {
            return null;
        }
        
        $category = \App\Models\UserCategory::find($this->category);
        return $category ? $category->name : null;
    }

    // Accessor to get the level/user type name from the user_types table
    public function getLevelNameAttribute(): ?string
    {
        if (!$this->level) {
            return null;
        }
        
        $userType = \App\Models\UserType::find($this->level);
        return $userType ? $userType->name : null;
    }

    // Relationships
    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function settings(): HasOne
    {
        return $this->hasOne(LmsCourseSettings::class, 'course_id');
    }

    public function sections(): HasMany
    {
        return $this->hasMany(LmsCourseSection::class, 'course_id')->orderBy('order');
    }

    public function competencies(): HasMany
    {
        return $this->hasMany(LmsCourseCompetency::class, 'course_id')->orderBy('order');
    }

    public function access(): HasOne
    {
        return $this->hasOne(LmsCourseAccess::class, 'course_id');
    }

    public function enrollments(): HasMany
    {
        return $this->hasMany(LmsCourseEnrollment::class, 'course_id');
    }

    // Scopes
    public function scopePublished($query)
    {
        return $query->where('status', 'PUBLISHED');
    }

    public function scopeDraft($query)
    {
        return $query->where('status', 'DRAFT');
    }

    public function scopeByCreator($query, $userId)
    {
        return $query->where('created_by', $userId);
    }

    // Helper methods
    public function isDraft(): bool
    {
        return $this->status === 'DRAFT';
    }

    public function isPublished(): bool
    {
        return $this->status === 'PUBLISHED';
    }

    public function isPendingReview(): bool
    {
        return $this->status === 'PENDING_REVIEW';
    }
}
