<?php

namespace App\Http\Controllers\API;

use App\City;
use App\About;
use App\Policy;
use App\Screen;
use App\Country;
use App\Helpers\ImageHelper;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Http\Resources\CityResource;
use App\Http\Resources\TermsResource;
use App\Http\Resources\AboutsResource;
use App\Http\Resources\CountryResource;
use App\Http\Resources\ScreensResource;
use App\Http\Resources\LanguagesResource;

class HomeController extends Controller
{

    /**
     * Return some scattered information like [about us - terms - language- etc ...]
     * @return \Illuminate\Http\JsonResponse
     */
    public function infoCollection() :JsonResponse
    {
        $logo = array_key_exists('website_logo', settings()) ? settings('website_logo') : 'assets/web/images/logo.png';
        $logo = ImageHelper::getFullPath($logo);
        $language = languages();
        $screens = Screen::withDescription();
        $abouts = About::getAbout();
        $terms = Policy::withDescription();

        return response()->json([
            'message' => 'success',
            'data' => [
                'logo' => $logo,
                'languages' => LanguagesResource::collection($language),
                'screens' => ScreensResource::collection($screens),
                'abouts' => AboutsResource::collection($abouts),
                'terms' => TermsResource::collection($terms),
            ]
        ], 200);

    }

    /**
     * Get all countries or by country id
     * @param null $country
     * @return JsonResponse
     */
    public function countries($country = null) :JsonResponse
    {
        $countries = Country::withDescription($country);
        $data = $country ? CountryResource::make($countries) : CountryResource::collection($countries);
        return response()->json(['message' => 'success', 'data' => $data], 200);
    }

    /**
     * Get cities by country id or city id or with together
     * @param null $country
     * @param null $city
     * @return JsonResponse
     */
    public function cities($country = null, $city = null)
    {
        $cities = City::withDescription($country, $city);
        if (!$cities){
            return response()->json(['message' => __('cities.no_data'), 'data' => null], 400);
        }

        $data = $city ? CityResource::make($cities) : CityResource::collection($cities);
        return response()->json(['message' => 'success', 'data' => $data], 200);
    }

    /**
     * Get city by id only
     * @param null $city
     * @return JsonResponse
     */
    public function city($city = null)
    {
        if (!$city){
            return response()->json(['message' => __('cities.no_data'), 'data' => null], 400);
        }
        $cities = City::withDescription(null, $city);
        if (!$cities){
            return response()->json(['message' => __('cities.no_data'), 'data' => null], 400);
        }
        $data = $city ? CityResource::make($cities) : CityResource::collection($cities);
        return response()->json(['message' => 'success', 'data' => $data], 200);
    }
}
