<?php

declare(strict_types=1);

namespace Kylin\Hr\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kylin\Hr\Enums\EmployeeStatus;
use Kylin\Hr\Enums\EmploymentType;
use Kylin\Hr\Enums\Gender;

/**
 * 员工模型
 *
 * 管理员工基本信息、部门职位、入职离职等。
 *
 * @property string $employee_no  工号
 * @property string $name         姓名
 * @property string $gender       性别
 * @property string $status       在职状态
 * @property string $hire_date    入职日期
 */
class Employee extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'user_id',
        'name',
        'code',
        'gender',
        'id_card',
        'phone',
        'email',
        'department_id',
        'position_id',
        'hire_date',
        'status',
        'employment_type',
        'base_salary',
        'address',
        'emergency_contact',
        'emergency_phone',
    ];

    protected $hidden = [
        'id_card',
    ];

    protected function casts(): array
    {
        return [
            'gender' => Gender::class,
            'status' => EmployeeStatus::class,
            'employment_type' => EmploymentType::class,
            'hire_date' => 'date',
            'base_salary' => 'decimal:2',
        ];
    }

    /**
     * 关联用户
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(\App\Models\User::class);
    }

    /**
     * 所属部门
     */
    public function department(): BelongsTo
    {
        return $this->belongsTo(Department::class);
    }

    /**
     * 所属岗位
     */
    public function position(): BelongsTo
    {
        return $this->belongsTo(Position::class);
    }

    /**
     * 合同
     */
    public function contracts(): HasMany
    {
        return $this->hasMany(Contract::class);
    }

    /**
     * 当前合同
     */
    public function activeContract(): HasOne
    {
        return $this->hasOne(Contract::class)->where('status', 1)->latestOfMany();
    }

    /**
     * 考勤记录
     */
    public function attendanceRecords(): HasMany
    {
        return $this->hasMany(AttendanceRecord::class);
    }

    /**
     * 请假记录
     */
    public function leaveRequests(): HasMany
    {
        return $this->hasMany(LeaveRequest::class);
    }

    /**
     * 加班记录
     */
    public function overtimeRequests(): HasMany
    {
        return $this->hasMany(OvertimeRequest::class);
    }

    /**
     * 薪资记录
     */
    public function salarySlips(): HasMany
    {
        return $this->hasMany(SalarySlip::class);
    }

    /**
     * 社保公积金
     */
    public function socialInsurance(): HasOne
    {
        return $this->hasOne(SocialInsurance::class)->latestOfMany();
    }

    /**
     * 离职记录
     */
    public function resignation(): HasOne
    {
        return $this->hasOne(EmployeeResignation::class);
    }

    /**
     * 调动记录
     */
    public function transfers(): HasMany
    {
        return $this->hasMany(EmployeeTransfer::class);
    }

    /**
     * 身份证号脱敏访问器
     */
    public function getIdCardMaskedAttribute(): string
    {
        $idCard = $this->getRawOriginal('id_card');
        if (strlen($idCard) === 18) {
            return substr($idCard, 0, 6) . '********' . substr($idCard, -4);
        }
        return $idCard;
    }

    /**
     * 手机号脱敏访问器
     */
    public function getPhoneMaskedAttribute(): string
    {
        $phone = $this->phone;
        if (strlen($phone) === 11) {
            return substr($phone, 0, 3) . '****' . substr($phone, -4);
        }
        return $phone;
    }

    /**
     * 查询作用域：在职员工
     */
    public function scopeActive($query)
    {
        return $query->where('status', EmployeeStatus::Active);
    }

    /**
     * 查询作用域：试用期员工
     */
    public function scopeOnTrial($query)
    {
        return $query->where('status', EmployeeStatus::Trial);
    }
}
