<?php

namespace App\Http\Controllers;

use App\Models\Submission;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Routing\Controllers\Middleware;

class SubmissionController extends Controller implements HasMiddleware
{
    public static function middleware(): array
    {
        return [new Middleware('can:lms.update')];
    }

    public function grade(Request $request, Submission $submission): RedirectResponse
    {
        $data = $request->validate([
            'marks' => ['nullable', 'numeric', 'min:0'],
            'feedback' => ['nullable', 'string', 'max:500'],
            'status' => ['required', 'in:submitted,graded,returned'],
        ]);

        $submission->update($data + ['graded_by' => auth()->id()]);

        return back()->with('success', 'Submission graded.');
    }
}
