﻿<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Scopes\OrganisationScope;
use Illuminate\Support\Facades\Auth;

class Course extends Model
{
    protected $guarded  = [];
    use HasFactory;




    protected static function booted()
    {
        static::addGlobalScope(new OrganisationScope);

        // Automatically set the organisation_id before creating the model
        static::creating(function ($model) {
            $model->organisation_id = Auth::user()->organisation_id;
        });
    }

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

    public function sections()
    {
        return $this->hasMany(Section::class);
    }

    public function lessons()
    {
        return $this->hasMany(Lesson::class);
    }

    public function enrollments()
    {
        return $this->hasMany(Enrollments::class);
    }

    public function wishlists()
    {
        $query = $this->hasMany(Wishlist::class);

        if (Auth::user()) {
            $query->where('user_id', Auth::user()->id);
        }

        return $query;
    }

    public function creator()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function instructors($instructors_ids = array())
    {
        if (!$instructors_ids) {
            $instructors_ids = json_decode($this->instructors, true);
        } elseif (!is_array($instructors_ids)) {
            $instructors_ids = json_decode($instructors_ids, true);
        }

        if (!is_array($instructors_ids)) {
            $instructors_ids = array();
        }

        return User::whereIn('id', $instructors_ids)->get();
    }

    function total_second()
    {
        return $this->hasMany(Lesson::class)
            ->select('id')
            ->selectRaw('SUM(TIME_TO_SEC(duration)) as total_time')
            ->groupBy('id')
            ->first()->total_time;
    }

    function total_duration()
    {
        $total_seconds = $this->hasMany(Lesson::class)
            ->select('id')
            ->selectRaw('SUM(TIME_TO_SEC(duration)) as total_time')
            ->groupBy('id')
            ->first()->total_time;


        $hours = floor($total_seconds / 3600); // Calculate the number of hours
        $minutes = floor(($total_seconds / 60) % 60); // Calculate the number of minutes
        $total_seconds = $total_seconds % 60; // Calculate the remaining seconds

        $duration = sprintf("%02d:%02d:%02d", $hours, $minutes, $total_seconds);
        return $duration;
    }