<?php

namespace App\Exports;

use App\Models\Confidence;
use App\Models\Proses;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

class ProsesExport implements FromCollection, WithHeadings
{

    protected $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        $proses = Proses::findOrFail($this->id);
        $itemset2 = Confidence::where('itemset', '2-item')->where('proses_id', $this->id)->get();
        $itemset3 = Confidence::where('itemset', '3-item')->where('proses_id', $this->id)->get();
        $rules = Confidence::where('proses_id', $this->id)->where('keterangan', 'Lolos')->get();

        // Merge all data into a single collection
        $data = $itemset2->merge($itemset3)->merge($rules);

        // Transform data as per the required columns
        $exportData = $data->map(function ($item, $key) {
            return [
                'No' => $key + 1,
                'Jenis Obat' => $item->items,
                'Keterangan' => $item->keterangan,
                'Confidence' => $item->confidence,
                'Lift Ratio' => $item->lift_ratio,
                'Korelasi' => $item->korelasi,
            ];
        });

        return $exportData;
    }

    public function headings(): array
    {
        return [
            'No',
            'Jenis Obat',
            'Keterangan',
            'Confidence',
            'Lift Ratio',
            'Korelasi',
        ];
    }
}
