<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\RoadObserved;
use Illuminate\Support\Facades\DB;

class HomeProjectController extends Controller
{
    public function getRoadItemsProjectPoints(Request  $request)
    {
        $request->validate([
            'start-date' => 'date_format:Y-m-d',
            'end-date' => 'date_format:Y-m-d'
        ]);

        if ($request->input('start-date') && $request->input('end-date')) {
            $startDate = date($request->input('start-date') . ' 00:00:00');
            $endtDate = date($request->input('end-date') . ' 23:59:59');
        } else {
            $startDate = date('Y-m-d 00:00:00');
            $endtDate = date('Y-m-d H:i:s');
        }

        $projects = \App\Models\RoadItemsProject::where(
            'item',
            ($request->input('item') && $request->input('item') <= 16 && $request->input('item') >= 1) ? $request->input('item') : 1
        )
            ->whereBetween('created_at', [$startDate, $endtDate])
            ->with('files')
            ->get();

        $projectsArray = [];
        $imageFileTypes = ['png', 'gif', 'peg', 'jpg'];

        foreach ($projects as $key => $project) {
            $files = [];
            $fileKey = 0;

            foreach ($project->files as $file) {
                if (in_array(substr($file->path, -3), $imageFileTypes)) {
                    if (substr($file->path, 0, 6) == 'public') {
                        $files[$fileKey] = asset('storage' . substr($file->path, 6));
                        $fileKey++;
                    } else {
                        $files[$fileKey] = asset('storage/' . $file->path);
                        $fileKey++;
                    }
                }
            }

            $projectsArray[$key] = [
                'id' => $project->id,
                'start_lat' => $project->start_lat,
                'start_lng' => $project->start_lng,
                'end_lat' => $project->end_lat,
                'end_lng' => $project->end_lng,
                'project_distance' => $project->project_distance,
                'province_name' => ($project->user->province) ? $project->user->province->name_fa : '-',
                'city_name' => ($project->user->city) ? $project->user->city->name_fa : '-',
                'item' => $project->item,
                'sub_items' => $project->sub_items,
                'sub_items_data' => $project->sub_items_data,
                'created_at' => verta($project->created_at)->format('Y/n/j H:i'),
                'updated_at' => verta($project->updated_at)->format('Y/n/j H:i'),
                'files' => $files
            ];
        }

        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $projectsArray
        ], 200);
    }
// sharareh begin
    // public function getFastReactionProjectPoints(Request  $request)
    // {
    //     $province = $request->province_id;
    //     $edare_shahri = $request->edare_shahri_id;
    //     $status = $request->status;
    //     $rms_status = $request->rms_status;

    //     if ($request->date_from && $request->date_to) {
    //         $date_from = $request->date_from.' 00:00:00';
    //         $date_to = $request->date_to.' 23:59:59';
    //     } else {
    //         $date_from = Date('Y-m-d');
    //         $date_to = new \DateTime('tomorrow');
    //         $date_to = $date_to->format('Y-m-d');
    //     }

    //     $projects = RoadObserved::select('id', 'lng', 'lat','status')
    //         ->whereBetween('created_at', [$date_from, $date_to])
    //         ->when($province, function ($query) use ($province) {
    //                 return $query->where('province_id', $province);
    //         })
    //         ->when($rms_status, function ($query) use ($rms_status) {
    //             if ($rms_status == 2) {
    //                 return $query->where('rms_status', '<>',0);
    //             } else {
    //                 return $query->where('rms_status', 0);
    //             }
    //         })
    //         ->when($edare_shahri, function ($query) use ($edare_shahri) {
    //             return $query->where('edarate_shahri_id', $edare_shahri);
    //         })
    //         ->when($status, function ($query) use ($status) {
    //             return $query->where('status', $status);
    //         })
    //         // ->toSql();
    //         ->get();
    //     return response()->json([
    //         'status' => 'succeed',
    //         'message' => '',
    //         'data' => $projects
    //     ], 200);
    // }
// sharareh begin

    public function getFastReactionProjectPointsFor141(Request $request)
    {
        $projects = \App\Models\RoadObserved::whereBetween('rms_status', 0)->get();

        $projectsArray = [];

        foreach ($projects as $key => $problem) {
            $projectsArray[$key] = [
                'id' => $problem->id,
                'uid' => $problem->AutoID,
                'Title' => $problem->Title ?? null,
                'StartTime_DateTime' => verta($problem->StartTime_DateTime)->format('Y/n/j - H:i'),
                'EndTime_DateTime' => verta($problem->EndTime_DateTime)->format('Y/n/j - H:i'),
                'ProvinceName' => $problem->ProvinceName ?? null,
                'TownName' => $problem->TownName ?? null,
                'lng'=> $problem->lng ?? null,
                'lat'=> $problem->lat ?? null,
                'EventTypeTitle' => $problem->EventTypeTitle ?? null,
                'FeatureTypeTitle' => $problem->FeatureTypeTitle ?? null,
                'Description' => $problem->Description ?? '-',
                'MobileForSendEventSms' => $problem->MobileForSendEventSms ?? '-',
                'rms_status' => $problem->rms_status,
                'rms_start_latlng' => $problem->rms_start_latlng ?? null,
                'rms_description' => $problem->rms_description ?? null,
                'rms_last_activity' => ($problem->rms_status > 0) ? verta($problem->updated_at)->format('Y/n/j - H:i') : null,
                'images' => $problem->files
            ];
        }

        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $projectsArray
        ], 200);
    }
}
