﻿<?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 Bootcamp extends Model
{
protected $guarded  = [];
    use HasFactory;

    protected $fillable = [
        'user_id',
        'title',
        'slug',
        'category_id',
        'description',
        'short_description',
        'is_paid',
        'price',
        'discount_flag',
        'discounted_price',
        'publish_date',
        'thumbnail',
        'faqs',
        'requirements',
        'outcomes',
        'meta_keywords',
        'meta_description',
        'status',
        'pending',
        'organisation_id'
    ];

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


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

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