<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PerbandinganController extends Controller
{
     public function index()
    {
        $dataWAFile = storage_path('app/public/dataWA_matang_dengan_klasifikasi.csv');
        $dataTeleFile = storage_path('app/public/dataTele_matang_dengan_klasifikasi.csv');

        $dataWA = $this->readCsv($dataWAFile);
        $dataTele = $this->readCsv($dataTeleFile);

        // Menghitung jumlah masing-masing kelas di field 'label_svm'
        $waCounts = $this->countLabels($dataWA, 4); // Asumsikan 'label_svm' ada di kolom ke-5 (index 4)
        $teleCounts = $this->countLabels($dataTele, 4); // Asumsikan 'label_svm' ada di kolom ke-5 (index 4)

        return view('dashboard', [
            'waCounts' => array_values($waCounts),
            'teleCounts' => array_values($teleCounts)
        ]);
    }

    private function readCsv($file)
    {
        $rows = [];
        if (($handle = fopen($file, 'r')) !== false) {
            while (($data = fgetcsv($handle, 1000, ',')) !== false) {
                $rows[] = $data;
            }
            fclose($handle);
        }
        return $rows;
}
    private function countLabels($data, $columnIndex)
    {
        $counts = ['fitur' => 0, 'performa' => 0, 'tampilan' => 0];
        foreach ($data as $key => $row) {
            if ($key > 0 && isset($row[$columnIndex])) { // Lewati header
                $label = $row[$columnIndex];
                if (isset($counts[$label])) {
                    $counts[$label]++;
                }
            }
        }
        return $counts;
    }
}
