<?php

declare(strict_types=1);

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\BugReport;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

/**
 * BugReportController — /bug-report
 *
 * GET  /bug-report        → show form (guests see a login prompt)
 * POST /bug-report        → authenticated users submit a report
 */
final class BugReportController extends Controller
{
    public function show(Request $request): View
    {
        return view('web.bug-report', [
            'page_title'            => 'Bug Report — Skinbase',
            'page_meta_description' => 'Submit a bug report or suggestion to the Skinbase team.',
            'page_canonical'        => url('/bug-report'),
            'hero_title'            => 'Bug Report',
            'hero_description'      => 'Found something broken? Submit a report and our team will look into it.',
            'breadcrumbs'           => collect([
                (object) ['name' => 'Home', 'url' => '/'],
                (object) ['name' => 'Bug Report', 'url' => '/bug-report'],
            ]),
            'success' => session('bug_report_success', false),
            'center_content' => true,
            'center_max'     => '3xl',
        ]);
    }

    public function submit(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'subject'     => ['required', 'string', 'max:255'],
            'description' => ['required', 'string', 'max:5000'],
        ]);

        BugReport::create([
            'user_id'     => $request->user()->id,
            'subject'     => $validated['subject'],
            'description' => $validated['description'],
            'ip_address'  => $request->ip(),
            'user_agent'  => substr($request->userAgent() ?? '', 0, 512),
            'status'      => 'open',
        ]);

        return redirect()->route('bug-report')->with('bug_report_success', true);
    }
}
