<?php

declare(strict_types=1);

namespace App\Http\Controllers\Studio;

use App\Http\Controllers\Controller;
use App\Models\Group;
use App\Services\GroupDiscoveryService;
use App\Services\GroupReputationService;
use App\Services\GroupService;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class GroupReputationStudioController extends Controller
{
    public function __construct(
        private readonly GroupService $groups,
        private readonly GroupReputationService $reputation,
        private readonly GroupDiscoveryService $discovery,
    ) {
    }

    public function show(Request $request, Group $group): Response
    {
        $this->authorize('viewReputationDashboard', $group);

        $this->reputation->refreshGroup($group);
        $metrics = $this->discovery->refresh($group);

        return Inertia::render('Studio/StudioGroupReputation', [
            'title' => $group->name . ' Reputation',
            'description' => 'Review contributor reliability, badge unlocks, and internal trust metrics.',
            'studioGroup' => $this->groups->mapGroupDetail($group, $request->user()),
            'reputation' => $this->reputation->summary($group),
            'trustSignals' => $this->reputation->trustSignals($group),
            'metrics' => [
                'freshness_score' => (float) $metrics->freshness_score,
                'activity_score' => (float) $metrics->activity_score,
                'release_score' => (float) $metrics->release_score,
                'trust_score' => (float) $metrics->trust_score,
                'collaboration_score' => (float) $metrics->collaboration_score,
                'last_calculated_at' => $metrics->last_calculated_at?->toISOString(),
            ],
        ]);
    }
}