<?php

namespace App\Http\Controllers\Public;

use App\Http\Controllers\Controller;
use App\Models\Classification;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\Http;

class ClassificationController extends Controller
{
    public function index()
    {
        $classifications = Classification::where('user_id', auth()->id())->get();
        return inertia('user/classification/ClassificationIndex', [
            'classifications' => $classifications,
        ]);
    }


    public function predict(Request $request)
    {
        $request->validate([
            'image' => 'required|image|max:2048',
        ]);

        $image = $request->file('image');

        try {
            // 1. Kirim ke AI Server untuk prediksi
            $response = Http::attach(
                'image',
                file_get_contents($image),
                $image->getClientOriginalName()
            )->post('http://127.0.0.1:5001/predict');

            if ($response->failed()) {
                return response()->json(['error' => 'Gagal terhubung ke AI server'], 500);
            }

            $data = $response->json();

            // 2. Jika sukses, simpan gambar dan hasil ke database
            $path = $image->store('classifications', 'public');

            $confidence = (float) filter_var($data['confidence'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
            $status = $confidence >= 90 ? 'Berhasil' : 'Gagal';

            Classification::create([
                'user_id' => auth()->id() ?? null,
                'image_path' => $path,
                'result' => $data['label'],
                'confidence' => $confidence,
                'status' => $status,
            ]);

            return response()->json([
                'label' => $data['label'],
                'confidence' => $data['confidence'],
                'message' => 'Hasil klasifikasi berhasil disimpan'
            ]);
        } catch (\Exception $e) {
            return response()->json(['error' => 'Terjadi kesalahan: ' . $e->getMessage()], 500);
        }
    }
    public function destroy(Classification $classification)
    {
        if ($classification->user_id !== auth()->id()) {
            abort(403);
        }

        // Hapus file dari storage jika ada
        if ($classification->image_path && \Storage::disk('public')->exists($classification->image_path)) {
            \Storage::disk('public')->delete($classification->image_path);
        }

        $classification->delete();

        return back()->with('success', 'Klasifikasi berhasil dihapus');
    }
}