<?php

namespace App\Exports\V3\SafetyAndPrivacy\Province;

use App\Models\EdarateShahri;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithDrawings;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class UtilityPassingReport implements FromView, WithEvents, ShouldAutoSize, WithDrawings
{
    public function __construct(private array $data){}

    public function view(): View
    {
        $axis = [
            1 => 'آزادراه',
            2 => 'بزرگراه',
            3 => 'اصلی',
            4 => 'فرعی',
            5 => 'روستایی'
        ];
        $grid = [];
        $summary = null; // کل استان

        foreach ($this->data as $r) {
            $eid = $r->edare_id;
            $name = $r->edare_name;
            $axisKey = $axis[$r->axis_type_id] ?? "axis_{$r->axis_type_id}";

            if ($eid === -1) {
                $summary ??= ['edare_name' => $name, 'total' => 0];
                $summary["{$axisKey}_1"] = $r->s1;
                $summary["{$axisKey}_2"] = $r->s2;
                $summary["{$axisKey}_3"] = $r->s3;
                $summary['total']        = $r->total_sum;
                continue;
            }

            if (!isset($grid[$eid])) {
                $grid[$eid] = ['edare_name' => $name, 'total' => 0];
            }
            $grid[$eid]["{$axisKey}_1"] = $r->s1;
            $grid[$eid]["{$axisKey}_2"] = $r->s2;
            $grid[$eid]["{$axisKey}_3"] = $r->s3;
            $grid[$eid]['total']        = $r->total_sum;
        }

        $exportRows = [];
        if ($summary) $exportRows[] = $summary;
        foreach ($grid as $row) $exportRows[] = $row;

        return view('v3.Reports.SafetyAndPrivacy.Province.UtilityPassingActivityReport', [
            'rows' => $exportRows,
            'axis' => $axis,
            'axisOrder' => array_keys($axis),
        ]);
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function (AfterSheet $event) {
                $event->sheet->getDelegate()->setRightToLeft(true);
            },
        ];
    }

    public function drawings(): array
    {
        $drawing = new Drawing();
        $drawing->setName('Logo');
        $drawing->setDescription('This is my logo');
        $drawing->setPath(public_path('/dist/logo.png'));
        $drawing->setWidth(50);
        $drawing->setHeight(50);
        $drawing->setOffsetX(5);
        $drawing->setOffsetY(5);
        $drawing->setCoordinates('A1');

        $drawing2 = new Drawing();
        $drawing2->setName('Logo');
        $drawing2->setDescription('This is my logo');
        $drawing2->setPath(public_path('/dist/141icon.png'));
        $drawing2->setWidth(50);
        $drawing2->setHeight(50);
        $drawing2->setOffsetX(5);
        $drawing2->setOffsetY(5);
        $drawing2->setCoordinates('Q1');
        return [$drawing, $drawing2];
    }
}