<?php

namespace App;

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

class Employee extends Model
{
	use SoftDeletes;
	
	// Employment Status Constants
	const STATUS_WORKING = 1;
	const STATUS_RESIGNED = 2;
	const STATUS_LEAVE = 3;
	
	// Gender Constants
	const GENDER_MALE = 1;
	const GENDER_FEMALE = 2;
	const GENDER_OTHER = 3;
	
	protected $fillable = [
		// Thông tin cơ bản
		'employee_code',
		'last_name', 'first_name',
		'last_name_kana', 'first_name_kana',
		'full_name', 'full_name_kana', 'preferred_name',
		'date_of_birth', 'gender',
		
		// Thông tin liên lạc
		'post_code', 'prefecture', 'city', 'address', 'building',
		'phone', 'mobile_phone', 'emergency_phone',
		'work_email', 'personal_email',
		
		// Thông tin công việc
		'join_date', 'department_id', 'position_id',
		'employee_type', 'employment_contract_type',
		'recruitment_method', 'course', 'level_id', 'salary_grade',
		'working_location', 'employment_status', 'resigned_date', 'resigned_reason',
		
		'is_created',
		'created_by',
		'updated_by',
		'is_activated'
	];
	
	protected $casts = [
		'recruitment_method' => 'integer',
		'salary_grade'       => 'integer',
		'employment_status'  => 'integer',
		'gender'             => 'integer',
		'resigned_reason'    => 'integer'
	];
	
	protected static function boot()
	{
		parent::boot();
		
		static::deleting(function ($employee) {
			$employee->positionHistories()->delete();
			$employee->levelHistories()->delete();
			$employee->leaveHistories()->delete();
			$employee->transitionPoint()->delete();
			$employee->retirementFund()->delete();
			$employee->severancePayment()->delete();
		});
		
		static::restoring(function ($employee) {
			$employee->positionHistories()->withTrashed()->restore();
			$employee->levelHistories()->withTrashed()->restore();
			$employee->leaveHistories()->withTrashed()->restore();
			$employee->transitionPoint()->withTrashed()->restore();
			$employee->retirementFund()->withTrashed()->restore();
			$employee->severancePayment()->withTrashed()->restore();
		});
	}
	
	// Existing Relationships
	public function department()
	{
		return $this->belongsTo(Department::class)->with(['parent']);
	}
	
	public function position()
	{
		return $this->belongsTo(Position::class);
	}
	
	public function level()
	{
		return $this->belongsTo(Level::class)->with(['parent', 'details']);
	}
	
	public function positionHistories()
	{
		return $this->hasMany(PositionHistory::class)->with('position')->orderBy('from_date', 'desc');
	}
	
	public function levelHistories()
	{
		return $this->hasMany(LevelHistory::class)->with('level')->orderBy('from_date', 'desc');
	}
	
	public function leaveHistories()
	{
		return $this->hasMany(LeaveHistory::class)->with('leaveType')->orderBy('from_date', 'desc');
	}
	
	public function transitionPoint()
	{
		return $this->hasOne(TransitionPoint::class);
	}
	
	public function retirementFund()
	{
		return $this->hasOne(RetirementFund::class);
	}
	
	public function severancePayment()
	{
		return $this->hasOne(SeverancePayment::class);
	}
	
	// Scopes
	public function scopeWorking($query)
	{
		return $query->where('employment_status', self::STATUS_WORKING);
	}
	
	public function scopeResigned($query)
	{
		return $query->where('employment_status', self::STATUS_RESIGNED);
	}
	
	public function scopeLeave($query)
	{
		return $query->where('employment_status', self::STATUS_LEAVE);
	}
}