<?php

namespace App\Exports;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;

class singleExport implements FromView, WithHeadings, WithTitle
{
    private $month = "December";

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


    /**
    * @return \Illuminate\Support\Collection
    */
    public function array(): array
    {
        return [$this->sampleData['months'][$this->month]];
    }

    public function headings(): array
    {
        return ['min', 'max'];
    }

    public function title(): string
    {
        return 'Month ' . $this->month;
    }


    public function view(): View
    {
        $data = $this->fakeData();
        return view('report.export', compact('data'));
    }

    public function fakeData(){
        $data = [
            'summary' => [],
            'daily' => []
        ];

        $total_min = 0;
        $total_max = 0;
        $total_avg = 0;

        for($i = 1; $i <= 31; $i++){

            $min_temp = rand(1,35);
            $max_temp = rand(1,35);
            $avg_temp = ($min_temp + $max_temp) / 2;

            $total_min += $min_temp;
            $total_max += $max_temp;
            $total_avg += $total_avg;

            array_push($data['daily'], ['min_temp' => $min_temp, 'max_temp' => $max_temp, 'avg_temp' => $avg_temp]);
        }

        $data['summary'] = [
            'avg_min' => $total_min / 31,
            'avg_max' => $total_max / 31,
            'avg_avg' => $total_avg / 31
        ];

        return $data;

    }
}

