<?php

namespace Modules\SmartStorage\Entities;

use Illuminate\Database\Eloquent\Model;

class SsFile extends Model
{
    protected $table = 'ss_files';

    protected $guarded = ['id'];

    protected $casts = [
        'is_encrypted' => 'boolean',
        'size'         => 'integer',
        'stored_size'  => 'integer',
    ];

    public function uploaded_by_user()
    {
        return $this->belongsTo(\App\User::class, 'uploaded_by');
    }

    public static function findByLogicalPath(string $logicalPath, $businessId = null): ?self
    {
        $query = static::where('logical_path', $logicalPath);
        if ($businessId !== null) {
            $query->where(function ($q) use ($businessId) {
                $q->where('business_id', $businessId)->orWhereNull('business_id');
            });
        }
        return $query->first();
    }
}
