<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Workflow\WorkflowController;
use App\Http\Controllers\Workflow\WorkflowStepController;
use App\Http\Controllers\Workflow\SettingsController;
use App\Http\Controllers\Workflow\WorkflowTransitionController;
use App\Http\Controllers\Workflow\WorkflowTaskController;
use App\Http\Controllers\Workflow\WorkflowDocumentController;

Route::middleware(['web', 'auth'])->prefix('workflow')->name('workflow.')->group(function () {
    // System Audit page (must be before /{workflow} routes)
    Route::get('/audit', [WorkflowController::class, 'auditHistory'])
        ->middleware('can:View System Logs')
        ->name('audit');

    // Document creation for workflow instance
    Route::post('/document', [\App\Http\Controllers\Workflow\WorkflowDocumentController::class, 'store'])->name('document.store');
    // My Workflows (assigned to me)
    Route::get('/myworkflow', [\App\Http\Controllers\Workflow\MyWorkflowController::class, 'index'])->name('myworkflow')->middleware('can:My Workflows');
    // Workflow CRUD
    Route::get('/', [WorkflowController::class, 'index'])->name('index');
    Route::get('/create', [WorkflowController::class, 'create'])->name('create');
    Route::post('/', [WorkflowController::class, 'store'])->name('store');
    Route::get('/{workflow}', [WorkflowController::class, 'show'])->name('show');
    Route::get('/{workflow}/edit', [WorkflowController::class, 'edit'])->name('edit');
    Route::put('/{workflow}', [WorkflowController::class, 'update'])->name('update');
    Route::delete('/{workflow}', [WorkflowController::class, 'destroy'])->name('destroy');

    // Workflow Steps
    Route::get('/{workflow}/steps/create', [WorkflowStepController::class, 'create'])->name('steps.create');
    Route::post('/{workflow}/steps', [WorkflowStepController::class, 'store'])->name('steps.store');
    Route::get('/{workflow}/steps/{step}/edit', [WorkflowStepController::class, 'edit'])->name('steps.edit');
    Route::put('/{workflow}/steps/{step}', [WorkflowStepController::class, 'update'])->name('steps.update');
    Route::delete('/{workflow}/steps/{step}', [WorkflowStepController::class, 'destroy'])->name('steps.destroy');

    // Workflow Settings
    Route::get('/workflow/settings', [SettingsController::class, 'index'])->name('settings');
    Route::post('/settings/save', [SettingsController::class, 'save'])->name('settings.save');

    // Workflow Transitions
    Route::get('/{workflow}/transitions/create', [WorkflowTransitionController::class, 'create'])->name('transitions.create');
    Route::post('/{workflow}/transitions', [WorkflowTransitionController::class, 'store'])->name('transitions.store');
    Route::get('/{workflow}/transitions/{transition}/edit', [WorkflowTransitionController::class, 'edit'])->name('transitions.edit');
    Route::put('/{workflow}/transitions/{transition}', [WorkflowTransitionController::class, 'update'])->name('transitions.update');

    // Workflow Tasks
    Route::get('/tasks', [WorkflowTaskController::class, 'index'])->name('tasks.index');
    Route::get('/tasks/{id}', [WorkflowTaskController::class, 'show'])->name('tasks.show');

    // Move task to next transition
    Route::post('/tasks/{id}/transition', [WorkflowTaskController::class, 'transition'])->name('tasks.transition');
    Route::post('/tasks/{id}/status', [WorkflowTaskController::class, 'updateStatus'])->name('tasks.updateStatus');
    Route::get('/document/{id}', [WorkflowDocumentController::class, 'show'])->name('documents.show');
    Route::get('/document/{id}/file', [WorkflowDocumentController::class, 'viewFile'])->name('documents.file');

});
