<?php

namespace App\Http\Controllers;

use App\Models\ApprovalWorkflow;
use App\Models\Document;
use App\Notifications\ApprovalRequestNotification;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;

class ApprovalWorkflowController extends Controller
{
    /**
     * 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)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(ApprovalWorkflow $approvalWorkflow)
    {
        //
    }

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

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

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

    public function initiateApproval(Document $document)
    {
        $workflow = $document->project->approvalWorkflow;
        $currentStep = $workflow->getCurrentStep($document);
        
        $document->approvals()->create([
            'user_id' => auth()->id(),
            'status' => 'pending',
            'step_index' => 0,
            'required_role' => $currentStep['role']
        ]);
        
        Notification::sendUsersWithRole($currentStep['role'])->notify(
            new ApprovalRequestNotification($document, $auth()->user())
        );
    }
}
