<?php

namespace App\Models\System;

use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class City extends Model
{
    use ModelTrait;
    protected $table = 'system_city';

    protected $fillable = [
        'title',
        'code',
        'parent_code',
    ];

    /**
     * 子级城市
     */
    public function children(): HasMany
    {
        return $this->hasMany(City::class, 'parent_code', 'code');
    }

    /**
     * 父级城市
     */
    public function parent()
    {
        return $this->belongsTo(City::class, 'parent_code', 'code');
    }

    /**
     * 判断是否是顶级城市（省/直辖市/自治区）
     */
    public function isTopLevel(): bool
    {
        return empty($this->parent_code);
    }
}
