<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use App\Models\City;
use App\Models\WeatherData;
use Illuminate\Http\Client\Response as ClientResponse;
use App\Http\Requests\StoreCityRequest;
use App\Http\Requests\UpdateCityRequest;

class DatabaseController extends Controller
{
    
    public static function getNewestWeatherDataByCityID($city_id) {
        return DB::table('cities')
            ->join('weather_data', 'cities.id', '=', 'weather_data.city_id')
            ->select('cities.id', 'cities.location_name', 'cities.location_country', 'weather_data.current_temperature', 'weather_data.weather_icon', 'weather_data.weather_description', 'weather_data.wind_speed', 'weather_data.wind_dir', 'weather_data.localtime_epoch')
            ->where('cities.id', '=', $city_id)
            ->orderBy('localtime_epoch', 'desc')
            ->first();
    }

    public function getNewestWeatherDataForAllCities () {      
        $city_ids = DB::table('cities')->pluck('id');
        
        $responses = collect();             

        foreach ($city_ids as $city_id) {
            $response = $this->getNewestWeatherDataByCityID($city_id);
            $responses->push($response);       
        }

        return $responses;
    }

    public static function store(ClientResponse $response) {
        $city = City::firstOrNew([
            'location_name' => $response->json('location.name'),
            'location_country' => $response->json('location.country')
        ]);    
        $city->save();

        $weatherData = WeatherData::firstOrNew([
            'city_id' => $city->id,
            'current_temperature' => $response->json('current.temperature'),
            'weather_icon' => $response->json("current.weather_icons")[0],
            'weather_description' => $response->json('current.weather_descriptions')[0],
            'wind_speed' => $response->json('current.wind_speed'),
            'wind_dir' => $response->json('current.wind_dir'),
            'localtime_epoch' => $response->json('location.localtime_epoch')
        ]);    
        $weatherData->save();
    }

    public static function storeApiRequest(StoreCityRequest $request) {
        $city = City::firstOrNew([
            'location_name' => $request->location_name,
            'location_country' => $request->location_country
        ]);    
        $city->save();
    
        $weatherData = WeatherData::firstOrNew([
            'city_id' => $city->id,
            'current_temperature' => $request->currentTemperature,
            'weather_icon' => $request->weatherIcon,
            'weather_description' => $request->weatherDescription,
            'wind_speed' => $request->windSpeed,
            'wind_dir' => $request->windDir,
            'localtime_epoch' => $request->localtimeEpoch
        ]);    
        $weatherData->save();

        return $city->id;
    }

    public function updateApiRequest($id, UpdateCityRequest $request) {
        $city = $this->getNewestWeatherDataByCityID($id);
    
        DB::table('cities')
            ->join('weather_data', 'cities.id', '=', 'weather_data.city_id')
            ->select('cities.location_name', 'cities.location_country', 'weather_data.current_temperature', 'weather_data.weather_icon', 'weather_data.weather_description', 'weather_data.wind_speed', 'weather_data.wind_dir', 'weather_data.localtime_epoch')
            ->where('cities.id', '=', $id)
            ->where('weather_data.localtime_epoch', '=', $city->localtime_epoch)          
            ->update([
                'cities.location_name' => $request->location_name,
                'cities.location_country' => $request->location_country,
                'weather_data.current_temperature' => $request->currentTemperature,
                'weather_data.weather_icon' => $request->weatherIcon,
                'weather_data.weather_description' => $request->weatherDescription,
                'weather_data.wind_speed' => $request->windSpeed,
                'weather_data.wind_dir' => $request->windDir,
                'weather_data.localtime_epoch' => $request->localtimeEpoch
            ]);
    }

}
