<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\V1\ApiController;
use App\Http\Controllers\V1\QueryTraits\SwApiQuery;
use App\Http\Controllers\V1\QueryTraits\SwManualApiQuery;
use App\Http\Resources\SwlManualDataResource;
use App\Http\Resources\SwStationResource;
use App\Jobs\SwCsvExportJob;
use App\Jobs\SwManualCsvExportJob;
use App\Models\ClientCollection;
use App\Models\ClientCollectionParameter;
use App\Models\ClientCollectionStation;
use Illuminate\Http\Request;

class SwApiController extends ApiController
{
    use SwApiQuery;
    use SwManualApiQuery;

    public function getSwJson(Request $request)
    {
        $ClientCollection = ClientCollection::whereHas('ClientToken', function ($query) {
            return $query->where('api_client_tokens.token', request()->bearerToken());
        })->active()->where('collection_id', 3)->first();

        if (!$ClientCollection) {
            return response()->json([
                'message' => 'You are not authorized to access this API',
                'status' => 'Unauthorized',
            ], 403);
        }

        $this->apiLogRequest($ClientCollection);

        $RequestData = [];

        if ($request->has('from_date')) {
            $RequestData['from_date'] = $request->from_date;
        } else {
            $RequestData['from_date'] = now()->format('Y-m-d');
        }

        if ($request->has('to_date')) {
            $RequestData['to_date'] = $request->to_date;
        } else {
            $RequestData['to_date'] = now()->format('Y-m-d');
        }

        if ($request->has('station_id')) {
            $RequestData['identifier'] = $request->station_id;
        } elseif ($request->has('station')) {
            $RequestData['identifier'] = $request->station;
        } elseif ($request->has('identifier')) {
            $RequestData['identifier'] = $request->identifier;
        }

        if ($request->has('per_page')) {
            $RequestData['per_page'] = $request->per_page;
        }

        $ClientCollectionParameter = ClientCollectionParameter::with('CollectionParameter')->where('collection_id', $ClientCollection->collection_id)->where('client_id', $ClientCollection->client_id)->get();

        foreach ($ClientCollectionParameter as $key => $value) {
            if (isset($RequestData['per_page']) && $value->CollectionParameter->name == 'max_per_page') {
                if ($RequestData['per_page'] > $value->value) {
                    return response()->json([
                        'message' => 'Your per page limit is ' . $value->value,
                        'status' => 'errors',
                    ], 422);
                }

            }

            if (isset($RequestData['from_date']) && $value->CollectionParameter->name == 'start_date' && $value->value) {
                if (now()->parse($RequestData['from_date'])->format('Y-m-d') <= now()->parse($value->value)->format('Y-m-d')) {
                    return response()->json([
                        'message' => 'Your from date limit is ' . $value->value,
                        'status' => 'errors',
                    ], 422);
                }
            }

            if (isset($RequestData['to_date']) && $value->CollectionParameter->name == 'end_date' && $value->value) {
                if (now()->parse($RequestData['to_date'])->format('Y-m-d') >= now()->parse($value->value)->format('Y-m-d')) {
                    return response()->json([
                        'message' => 'Your to date limit is ' . $value->value,
                        'status' => 'errors',
                    ], 422);
                }
            }

        }

        return SwStationResource::collection($this->getSwAllDataQuery($RequestData, $ClientCollection->id, true))->additional($this->commonJsonReturn('You surface water data json format is successfully'));
    }

    public function getSwCsv(Request $request)
    {

        $ClientCollection = ClientCollection::whereHas('ClientToken', function ($query) {
            return $query->where('api_client_tokens.token', request()->bearerToken());
        })->active()->where('collection_id', 4)->first();

        if (!$ClientCollection) {
            return response()->json([
                'message' => 'You are not authorized to access this API',
                'status' => 'Unauthorized',
            ], 403);
        }

        $RequestData = [];

        if ($request->has('date')) {
            $RequestData['date'] = $request->date;
        } else {
            if ($request->has('from_date')) {
                $RequestData['from_date'] = $request->from_date;
            } else {
                $RequestData['from_date'] = now()->format('Y-m-d');
            }

            if ($request->has('to_date')) {
                $RequestData['to_date'] = $request->to_date;
            } else {
                $RequestData['to_date'] = now()->format('Y-m-d');
            }
        }

        if ($request->has('station_id')) {
            $RequestData['identifier'] = $request->station_id;
        } elseif ($request->has('station')) {
            $RequestData['identifier'] = $request->station;
        } elseif ($request->has('identifier')) {
            $RequestData['identifier'] = $request->identifier;
        }

        if ($request->has('per_page')) {
            $RequestData['per_page'] = $request->per_page;
        }

        $ClientCollectionParameter = ClientCollectionParameter::with('CollectionParameter')->where('collection_id', $ClientCollection->collection_id)->where('client_id', $ClientCollection->client_id)->get();

        foreach ($ClientCollectionParameter as $key => $value) {
            if (isset($RequestData['per_page']) && $value->CollectionParameter->name == 'max_per_page' && $value->value) {
                if ($RequestData['per_page'] > $value->value) {
                    return response()->json([
                        'message' => 'Your per page limit is ' . $value->value,
                        'status' => 'errors',
                    ], 422);
                }

            }

            if (isset($RequestData['from_date']) && $value->CollectionParameter->name == 'start_date' && $value->value) {
                if (now()->parse($RequestData['from_date'])->format('Y-m-d') < now()->parse($value->value)->format('Y-m-d')) {
                    return response()->json([
                        'message' => 'Your from date limit is ' . $value->value,
                        'status' => 'errors',
                    ], 422);
                }
            }

            if (isset($RequestData['to_date']) && $value->CollectionParameter->name == 'end_date' && $value->value) {
                if (now()->parse($RequestData['to_date'])->format('Y-m-d') >= now()->parse($value->value)->format('Y-m-d')) {
                    return response()->json([
                        'message' => 'Your to date limit is ' . $value->value,
                        'status' => 'errors',
                    ], 422);
                }
            }
        }

        SwCsvExportJob::dispatch($RequestData, $ClientCollection->id);
        return response()->json($this->commonJsonReturn('Your surface water data is successfully export to ftp'), 200);

    }

    public function getSwManualJson(Request $request)
    {
        $ClientCollection = ClientCollection::whereHas('ClientToken', function ($query) {
            return $query->where('api_client_tokens.token', request()->bearerToken());
        })->active()->where('collection_id', 14)->first();

        if (!$ClientCollection) {
            return response()->json([
                'message' => 'You are not authorized to access this API',
                'status' => 'Unauthorized',
            ], 403);
        }

        $this->apiLogRequest($ClientCollection);

        $RequestData = [];

        if ($request->has('from_date')) {
            $RequestData['from_date'] = $request->from_date;
        } else {
            $RequestData['from_date'] = now()->format('Y-m-d');
        }

        if ($request->has('to_date')) {
            $RequestData['to_date'] = $request->to_date;
        } else {
            $RequestData['to_date'] = now()->format('Y-m-d');
        }

        if ($request->has('station_id')) {
            $RequestData['identifier'] = $request->station_id;
        } elseif ($request->has('station')) {
            $RequestData['identifier'] = $request->station;
        } elseif ($request->has('identifier')) {
            $RequestData['identifier'] = $request->identifier;
        } else {
            return response()->json([
                'message' => 'Please provide station id',
                'status' => 'errors',
            ], 422);
        }

        if ($request->has('per_page')) {
            $RequestData['per_page'] = $request->per_page;
        }

        $ClientCollectionParameter = ClientCollectionParameter::with('CollectionParameter')->where('collection_id', $ClientCollection->collection_id)->where('client_id', $ClientCollection->client_id)->get();

        if (!$ClientCollection->is_all_station && isset($RequestData['identifier'])) {
            if (!ClientCollectionStation::where('client_collection_id', $ClientCollection->id)->where('station_id', $RequestData['identifier'])->exists()) {
                return response()->json([
                    'message' => 'Your station is not authorized to access this API',
                    'status' => 'errors',
                ], 422);
            }
        }

        foreach ($ClientCollectionParameter as $key => $value) {
            if (isset($RequestData['per_page']) && $value->CollectionParameter->name == 'max_per_page') {
                if ($RequestData['per_page'] > $value->value) {
                    return response()->json([
                        'message' => 'Your per page limit is ' . $value->value,
                        'status' => 'errors',
                    ], 422);
                }

            }

            if (isset($RequestData['from_date']) && $value->CollectionParameter->name == 'start_date' && $value->value) {
                if (now()->parse($RequestData['from_date'])->format('Y-m-d') < now()->parse($value->value)->format('Y-m-d')) {
                    return response()->json([
                        'message' => 'Your from date limit is ' . $value->value,
                        'status' => 'errors',
                    ], 422);
                }
            }

            if (isset($RequestData['to_date']) && $value->CollectionParameter->name == 'end_date' && $value->value) {
                if (now()->parse($RequestData['to_date'])->format('Y-m-d') > now()->parse($value->value)->format('Y-m-d')) {
                    return response()->json([
                        'message' => 'Your to date limit is ' . $value->value,
                        'status' => 'errors',
                    ], 422);
                }
            }

        }

        return SwlManualDataResource::collection($this->getSwManualDataQuery($RequestData, $ClientCollection->id, true))->additional($this->commonJsonReturn('You surface water level manual data json format is successfully'));
    }

    public function getSwManualCsv(Request $request)
    {

        $ClientCollection = ClientCollection::whereHas('ClientToken', function ($query) {
            return $query->where('api_client_tokens.token', request()->bearerToken());
        })->active()->where('collection_id', 12)->first();

        if (!$ClientCollection) {
            return response()->json([
                'message' => 'You are not authorized to access this API',
                'status' => 'Unauthorized',
            ], 403);
        }

        $RequestData = [];

        if ($request->has('date')) {
            $RequestData['date'] = $request->date;
        } else {
            if ($request->has('from_date')) {
                $RequestData['from_date'] = $request->from_date;
            } else {
                $RequestData['from_date'] = now()->format('Y-m-d');
            }

            if ($request->has('to_date')) {
                $RequestData['to_date'] = $request->to_date;
            } else {
                $RequestData['to_date'] = now()->format('Y-m-d');
            }
        }

        if ($request->has('station_id')) {
            $RequestData['identifier'] = $request->station_id;
        } elseif ($request->has('station')) {
            $RequestData['identifier'] = $request->station;
        } elseif ($request->has('identifier')) {
            $RequestData['identifier'] = $request->identifier;
        }

        if ($request->has('per_page')) {
            $RequestData['per_page'] = $request->per_page;
        }

        $ClientCollectionParameter = ClientCollectionParameter::with('CollectionParameter')->where('collection_id', $ClientCollection->collection_id)->where('client_id', $ClientCollection->client_id)->get();

        foreach ($ClientCollectionParameter as $key => $value) {
            if (isset($RequestData['per_page']) && $value->CollectionParameter->name == 'max_per_page' && $value->value) {
                if ($RequestData['per_page'] > $value->value) {
                    return response()->json([
                        'message' => 'Your per page limit is ' . $value->value,
                        'status' => 'errors',
                    ], 422);
                }

            }

            if (isset($RequestData['from_date']) && $value->CollectionParameter->name == 'start_date' && $value->value) {
                if (now()->parse($RequestData['from_date'])->format('Y-m-d') < now()->parse($value->value)->format('Y-m-d')) {
                    return response()->json([
                        'message' => 'Your from date limit is ' . $value->value,
                        'status' => 'errors',
                    ], 422);
                }
            }

            if (isset($RequestData['to_date']) && $value->CollectionParameter->name == 'end_date' && $value->value) {
                if (now()->parse($RequestData['to_date'])->format('Y-m-d') >= now()->parse($value->value)->format('Y-m-d')) {
                    return response()->json([
                        'message' => 'Your to date limit is ' . $value->value,
                        'status' => 'errors',
                    ], 422);
                }
            }
        }

        SwManualCsvExportJob::dispatch($RequestData, $ClientCollection->id);

        return response()->json($this->commonJsonReturn('Your surface water manual data is successfully export to ftp'), 200);

    }
}
