<?php

namespace App\Exports;

use App\Models\HasilPrediksi;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;

class HasilPrediksiExport implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize
{
    protected $id;
    private $rowNumber = 0;

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

    public function collection() {
        return HasilPrediksi::where('user_id', $this->id)
            ->orderBy('tanggal', 'asc')
            ->get();
    }

    public function headings(): array {
        return ["No", "Tanggal", "Estimasi (Tabung)", "Batas Bawah", "Batas Atas"];
    }

    public function map($row): array {
        $this->rowNumber++;
        return [
            $this->rowNumber,
            \Carbon\Carbon::parse($row->tanggal)->translatedFormat('d F Y'),
            (int)($row->estimasi    ?? 0),
            (int)($row->batas_bawah ?? 0),
            (int)($row->batas_atas  ?? 0),
        ];
    }
}