<?php

namespace App\Exports\V3\RoadItemsProjects;

use App\Models\Province;
use App\Models\RoadItemsProject;
use Carbon\Carbon;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\DB;
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\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

class CountryActivityPerItemReport implements FromView, WithEvents, ShouldAutoSize, WithDrawings
{
    public function __construct(private ?string $fromDate, private ?string $toDate){}

    public function view(): View
    {
        $fromDate = $this->fromDate ?? '2021-03-21 00:00:00';
        $toDate = $this->toDate ?? \Carbon\Carbon::now();

        $query = "SELECT 
                COUNT(CASE WHEN item = 1 THEN 1 END) AS MarematRooyeCount,
                COUNT(CASE WHEN item = 2 THEN 1 END) AS PaksaziCount,
                COUNT(CASE WHEN item = 3 THEN 1 END) AS AlaemCount,
                COUNT(CASE WHEN item = 4 THEN 1 END) AS HefazCount,
                COUNT(CASE WHEN item = 5 THEN 1 END) AS RoshanayiCount,
                COUNT(CASE WHEN item = 6 THEN 1 END) AS KhatkeshiCount,
                COUNT(CASE WHEN item = 7 THEN 1 END) AS RangamiziCount,
                COUNT(CASE WHEN item = 8 THEN 1 END) AS WashingCount,
                COUNT(CASE WHEN item = 9 THEN 1 END) AS ImenSaziCount,
                COUNT(CASE WHEN item = 10 THEN 1 END) AS HarimCount,
                COUNT(CASE WHEN item = 11 THEN 1 END) AS PolCount,
                COUNT(CASE WHEN item = 12 THEN 1 END) AS ToonelCount,
                COUNT(CASE WHEN item = 13 THEN 1 END) AS AmaliatZemestaniCount,
                COUNT(CASE WHEN item = 14 THEN 1 END) AS MashinAlatCount,
                COUNT(CASE WHEN item = 15 THEN 1 END) AS TollhouseCount,
                COUNT(CASE WHEN item = 16 THEN 1 END) AS EmergencyCount,
                province_fa,
                province_id,
                COUNT(CASE WHEN item != 20 THEN 1 END) AS TotalProvinceCount
            FROM (
                SELECT 
                    province_fa,
                    province_id,
                    item,
                    status
                FROM road_items_projects
                WHERE activity_date_time BETWEEN '{$fromDate}' and '{$toDate}'
            ) AS merged_table
            WHERE status = 1
            GROUP BY province_id, province_fa;";

        $provinceActivities = collect(DB::select($query));

        $totalItemCount = [
            'totalMarematRooyeCount' => $provinceActivities->sum('MarematRooyeCount'),
            'totalPaksaziCount' => $provinceActivities->sum('PaksaziCount'),
            'totalAlaemCount' => $provinceActivities->sum('AlaemCount'),
            'totalHefazCount' => $provinceActivities->sum('HefazCount'),
            'totalRoshanayiCount' => $provinceActivities->sum('RoshanayiCount'),
            'totalKhatkeshiCount' => $provinceActivities->sum('KhatkeshiCount'),
            'totalRangamiziCount' => $provinceActivities->sum('RangamiziCount'),
            'totalWashingCount' => $provinceActivities->sum('WashingCount'),
            'totalImenSaziCount' => $provinceActivities->sum('ImenSaziCount'),
            'totalHarimCount' => $provinceActivities->sum('HarimCount'),
            'totalPolCount' => $provinceActivities->sum('PolCount'),
            'totalToonelCount' => $provinceActivities->sum('ToonelCount'),
            'totalAmaliatZemestaniCount' => $provinceActivities->sum('AmaliatZemestaniCount'),
            'totalMashinAlatCount' => $provinceActivities->sum('MashinAlatCount'),
            'totalTollhouseCount' => $provinceActivities->sum('TollhouseCount'),
            'totalEmergencyCount' => $provinceActivities->sum('EmergencyCount'),
            'totalCountryCount' => $provinceActivities->sum('TotalProvinceCount'),
        ];

        return view('v3.Reports.RoadItems.CountryActivityPerItem', [
            'provinceCount' => $provinceActivities,
            'countryCount' => $totalItemCount,
            'fromDate' => $fromDate,
            'toDate' => $toDate
        ]);
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function (AfterSheet $event) {
                $event->sheet->getDelegate()->setRightToLeft(true);
                $event->sheet->getDelegate()->getStyle('A:U')->getFont()->setName('Arial');
                $event->sheet->getDelegate()->getStyle('A:U')
                    ->getAlignment()
                    ->setHorizontal(Alignment::HORIZONTAL_CENTER);

                $event->sheet->getDelegate()->getStyle('A:U')
                    ->getAlignment()
                    ->setVertical(Alignment::VERTICAL_CENTER);
            }
        ];
    }

    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];
    }
}