<?php

namespace App\Models;

use App\Libs\Database\Eloquent\ContentTypeModel;
use App\Models\GenericBackoffice\Traits\LanguageSupportTrait;

class Country extends ContentTypeModel
{
    use LanguageSupportTrait;

    protected $hidden = ['languages', "websites"];
    protected $with = ['languages', "websites"];

    protected $dates = []; // dates

    protected $casts = [];
    protected $table = 'countries';

    public function cities()
    {
        return $this->hasMany(City::class);
    }

    /**
     * The "booted" method of the model.
     *
     * @return void
     */
    protected static function booted()
    {
        static::saving(function ($country) {

            //We want to ensure that only one country has the main flag on
            if ($country->main_country) {
                self::query()->update(['main_country' => 0]);
            }
        });
    }
}
