<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Course extends Model
{
    use HasFactory;

    protected $fillable = [
        'parent_id',
        'discipline_id',
        'name',
        'course_type_id',
        'causal_id',
        'max_members',
        'instructor',
        'price',
        'months',
        'date_from',
        'date_to',
        'course_duration_id',
        'course_frequency_id',
        'course_level_id',
        'enabled',
        'year',
        'subscription_price',
        'sub_causal_id',
        'category_id',
        'active',
        'type',
        'when',
        'prices',
        'instructor_id'
    ];

    public function parent()
    {
        return $this->belongsTo(Course::class);
    }

    public function discipline()
    {
        return $this->belongsTo(Discipline::class, 'discipline_id');
    }

    public function type()
    {
        return $this->belongsTo(CourseType::class, 'course_type_id');
    }

    public function duration()
    {
        return $this->belongsTo(CourseDuration::class, 'course_duration_id');
    }

    public function frequency()
    {
        return $this->belongsTo(CourseFrequency::class, 'course_frequency_id');
    }

    public function level()
    {
        return $this->belongsTo(CourseLevel::class, 'course_level_id');
    }

    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id');
    }

    public function member()
    {
        return $this->belongsTo(Member::class);
    }

    public function getins()
    {
        return $this->belongsTo(User::class, 'instructor_id');
    }

    public function childs() {
        return $this->hasMany(\App\Models\Course::class,'parent_id','id') ;
    }

    public function getTree()
    {
        $str = '';
        if ($this->parent_id != null)
        {
            $a = $this->recursiveName($this->parent_id, array($this->name));
            $a = array_reverse($a);
            $str = implode(" - ", $a);
        }
        else
        {
            $str = $this->name;
        }
        return $str;
    }

    public function recursiveName($parent_id, $array)
    {
        $x = \App\Models\Course::findOrFail($parent_id);
        $array[] = $x->name;

        if ($x->parent_id != null)
        {
            return $this->recursiveName($x->parent_id, $array);
        }
        else
        {
            return $array;
        }
    }

    public function recursiveParent($parent_id, $array)
    {
        if ($parent_id == null)
            return $array;
        $x = \App\Models\Course::findOrFail($parent_id);
        $array[] = $x->id;

        if ($x->parent_id != null)
        {
            return $this->recursiveParent($x->parent_id, $array);
        }
        else
        {
            return $array;
        }
    }

    public function getCount()
    {
        return \App\Models\MemberCourse::where('course_id', $this->id)->count();
    }
    public function getTypeFieldAttribute()
    {
        return $this->attributes['type'] ?? null;
    }

    // Method to get type field with capitalization
    public function getFormattedTypeField()
    {
        return ucfirst($this->attributes['type'] ?? 'No Type');
    }

    public function getDetailsName() {
        $courseDiscipline = $this->discipline?->name ?? '';
        $courseName = $this->name ?? 'Corso Sconosciuto';
        $levelName = is_object($this->level) ? $this->level->name : '';
        $frequencyName = is_object($this->frequency) ? $this->frequency->name : '';
        $typeName = $this->getFormattedTypeField() ?? '';

        // Build display name with level and frequency
        $displayNameParts = [];
        if ($courseDiscipline) $displayNameParts[] = $courseDiscipline;
        
        $displayNameParts[] = $courseName;
        if ($levelName) $displayNameParts[] = $levelName;
        // if ($typeName) $displayNameParts[] = $typeName;
        if ($frequencyName) $displayNameParts[] = $frequencyName;

        return implode(' - ', $displayNameParts);
    }
}
