<?php

namespace App\Http\Controllers\API;

use App\City;
use App\About;
use App\Http\Requests\API\VisitorMessageRequest;
use App\Http\Resources\ContactUsResource;
use App\Http\Resources\DisclaimerResource;
use App\Http\Resources\RoomResource;
use App\Policy;
use App\Room;
use App\Screen;
use App\Country;
use App\Helpers\ImageHelper;
use App\VisitorMessage;
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 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();
        $disclaimer = Policy::withDescription();

        return response()->json([
            'message' => '',
            'data' => [
                'escorts_maximum' => 25,
                'logo' => $logo,
                'languages' => LanguagesResource::collection($language),
                'screens' => ScreensResource::make($screens),
                'abouts' => AboutsResource::make($abouts),
                'terms' => TermsResource::make($terms),
                'disclaimer' => DisclaimerResource::make($disclaimer),
            ]
        ], 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' => '', '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' => '', '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' => '', 'data' => $data], 200);
    }

    public function rooms($room_id = null)
    {
        $rooms = Room::withDescription($room_id);
        if (!$rooms){
            return response()->json(['message' => __('rooms.no_data'), 'data' => null], 400);
        }
        $data = $room_id ? RoomResource::make($rooms) : RoomResource::collection($rooms);
        return response()->json(['message' => '', 'data' => $data], 200);
    }

    public function contactUs(VisitorMessageRequest $request)
    {
        $data = $request->all();
        $data['phone'] = $request->code.$request->phone;
        $contactUs = VisitorMessage::create($data);
        return response()->json(['message' => '', 'data' => ContactUsResource::make($contactUs)], 200);
    }
}
