<?php

namespace App\Models;

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

class UserDocuments extends Model
{
    use HasFactory;
     /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'user_id',
        'doc_name',
        'doc_number',
        'doc_page',
        'description',
        'path',
        'uploaded_name',
        'saved_as',
        'status',
        'created_by'
    ];

    
    public function uploadFile($userDoc, $user, $postData = []){
        ini_set('memory_limit', '1024M');
        ini_set('max_execution_time', 180); //3 minutes
        
        $file = $userDoc['doc'];
        $savedAs = $user['id'] . '_'.$userDoc['doc_name'].'_'.$userDoc['doc_page'].'.'.$file->extension();
        $filePath = $file->storeAs('uploads', $savedAs, 'public');
        $userDocs = [
            'user_id' => $user['id'],
            'doc_name' => $userDoc['doc_name'],
            'doc_number' => $userDoc['doc_number'],
            'doc_page' =>  $userDoc['doc_page'],
            'description' => isset($userDoc['description']) ? $userDoc['description'] : '',
            'path' => '/'.$filePath,
            'uploaded_name' => $file->getClientOriginalName(),
            'saved_as' => $savedAs,
            'verify' => isset($userDoc['verify']) ? $userDoc['verify'] : 0,
            'created_by' => Auth::id(),
        ];
        $userDocs = self::create($userDocs);
        return $userDocs;
    }
}
