<?php

namespace App\Http\Controllers;

use App\Jobs\ProcessDocumentOCR;
use App\Models\Document;
use App\Models\Project;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class DocumentController extends Controller
{
    public $comments=[];


    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'files.*' => 'required|file|mimes:pdf,docx,xlsx,jpg,png|max:5120',
            'project_id' => 'required|exists:projects,id',
            'folder_id' => 'nullable|exists:folders,id'
        ]);
        
        foreach ($request->file('files') as $file) {
            $document = Document::create([
                'name' => $file->getClientOriginalName(),
                'project_id' => $request->project_id,
                'folder_id' => $request->folder_id,
                'created_by' => auth()->id()
            ]);
            
            $document->addMedia($file)->toMediaCollection('documents');
            ProcessDocumentOCR::dispatch($document->currentVersion);
        }
        
        return redirect()->back()->with('success', 'Files uploaded successfully');
    }

    /**
     * Display the specified resource.
     */
    public function show(Document $document)
    {
        $this->authorize('view', $document); // Si usas políticas

        if (!Storage::exists($document->file_path)) {
            abort(404);
        }

        $document->url = Storage::url($document->file_path);

        return view('documents.show', [
            'document' => $document,
            'versions' => $document->versions()->latest()->get(),
            'comments' => $this->comments,
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Document $document)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Document $document)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Document $document)
    {
        //
    }

    public function upload(Request $request, Project $project)
    {
        $request->validate([
            'files.*' => 'required|mimes:pdf,docx,xlsx,jpeg,png|max:2048'
        ]);
        
        foreach ($request->file('files') as $file) {
            $document = $project->documents()->create([
                'name' => $file->getClientOriginalName(),
                'status' => 'pending'
            ]);
            
            $this->createVersion($document, $file);
        }
    }

    private function createVersion(Document $document, $file)
    {
        $version = $document->versions()->create([
            'file_path' => $file->store("projects/{$document->project_id}/documents"),
            'hash' => hash_file('sha256', $file),
            'user_id' => auth()->id()
        ]);
        
        $document->update(['current_version_id' => $version->id]);
    }
}
