<?php

namespace App\Services;

use App\Models\City;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use App\Exceptions\ApiHttpException;
use App\Libs\Api\ParsedListParams;
use App\Models\Country;
use Illuminate\Database\Eloquent\Builder;
use App\Services\DataService;

class CountryDataService extends DataService
{



    /**
     * Gets the main country code for the app
     *
     * @return string
     */
    public function getMainCountryCode(): string
    {
        $country = Country::where('main_country', 1)->first();
        if ($country) {
            return $country->code;
        }

        return 'pt';
    }


    /**
     * applyQueryParams
     *
     * @param Builder &$query
     * @param ParsedListParams $params
     * @return void
     */
    private function applyQueryParams(Builder $query, ParsedListParams $params): void
    {

        $orderDirection = $params->orderDirection;
        $orderBy        = $params->orderBy;
        if ($orderBy === 'name') {
            $orderBy = 'title';
        }
        $query->orderBy($orderBy, $orderDirection);

        // Apply the specific Filters
        // City name Filter
        if (isset($params->filters['name'])) {
            $query->where('title', 'like', $params->filters['name'] . '%');
        }
    }
}
