<?php

namespace App\Traits;

use Illuminate\Support\Facades\Schema;

trait ConditionalSoftDeletes
{
    /**
     * Check if the table has a deleted_at column
     */
    protected function hasSoftDeletesColumn()
    {
        return Schema::hasColumn($this->getTable(), 'deleted_at');
    }
    
    /**
     * Override the boot method to conditionally add global scope
     */
    public static function bootConditionalSoftDeletes()
    {
        static::addGlobalScope('conditionalSoftDeletes', function ($query) {
            $instance = new static;
            if ($instance->hasSoftDeletesColumn()) {
                $query->whereNull($instance->getTable() . '.deleted_at');
            }
        });
    }
    
    /**
     * Get the name of the "deleted at" column.
     */
    public function getDeletedAtColumn()
    {
        return $this->hasSoftDeletesColumn() ? 'deleted_at' : null;
    }
}