<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Batch extends Model
{
    use HasFactory, SoftDeletes;
    protected $fillable = [
        'id',
        'batch_mode',
        'batch_name',
        'batch_code',
        'start_date',
        'number_of_classes',
        'tutor_id',
        'address_id',
        'status',
        'notes',
        'user_ids',
        'capacity',
        'created_by',
        'updated_by',
        'plan_id',
        'class_duration',
        'class_time',
        'meeting_id',
        'meeting_password',
        'deleted_by',
    ];

    public function scopeSearchQuery($query)
    {
      
        if (request('keyword')) {
            foreach ($this->fillable as $column) {
                switch ($column) {
                    case 'id':
                        $query->orWhere($column, 'LIKE', '%' . request('keyword') . '%');
                      break;
                    case 'batch_name':
                      $query->orWhere($column, 'LIKE', '%' . request('keyword') . '%');
                      break;
                      case 'batch_code':
                        $query->orWhere($column, 'LIKE', '%' . request('keyword') . '%');
                        break;
                    
                    default:
                        $query->orWhere($column, 'LIKE', '%' . request('keyword') . '%');
                        break;
                }
            }
        }
        return $query;
    }

    public function candidates()
    {
        return $this->belongsToMany(User::class, 'batch_user', 'batch_id', 'user_id')
                    ->withTimestamps(); // Assuming you want to include timestamps in the pivot table
    }

    
    public function plans()
    {
        return $this->belongsTo(\Modules\Plan\Entities\Plan::class,'plan_id');
    }
}
