<?php

namespace App\Http\Controllers;

use App\Models\UserSignDocument;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class UserSignDocumentController extends Controller
{
    public function store(Request $request)
    {
        $request->validate([
            'fk_id_user' => 'required',
            'file'       => 'required|file|mimes:pdf|max:10240',
        ]);

        $file     = $request->file('file');
        $fileName = time() . '_' . $file->getClientOriginalName();
        $filePath = $file->storeAs('sign_documents', $fileName, 'public');

        $document = UserSignDocument::create([
            'fk_id_user'  => $request->fk_id_user,
            'file_name'   => $fileName,
            'file_path'   => $filePath,
            'deleted'     => 0,
            'uploaded_at' => now(),
        ]);

        return response()->json([
            'message'  => 'Documento guardado correctamente',
            'document' => $document,
        ], 201);
    }
}