<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;

class City extends BaseModel
{
    public static function boot(): void
    {
        parent::boot();
        static::creating(function (City $city) {
            $city->user_uuid = auth()->check() ? auth()->id() : null;
        });
        static::updating(function (City $city) {
            $city->user_uuid = auth()->check() ? auth()->id() : null;
        });
    }

    public function Country(): BelongsTo
    {
        return $this->belongsTo(Country::class, 'country_uuid', 'uuid');
    }

    public function Translate(): void
    {
        $this->name = translate($this->name);
        $this->Country->name = translate($this->Country->en_name);
    }

    public static function CanCreate(): bool
    {
        return auth()->user()->type != 'user' || auth()->user()->HasPermission( 'create_cities');
    }

    public function CanEdit(): bool
    {
        return auth()->user()->type == 'admin' || $this->user_uuid == auth()->id() || auth()->user()->HasPermission('edit_cities');
    }

    public static function CanShow(): bool
    {
        return auth()->user()->type != 'user' || auth()->user()->HasPermission('show_cities');
    }

    public function CanDelete(): bool
    {
        return auth()->user()->type == 'admin' || auth()->user()->HasPermission('delete_cities');
    }
}
