<?php

namespace App\Services\Reports;

use App\Models\InfoItem;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

class RoadItemReportService
{
    public function countryActivityPerSubItem(int $item, string $fromDate, string $toDate): array
    {
        $subItems = InfoItem::query()->where('item', $item)
            ->orderBy('sub_item')
            ->get(['sub_item', 'sub_item_str', 'sub_item_unit']);

        $query = DB::table('road_items_projects')
            ->selectRaw('province_id, province_fa');

        foreach ($subItems as $subItem) {
            $query->selectRaw("
                COUNT(CASE WHEN sub_item = {$subItem->sub_item} THEN 1 END) as Count_{$subItem->sub_item},
                SUM(CASE WHEN sub_item = {$subItem->sub_item} THEN sub_item_data END) as Sum_{$subItem->sub_item}
            ");
        }

        $query->where('item', '=', $item)
            ->where('status', '=', 1)
            ->whereBetween('activity_date_time', [$fromDate, $toDate])
            ->groupBy('province_id', 'province_fa')
            ->orderBy('province_id');

        $activities = $query->get();

        $totalItemCount = [
            'province_fa' => 'کشوری'
        ];

        foreach ($subItems as $subItem) {
            $totalItemCount["Count_{$subItem->sub_item}"] = $activities->sum("Count_{$subItem->sub_item}");
            $totalItemCount["Sum_{$subItem->sub_item}"] = $activities->sum("Sum_{$subItem->sub_item}");
        }

        return [
            'data' => $activities->push((object)$totalItemCount),
            'fromDate' => $fromDate,
            'toDate' => $toDate,
            'details' => $subItems->keyBy('sub_item'),
        ];
    }

    public function provinceActivityPerSubItem(int $item, string $fromDate, string $toDate, int $provinceId): Collection
    {
        $subItems = InfoItem::query()->where('item', $item)
            ->orderBy('sub_item')
            ->get(['sub_item', 'sub_item_str', 'sub_item_unit']);

        $query = DB::table('road_items_projects')
            ->selectRaw('province_id, province_fa, user_name as city_fa, city_id');

        foreach ($subItems as $subItem) {
            $query->selectRaw("
                COUNT(CASE WHEN sub_item = {$subItem->sub_item} THEN 1 END) as Count_{$subItem->sub_item},
                SUM(CASE WHEN sub_item = {$subItem->sub_item} THEN sub_item_data END) as Sum_{$subItem->sub_item}
            ");
        }

        $query->where('item', '=', $item)
            ->where('status', '=', 1)
            ->where('province_id', '=', $provinceId)
            ->whereBetween('activity_date_time', [$fromDate, $toDate])
            ->groupBy('city_id')
            ->orderBy('province_id');

        $activities = $query->get();

        $totalItemCount = [
            'city_fa' => 'استانی'
        ];

        foreach ($subItems as $subItem) {
            $totalItemCount["Count_{$subItem->sub_item}"] = $activities->sum("Count_{$subItem->sub_item}");
            $totalItemCount["Sum_{$subItem->sub_item}"] = $activities->sum("Sum_{$subItem->sub_item}");
        }

        return collect([
            'data' => $activities->push((object)$totalItemCount),
            'fromDate' => $fromDate,
            'toDate' => $toDate,
            'details' => $subItems->keyBy('sub_item')
        ]);
    }
}