<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Filiale;
use App\Models\Form;
use App\Models\FormSubmission;
use Illuminate\View\View;

class SubmissionsController extends Controller
{
    public function overview(): View
    {
        $filiales = Filiale::with('user')
            ->withCount('submissions')
            ->orderBy('name')
            ->get();

        return view('admin.submissions.overview', compact('filiales'));
    }

    public function byFiliale(Filiale $filiale): View
    {
        $submissions = $filiale->submissions()
            ->with(['form', 'user'])
            ->latest('submitted_at')
            ->paginate(15);

        return view('admin.submissions.by-filiale', compact('filiale', 'submissions'));
    }

    public function index(Form $form): View
    {
        $submissions = $form->submissions()
            ->with(['filiale', 'user', 'values.field'])
            ->latest()
            ->paginate(15);

        return view('admin.submissions.index', compact('form', 'submissions'));
    }

    public function show(FormSubmission $submission): View
    {
        $this->authorize('view', $submission);

        $submission->load([
            'form',
            'filiale',
            'user',
            'values.field',
            'revisions' => fn ($query) => $query->with('user')->latest(),
        ]);

        return view('admin.submissions.show', compact('submission'));
    }
}
