<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\TravelCountry
 *
 * @property int $id
 * @property int|null $crm_id
 * @property string $name
 * @property string|null $html_information
 * @property string|null $entry_requirements
 * @property int|null $feedback_page_id
 * @property int|null $is_customer_country
 * @property int|null $active_frontend
 * @property int|null $active_backend
 * @property string|null $updated_at
 * @property string|null $created_at
 * @property-read \App\Models\Page|null $feedback_page
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\TravelNationalityRequirement[] $travel_nationality_requirements
 * @property-read \App\Models\TravelProgramCountry $travel_program_country
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TravelCountry whereActiveBackend($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TravelCountry whereActiveFrontend($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TravelCountry whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TravelCountry whereCrmId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TravelCountry whereEntryRequirements($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TravelCountry whereFeedbackPageId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TravelCountry whereHtmlInformation($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TravelCountry whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TravelCountry whereIsCustomerCountry($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TravelCountry whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TravelCountry whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class TravelCountry extends Model
{
    //use the connection to sec. Datebase sterntours
    protected $connection = 'mysql_stern';

    protected $table = 'travel_country';


    protected $fillable = [
        'name',
        'html_information',
        'entry_requirements',
        'is_customer_country',
        'active_frontend',
        'active_backend'

    ];

    public $timestamps = false;

    public function travel_program_country()
    {
        return $this->hasOne('App\Models\TravelProgramCountry', 'country_id', 'id');
    }

    public function feedback_page()
    {
        return $this->belongsTo('App\Models\Page', 'feedback_page_id', 'id');
    }

    public function travel_nationality_requirements()
    {
        return $this->hasMany('App\Models\TravelNationalityRequirement', 'travel_country_id', 'id');
    }


    public function getNationalityRequirement($travel_nationality_id){

       $model = TravelNationalityRequirement::where('travel_country_id', $this->id)->where('travel_nationality_id', $travel_nationality_id)->first();
       if($model){
            return $model->text;
       }
       return "";
    }

    public function setNationalityRequirement($travel_nationality_id, $text){
        $model = TravelNationalityRequirement::where('travel_country_id', $this->id)->where('travel_nationality_id', $travel_nationality_id)->first();
        if($model){
            $model->text = $text;
            $model->save();
        }else{
            $data = [
                'travel_country_id' => $this->id,
                'travel_nationality_id' => $travel_nationality_id,
                'text' => $text,
            ];
            TravelNationalityRequirement::create($data);
        }
        return "";
    }
}