<?php

namespace App\Http\Controllers;

use App\Models\Document;
use App\Notifications\DocumentStatusChanged;
use Illuminate\Http\Request;

class ApprovalController extends Controller
{
    //

    public function updateStatus(Request $request, Document $document)
    {
        $validated = $request->validate([
            'status' => 'required|in:approved,rejected',
            'comment' => 'required_if:status,rejected'
        ]);
        
        $document->approvals()->create([
            'user_id' => auth()->id(),
            'status' => $validated['status'],
            'comment' => $validated['comment'] ?? null
        ]);
        
        $document->update(['status' => $validated['status']]);
        
        event(new DocumentStatusChanged($document, $validated['status']));
    }
}
