<?php

namespace App\Http\Controllers;

use App\Models\AssessmentAttempt;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Http\Request;

class PdfReportController extends Controller
{
    public function assessment(string $assessment, AssessmentAttempt $attempt)
    {
        abort_unless($attempt->status === 'completed', 404);

        $attempt->load(['assessment.questions', 'band']);

        $labels = $attempt->assessment->isProfileMode()
            ? collect($attempt->assessment->profileTypes())
            : $attempt->assessment->questions->groupBy('dimension')->map(fn ($q) => $q->first()->section);

        $radarItems = collect($attempt->dimension_percents ?? [])
            ->mapWithKeys(fn ($pct, $dim) => [(string) ($labels[$dim] ?? ucfirst($dim)) => $pct])
            ->all();

        return Pdf::loadView('pdf.assessment-report', [
            'attempt' => $attempt,
            'gaugeImg' => \App\Support\ReportChart::gauge((int) round($attempt->percent)),
            'radarImg' => \App\Support\ReportChart::radar($radarItems),
        ])->download("moneywise-{$attempt->assessment->slug}-report.pdf");
    }

    public function tool(Request $request, string $tool)
    {
        $titles = [
            'budget' => 'Monthly Budget',
            'debt-payoff' => 'Debt Payoff Plan',
            'compound-growth' => 'Compound Growth Projection',
            'emergency-fund' => 'Emergency Fund Plan',
            'wealth-goals' => 'Wealth & Goals Plan',
            'values-clarification' => 'My Money Compass',
            'loan-cost' => 'True Cost of Loan Report',
            'retirement' => 'Retirement Readiness Plan',
        ];

        abort_unless(isset($titles[$tool]), 404);

        $data = $request->validate([
            'currency' => ['nullable', 'string', 'max:5'],
            'highlight' => ['nullable', 'array'],
            'highlight.value' => ['required_with:highlight', 'string', 'max:80'],
            'highlight.label' => ['required_with:highlight', 'string', 'max:120'],
            'sections' => ['required', 'array', 'max:8'],
            'sections.*.title' => ['required', 'string', 'max:120'],
            'sections.*.rows' => ['required', 'array', 'max:30'],
            'sections.*.rows.*' => ['array', 'size:2'],
            'sections.*.rows.*.*' => ['nullable', 'string', 'max:160'],
            'note' => ['nullable', 'string', 'max:500'],
            'chart' => ['nullable', 'string', 'starts_with:data:image/', 'max:3000000'],
            'chartTitle' => ['nullable', 'string', 'max:120'],
        ]);

        return Pdf::loadView('pdf.tool-report', [
            'title' => $titles[$tool],
            'currency' => $data['currency'] ?? null,
            'highlight' => $data['highlight'] ?? null,
            'sections' => $data['sections'],
            'note' => $data['note'] ?? null,
            'chart' => $data['chart'] ?? null,
            'chartTitle' => $data['chartTitle'] ?? null,
        ])->download("moneywise-{$tool}-report.pdf");
    }
}
