<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Actions;
use Illuminate\Support\Facades\DB;

class WeatherController extends Controller
{

    public function GetWeatherCity(Request $request)
    {

        $model = new Actions\GetWeatherCity;

        if(!$model->Validate($request))
            return $model->Unsuccessful();

        if(!$model->Verify())
            return $model->Unsuccessful();

        return $model->Success(['data' => $model->Run()]);
    }

    public function GetWeatherProvince(Request $request)
    {

        $model = new Actions\GetWeatherProvince;

        if(!$model->Validate($request))
            return $model->Unsuccessful();

        if(!$model->Verify())
            return $model->Unsuccessful();

        return $model->Success(['data' => $model->Run()]);
    }

    public function GetPopularCities(Request $request)
    {

        $model = new Actions\GetPopularCities;

        return $model->Success(['data' => $model->Run()]);
    }

    public function Store(Request $request)
    {

        // $token = $request->bearerToken();
        // if ($token !== config('weather.api_token')) {
        //     return response()->json([
        //         'success' => false,
        //         'message' => 'Unauthorized'
        //     ], 401);
        // }

        $model = new Actions\Store;

        if(!$model->Validate($request))
            return $model->Unsuccessful();

        if(!$model->Verify())
            return $model->Unsuccessful();

        if(!$model->Run())
            return $model->Unsuccessful();

        return $model->Success([
            'message' => 'داده‌های هواشناسی با موفقیت ذخیره شد',
        ]);
    }

    public function StoreBatch(Request $request)
    {

        // $token = $request->bearerToken();
        // if ($token !== config('weather.api_token')) {
        //     return response()->json([
        //         'success' => false,
        //         'message' => 'Unauthorized'
        //     ], 401);
        // }

        $model = new Actions\StoreBatch;

        if(!$model->Validate($request))
            return $model->Unsuccessful();

        if(!$model->Verify())
            return $model->Unsuccessful();

        if(!$model->Run())
            return $model->Unsuccessful();

        return $model->Success([
            'message' => 'داده‌های هواشناسی با موفقیت ذخیره شد',
        ]);
    }

}
