<?php

namespace Kylin\LawCase\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kylin\LawCase\Traits\Encryptable;

/**
 * 客户模型
 *
 * 管理律所客户信息，敏感字段通过 Encryptable 加密存储。
 *
 * @property string $client_no   客户编号
 * @property string $client_type 客户类型
 * @property string $name        客户名称
 * @property string $phone       电话（加密）
 * @property string $email       邮箱（加密）
 */
class Client extends Model
{
    use SoftDeletes, Encryptable;

    protected $table = 'kylin_law_clients';

    protected $fillable = [
        'client_no',
        'client_type',
        'client_name',
        'id_number',
        'phone',
        'email',
        'address',
        'contact_person',
        'contact_phone',
        'is_conflict_checked',
        'tags',
        'description',
    ];

    protected array $encryptable = ['id_number', 'phone', 'email'];

    protected $casts = [
        'is_conflict_checked' => 'boolean',
        'tags' => 'array',
    ];

    public function cases()
    {
        return $this->hasMany(CaseModel::class);
    }

    public function fees()
    {
        return $this->hasMany(Fee::class);
    }

    public function conflictChecks()
    {
        return $this->hasMany(ConflictCheck::class);
    }

    public function scopeByType($query, string $type)
    {
        return $query->where('client_type', $type);
    }

    public function scopeSearchByName($query, string $name)
    {
        return $query->where('client_name', 'like', "%{$name}%");
    }
}
