<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Course extends Model
{
    protected $fillable = [
        'title',
        'description',
        'popular',
        'price',
        'img',
        'category_id',
    ];

    protected function casts(): array
    {
        return [
            'price' => 'float',
        ];
    }

    public function getCreatedAtAttribute($date)
    {
        return Carbon::createFromTimestamp(strtotime($date))->format('Y-m-d h:i A');
    }

    public function reviews()
    {
        return $this->hasMany(Review::class);
    }


    public function getUpdatedAtAttribute($date)
    {
        return Carbon::createFromTimestamp(strtotime($date))->format('Y-m-d h:i A');
    }

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

    public function lectures()
    {
        return $this->hasMany(Lecture::class);
    }

    public function subscriptions()
    {
        return $this->hasMany(Subscription::class);
    }

    public function banner()
    {
        return $this->hasOne(Banner::class);
    }

    public function views()
    {
        return $this->hasMany(LectureViews::class);
    }
}
