<?php

namespace App\Models;

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

class MemberCourse extends Model
{
    use HasFactory;

    protected $fillable = [
        'member_id',
        'course_id',
        'date_from',
        'date_to',
        'when',
        'course_type_id',
        'status',
        'price',
        'subscription_price',
        'months',
        'days',
        'course_subscription_id',
        'notes',
        'subscribed'
    ];

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

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

    public function courseSubscription()
    {
        return $this->belongsTo(CourseSubscription::class);
    }

    public function getStatus()
    {
        $status = 0;
        $rates = \App\Models\Rate::where('member_course_id', $this->id)->orderBy('date')->get();
        foreach($rates as $rate)
        {
            if ($rate->date > date("Y-m-d"))
            {
                break;
            }
            $status = $rate->status;
        }
        return $status;
    }
}
