<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Client;
use App\Services\Operations\ClientHealthScoreService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class HealthScoreController extends Controller
{
    public function __construct(protected ClientHealthScoreService $healthScores)
    {
    }

    public function index(Request $request): View
    {
        $filters = $request->only(['q', 'platform', 'status', 'health_status', 'min_score', 'max_score']);
        $data = $this->healthScores->dashboard($filters);

        return view('admin.health-scores.index', compact('data', 'filters'));
    }

    public function show(Client $client): View
    {
        $data = $this->healthScores->clientDetail($client);

        return view('admin.health-scores.show', compact('client', 'data'));
    }

    public function calculateAll(Request $request): RedirectResponse
    {
        $filters = $request->only(['platform', 'status']);
        $result = $this->healthScores->storeAll($filters);

        return back()->with('success', $result['message']);
    }

    public function calculateClient(Client $client): RedirectResponse
    {
        $score = $this->healthScores->calculateAndStore($client);

        return back()->with('success', $client->company_name.' sağlık skoru güncellendi: '.$score->score.'/100');
    }
}
