<?php

namespace Kylin\Community\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Kylin\Community\Traits\Encryptable;

/**
 * 居民信息
 */
class Resident extends Model
{
    use Encryptable;

    protected $fillable = [
        'user_id',
        'name',
        'id_card',
        'phone',
        'gender',
        'birthday',
        'address',
        'tags',
        'special_groups',
        'is_living',
    ];

    /**
     * 需要加密存储的敏感字段
     * 符合 GB/T 35273-2020 要求
     */
    protected array $encryptable = [
        'id_card',
        'phone',
    ];

    protected function casts(): array
    {
        return [
            'birthday' => 'date',
            'tags' => 'array',
            'special_groups' => 'array',
            'is_living' => 'boolean',
        ];
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(\App\Models\User::class);
    }

    public function houses(): BelongsToMany
    {
        return $this->belongsToMany(House::class, 'resident_house')
            ->withPivot('relation', 'is_owner')
            ->withTimestamps();
    }
}
