<?php

namespace Modules\Account\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Family extends Model
{
    use SoftDeletes;

    protected $table = 'account_families';
    protected $fillable = [
        'name',
        'owner_id',
        'invite_code'
    ];
    protected $dateFormat = 'Y-m-d H:i:s';

    protected function casts(): array
    {
        return [
            'created_at' => 'datetime:Y-m-d H:i:s',
            'updated_at' => 'datetime:Y-m-d H:i:s',
            'deleted_at' => 'datetime:Y-m-d H:i:s',
        ];
    }

    /**
     * 家主
     */
    public function owner()
    {
        return $this->belongsTo(\Modules\Member\Models\Member::class, 'owner_id', 'uid');
    }

    /**
     * 家庭成员
     */
    public function members()
    {
        return $this->belongsToMany(
            \Modules\Member\Models\Member::class,
            'account_family_members',
            'family_id',
            'user_id'
        )->withTimestamps();
    }

    /**
     * 家庭成员关系
     */
    public function familyMembers()
    {
        return $this->hasMany(FamilyMember::class);
    }

    /**
     * 账单
     */
    public function bills()
    {
        return $this->hasMany(Bill::class);
    }
}
