<?php

namespace App\Http\Controllers;

use App\Models\RatingType;
use App\Models\RatingTypeMetric;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class RatingTypeMetricController extends Controller
{
    // Display the list of metrics for a specific rating type
    public function index(RatingType $ratingType)
    {
        $metrics = $ratingType->metrics;
        return view('rating_types.metrics_index', compact('ratingType', 'metrics'));
    }

    public function store(Request $request, RatingType $ratingType)
    {
        $validatedData = $request->validate([
            'metric_value' => 'required|numeric|between:0,100',
        ]);

        // Handle numeric rating type with min/max values
        $data = $request->all();
        if ($ratingType->type === 'numeric' || $ratingType->type === 'percentage') {
            $request->validate([
                'min_value' => 'required|numeric',
                'max_value' => 'required|numeric|gt:min_value',
            ]);
            $rating_value = $request->min_value . '-' . $request->max_value;
        } else {
            $request->validate([
                'rating_value' => 'required|string',
            ]);
            $rating_value = $request->rating_value;
        }

        $metric = RatingTypeMetric::create([
            'organisation_id' => Auth::user()->organisation_id,
            'rating_type_id' => $ratingType->id,
            'rating_value' => $rating_value,
            'metric_value' => $request->metric_value,
        ]);

        if ($request->ajax()) {
            return response()->json([
                'success' => true,
                'message' => __('Metric added successfully.'),
                'metric' => $metric
            ]);
        }

        return redirect()->route('rating_types.metrics.index', $ratingType)->with('success', __('Metric added successfully.'));
    }

    public function update(Request $request, RatingType $ratingType, RatingTypeMetric $metric)
    {
        $validatedData = $request->validate([
            'metric_value' => 'required|numeric|between:0,100',
        ]);

        // Handle numeric rating type with min/max values
        if ($ratingType->type === 'numeric') {
            $request->validate([
                'min_value' => 'required|numeric',
                'max_value' => 'required|numeric|gt:min_value',
            ]);
            $rating_value = $request->min_value . '-' . $request->max_value;
        } else {
            $request->validate([
                'rating_value' => 'required|string',
            ]);
            $rating_value = $request->rating_value;
        }

        $metric->update([
            'rating_value' => $rating_value,
            'metric_value' => $request->metric_value,
        ]);

        if ($request->ajax()) {
            return response()->json([
                'success' => true,
                'message' => __('Metric updated successfully.'),
                'metric' => $metric
            ]);
        }

        return redirect()->route('rating_types.metrics.index', $ratingType)->with('success', __('Metric updated successfully.'));
    }

    public function destroy(RatingType $ratingType, RatingTypeMetric $metric)
    {
        $metric->delete();

        if (request()->ajax()) {
            return response()->json([
                'success' => true,
                'message' => __('Metric deleted successfully.')
            ]);
        }

        return redirect()->route('rating_types.metrics.index', $ratingType)->with('success', __('Metric deleted successfully.'));
    }
}
