<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Contracts;
use App\Models\RoadMaintenanceProject;
use App\Models\RoadUpgradeSafety;
use App\Models\RoadDangerPrevention;
use App\Models\RoadTechnicalBuilding;
use App\Models\RoadTollhouseManagement;

class FinanceProjectController extends Controller
{
    public function getMaintenanceProjectList(Request $request)
    {
        if ($request->has('project_id')) {
            $getProvinceID = Contracts::where('id', $request->project_id)->first();
            $roadProjects = RoadMaintenanceProject::with('latestHistory')
                                                    ->where('province_id', $getProvinceID->province_id)
                                                    ->get();
        } else {
            $roadProjects = RoadMaintenanceProject::with('latestHistory')->get();
        }
        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $roadProjects,
        ], 200);
    }

    public function showMaintenanceProjectData(Request $request)
    {
        $roadProject = RoadMaintenanceProject::find($request->id);
        $roadProjectHistories = $roadProject->projectHistories;

        $roadProjectHistoriesFiles = [];
        $imagesIndex = 0;

        foreach ($roadProjectHistories as $key => $roadProjectHistory) {
            foreach ($roadProjectHistory->files as $keyPath => $file) {
                $roadProjectHistoriesFiles[$imagesIndex] = asset('storage' . substr($file->path, 6));
                $imagesIndex++;
            }
        }

        $cities = [];
        $citiesIndex = 0;

        foreach ($roadProject->cities_id as $cityKey => $value) {
            $getCity = \App\Models\City::find($cityKey);

            $cities[$citiesIndex] = [
                'id' => $getCity->id,
                'name' => $getCity->name_fa,
                'value' =>  $value
            ];

            $citiesIndex++;
        }

        $roadProjectArray = [
            'id' => $roadProject->id,
            'province' => [
                'id' => ($roadProject->province) ? $roadProject->province->id : null,
                'name' => ($roadProject->province) ? $roadProject->province->name_fa : '-',
                'lat_lng' => [
                    $roadProject->province->center_lat,
                    $roadProject->province->center_long
                ]
            ],
            'cities' => $cities,
            'axis_name' => ($roadProject->axis_name) ? $roadProject->axis_name : '-',
            'axis_type' => ($roadProject->axis_type) ? : 0,
            'project_name' => ($roadProject->project_name) ? $roadProject->project_name : '-',
            'contractor_name' => ($roadProject->contractor_name) ? $roadProject->contractor_name : '-',
            'consultant_name' => ($roadProject->consultant_name) ? $roadProject->consultant_name : '-',
            'contract_date' => ($roadProject->contract_date) ? $roadProject->contract_date : '-',
            'contract_credit' => ($roadProject->contract_credit) ? $roadProject->contract_credit : '-',
            'submited_at' => ($roadProject->submited_at) ?
                verta($roadProject->submited_at)->format('Y/n/j - H:i') : '-',
            'last_updated_at' => ($roadProject->last_updated_at) ?
                verta($roadProject->last_updated_at)->format('Y/n/j - H:i') : '-',
            'start_latlng' => $roadProject->start_latlng,
            'end_latlng' => $roadProject->end_latlng,
            'project_distance' => $roadProject->project_distance,
            'project_distance_update' => $roadProject->project_distance_update,
            'physical_progress' => $roadProject->physical_progress,
            'images' => $roadProjectHistoriesFiles,
            'project_status' => $roadProject->project_status,
            'rate' => ($roadProject->rate)? $roadProject->rate:null,
            'rate_desc' => ($roadProject->rate_desc)? $roadProject->rate_desc:null
        ];

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


    public function getUpgradeSafetyList(Request $request)
    {
        if ($request->has('project_id')) {
            $getProvinceID = Contracts::where('id', $request->project_id)->first();
            $roadProjects = RoadUpgradeSafety::with('latestHistory')
                                                    ->where('province_id', $getProvinceID->province_id)
                                                    ->get();
        } else {
            $roadProjects = RoadUpgradeSafety::with('latestHistory')->get();
        }
        


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

    public function showUpgradeSafetyData(Request $request)
    {
        $roadProject = RoadUpgradeSafety::find($request->id);
        $roadProjectHistories = $roadProject->projectHistories;

        $roadProjectHistoriesFiles = [];
        $imagesIndex = 0;

        foreach ($roadProjectHistories as $key => $roadProjectHistory) {
            foreach ($roadProjectHistory->files as $keyPath => $file) {
                $roadProjectHistoriesFiles[$imagesIndex] = asset('storage' . substr($file->path, 6));
                $imagesIndex++;
            }
        }

        $cities = [];
        $citiesIndex = 0;

        foreach ($roadProject->cities_id as $cityKey => $value) {
            $getCity = \App\Models\City::find($cityKey);

            $cities[$citiesIndex] = [
                'id' => $getCity->id,
                'name' => $getCity->name_fa,
                'value' =>  $value
            ];

            $citiesIndex++;
        }

        $roadProjectArray = [
            'id' => $roadProject->id,
            'province' => [
                'id' => ($roadProject->province) ? $roadProject->province->id : null,
                'name' => ($roadProject->province) ? $roadProject->province->name_fa : '-',
                'lat_lng' => [
                    $roadProject->province->center_lat,
                    $roadProject->province->center_long
                ]
            ],
            'cities' => $cities,
            'axis_name' => ($roadProject->axis_name) ? $roadProject->axis_name : '-',
            'axis_type' => ($roadProject->axis_type) ? : 0,
            'project_name' => ($roadProject->project_name) ? $roadProject->project_name : '-',
            'contractor_name' => ($roadProject->contractor_name) ? $roadProject->contractor_name : '-',
            'consultant_name' => ($roadProject->consultant_name) ? $roadProject->consultant_name : '-',
            'contract_date' => ($roadProject->contract_date) ? $roadProject->contract_date : '-',
            'contract_credit' => ($roadProject->contract_credit) ? $roadProject->contract_credit : '-',
            'submited_at' => ($roadProject->submited_at) ?
                verta($roadProject->submited_at)->format('Y/n/j - H:i') : '-',
            'last_updated_at' => ($roadProject->last_updated_at) ?
                verta($roadProject->last_updated_at)->format('Y/n/j - H:i') : '-',
            'start_latlng' => $roadProject->start_latlng,
            'end_latlng' => $roadProject->end_latlng,
            'project_distance' => $roadProject->project_distance,
            'project_distance_update' => $roadProject->project_distance_update,
            'physical_progress' => $roadProject->physical_progress,
            // 'credit_progress' => ((($roadProject->contract_credit - $roadProject->contractor_credit)
                // / $roadProject->contract_credit) * 100),
            'images' => $roadProjectHistoriesFiles,
            'project_status' => $roadProject->project_status,
            'project_description' => $roadProject->project_description,
            'rate' => ($roadProject->rate)? $roadProject->rate:null,
            'rate_desc' => ($roadProject->rate_desc)? $roadProject->rate_desc:null
        ];

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


    public function getDangerPreventionList(Request $request)
    {
        if ($request->has('project_id')) {
            $getProvinceID = Contracts::where('id', $request->project_id)->first();
            $roadProjects = RoadDangerPrevention::with('latestHistory')
                                                    ->where('province_id', $getProvinceID->province_id)
                                                    ->get();
        } else {
            $roadProjects = RoadDangerPrevention::with('latestHistory')->get();
        }
        
        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $roadProjects,
        ], 200);
    }

    public function showDangerPreventionData(Request $request)
    {
        $roadProject = RoadDangerPrevention::find($request->id);
        $roadProjectHistories = $roadProject->projectHistories;

        $roadProjectHistoriesFiles = [];
        $imagesIndex = 0;

        foreach ($roadProjectHistories as $key => $roadProjectHistory) {
            foreach ($roadProjectHistory->files as $keyPath => $file) {
                $roadProjectHistoriesFiles[$imagesIndex] = asset('storage' . substr($file->path, 6));
                $imagesIndex++;
            }
        }

        $cities = [];
        $citiesIndex = 0;

        foreach ($roadProject->cities_id as $cityKey => $value) {
            $getCity = \App\Models\City::find($cityKey);

            $cities[$citiesIndex] = [
                'id' => $getCity->id,
                'name' => $getCity->name_fa,
                'value' =>  $value
            ];

            $citiesIndex++;
        }

        $roadProjectArray = [
            'id' => $roadProject->id,
            'province' => [
                'id' => ($roadProject->province) ? $roadProject->province->id : null,
                'name' => ($roadProject->province) ? $roadProject->province->name_fa : '-',
                'lat_lng' => [
                    $roadProject->province->center_lat,
                    $roadProject->province->center_long
                ]
            ],
            'cities' => $cities,
            'axis_name' => ($roadProject->axis_name) ? $roadProject->axis_name : '-',
            'axis_type' => ($roadProject->axis_type) ? : 0,
            'project_name' => ($roadProject->project_name) ? $roadProject->project_name : '-',
            'contractor_name' => ($roadProject->contractor_name) ? $roadProject->contractor_name : '-',
            'consultant_name' => ($roadProject->consultant_name) ? $roadProject->consultant_name : '-',
            'contract_date' => ($roadProject->contract_date) ? $roadProject->contract_date : '-',
            'contract_credit' => ($roadProject->contract_credit) ? $roadProject->contract_credit : '-',
            'submited_at' => ($roadProject->submited_at) ?
                verta($roadProject->submited_at)->format('Y/n/j - H:i') : '-',
            'last_updated_at' => ($roadProject->last_updated_at) ?
                verta($roadProject->last_updated_at)->format('Y/n/j - H:i') : '-',
            'start_latlng' => $roadProject->start_latlng,
            'end_latlng' => $roadProject->end_latlng,
            'project_distance' => $roadProject->project_distance,
            'project_distance_update' => $roadProject->project_distance_update,
            'physical_progress' => $roadProject->physical_progress,
            // 'credit_progress' => ((($roadProject->contract_credit - $roadProject->contractor_credit)
                // / $roadProject->contract_credit) * 100),
            'images' => $roadProjectHistoriesFiles,
            'project_status' => $roadProject->project_status,
            'project_description' => $roadProject->project_description,
            'rate' => ($roadProject->rate)? $roadProject->rate:null,
            'rate_desc' => ($roadProject->rate_desc)? $roadProject->rate_desc:null
        ];

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

    public function getTechnicalBuildingList(Request $request)
    {
        if ($request->has('project_id')) {
            $getProvinceID = Contracts::where('id', $request->project_id)->first();
            $roadProjects = RoadTechnicalBuilding::with('latestHistory')
                                                    ->where('province_id', $getProvinceID->province_id)
                                                    ->get();
        } else {
            $roadProjects = RoadTechnicalBuilding::with('latestHistory')->get();
        }

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

    public function showTechnicalBuildingData(Request $request)
    {
        $roadProject = RoadTechnicalBuilding::find($request->id);
        $roadProjectHistories = $roadProject->projectHistories;

        $roadProjectHistoriesFiles = [];
        $imagesIndex = 0;

        foreach ($roadProjectHistories as $key => $roadProjectHistory) {
            foreach ($roadProjectHistory->files as $keyPath => $file) {
                $roadProjectHistoriesFiles[$imagesIndex] = asset('storage' . substr($file->path, 6));
                $imagesIndex++;
            }
        }

        $cities = [];
        $citiesIndex = 0;

        foreach ($roadProject->cities_id as $cityKey => $value) {
            $getCity = \App\Models\City::find($cityKey);

            $cities[$citiesIndex] = [
                'id' => $getCity->id,
                'name' => $getCity->name_fa,
                'value' =>  $value
            ];

            $citiesIndex++;
        }

        $roadProjectArray = [
            'id' => $roadProject->id,
            'province' => [
                'id' => ($roadProject->province) ? $roadProject->province->id : null,
                'name' => ($roadProject->province) ? $roadProject->province->name_fa : '-',
                'lat_lng' => [
                    $roadProject->province->center_lat,
                    $roadProject->province->center_long
                ]
            ],
            'cities' => $cities,
            'axis_name' => ($roadProject->axis_name) ? $roadProject->axis_name : '-',
            'axis_type' => ($roadProject->axis_type) ? : 0,
            'project_name' => ($roadProject->project_name) ? $roadProject->project_name : '-',
            'contractor_name' => ($roadProject->contractor_name) ? $roadProject->contractor_name : '-',
            'consultant_name' => ($roadProject->consultant_name) ? $roadProject->consultant_name : '-',
            'contract_date' => ($roadProject->contract_date) ? $roadProject->contract_date : '-',
            'contract_credit' => ($roadProject->contract_credit) ? $roadProject->contract_credit : '-',
            'submited_at' => ($roadProject->submited_at) ?
                verta($roadProject->submited_at)->format('Y/n/j - H:i') : '-',
            'last_updated_at' => ($roadProject->last_updated_at) ?
                verta($roadProject->last_updated_at)->format('Y/n/j - H:i') : '-',
            'start_latlng' => $roadProject->start_latlng,
            'end_latlng' => $roadProject->end_latlng,
            'project_distance' => $roadProject->project_distance,
            'project_distance_update' => $roadProject->project_distance_update,
            'physical_progress' => $roadProject->physical_progress,
            // 'credit_progress' => ((($roadProject->contract_credit - $roadProject->contractor_credit)
                // / $roadProject->contract_credit) * 100),
            'images' => $roadProjectHistoriesFiles,
            'project_status' => $roadProject->project_status,
            'project_description' => $roadProject->project_description,
            'rate' => ($roadProject->rate)? $roadProject->rate:null,
            'rate_desc' => ($roadProject->rate_desc)? $roadProject->rate_desc:null
        ];

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


    public function getTollhouseManagementListPublic(Request $request)
    {
        if ($request->has('project_id')) {
            $getProvinceID = Contracts::where('id', $request->project_id)->first();
            $roadProjects = RoadTollhouseManagement::with('latestHistory')
                                                    ->where('province_id', $getProvinceID->province_id)
                                                    ->get();
        } else {
            $roadProjects = RoadTollhouseManagement::with('latestHistory')->get();
        }
        
        
        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $roadProjects,
            'can_submit' => 1
        ], 200);
    }

    public function showTollhouseManagementData(Request $request)
    {
        $roadProject = RoadTollhouseManagement::find($request->id);
        $roadProjectHistories = $roadProject->projectHistories;

        $roadProjectHistoriesFiles = [];
        $imagesIndex = 0;

        foreach ($roadProjectHistories as $key => $roadProjectHistory) {
            foreach ($roadProjectHistory->files as $keyPath => $file) {
                $roadProjectHistoriesFiles[$imagesIndex] = asset('storage' . substr($file->path, 6));
                $imagesIndex++;
            }
        }

        $cities = [];
        $citiesIndex = 0;

        foreach ($roadProject->cities_id as $cityKey => $value) {
            $getCity = \App\Models\City::find($cityKey);

            $cities[$citiesIndex] = [
                'id' => $getCity->id,
                'name' => $getCity->name_fa,
                'value' =>  $value
            ];

            $citiesIndex++;
        }

        $roadProjectArray = [
            'id' => $roadProject->id,
            'province' => [
                'id' => ($roadProject->province) ? $roadProject->province->id : null,
                'name' => ($roadProject->province) ? $roadProject->province->name_fa : '-',
                'lat_lng' => [
                    $roadProject->province->center_lat,
                    $roadProject->province->center_long
                ]
            ],
            'cities' => $cities,
            'axis_name' => ($roadProject->axis_name) ? $roadProject->axis_name : '-',
            'axis_type' => ($roadProject->axis_type) ? : 0,
            'project_name' => ($roadProject->project_name) ? $roadProject->project_name : '-',
            'contractor_name' => ($roadProject->contractor_name) ? $roadProject->contractor_name : '-',
            'consultant_name' => ($roadProject->consultant_name) ? $roadProject->consultant_name : '-',
            'contract_date' => ($roadProject->contract_date) ? $roadProject->contract_date : '-',
            'contract_credit' => ($roadProject->contract_credit) ? $roadProject->contract_credit : '-',
            'submited_at' => ($roadProject->submited_at) ?
                verta($roadProject->submited_at)->format('Y/n/j - H:i') : '-',
            'last_updated_at' => ($roadProject->last_updated_at) ?
                verta($roadProject->last_updated_at)->format('Y/n/j - H:i') : '-',
            'start_latlng' => $roadProject->start_latlng,
            'end_latlng' => $roadProject->end_latlng,
            'project_distance' => $roadProject->project_distance,
            'project_distance_update' => $roadProject->project_distance_update,
            'physical_progress' => $roadProject->physical_progress,
            // 'credit_progress' => ((($roadProject->contract_credit - $roadProject->contractor_credit)
                // / $roadProject->contract_credit) * 100),
            'images' => $roadProjectHistoriesFiles,
            'project_status' => $roadProject->project_status,
            'project_description' => $roadProject->project_description,
            'rate' => ($roadProject->rate)? $roadProject->rate:null,
            'rate_desc' => ($roadProject->rate_desc)? $roadProject->rate_desc:null
        ];

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