<?php

namespace App\Domain\HR\Models;

use App\Domain\HR\Enums\EmployeeStatus;
use App\Domain\HR\Enums\EmploymentType;
use App\Domain\HR\Enums\SalaryFrequency;
use App\Domain\Shared\Traits\BelongsToAcademy;
use App\Domain\Shared\Traits\HasUuid;
use App\Domain\Shared\Traits\ManglesUniqueOnDelete;
use Illuminate\Database\Eloquent\Builder;
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\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Domain\Shared\Traits\BelongsToBranch;

class Employee extends Model
{
    use BelongsToAcademy, HasUuid, SoftDeletes, ManglesUniqueOnDelete, BelongsToBranch;

    /**
     * branch_id NULL means every branch uses this row — catalogue and
     * configuration, not an operational record owned by one branch.
     */
    protected static bool $branchScopeAllowsShared = true;

    protected array $uniqueFieldsToMangle = ['employee_number'];
    protected array $mangleMaxLengths = ['employee_number' => 20];

    protected $fillable = [
        'academy_id',
        'person_id',
        'user_id',
        'employee_number',
        'department',
        'position',
        'employment_type',
        'start_date',
        'end_date',
        'salary_amount',
        'salary_frequency',
        'working_hours_per_week',
        'branch_id',
        'manager_id',
        'status',
        'termination_reason',
        'notes',
        'metadata',
        'created_by',
        'updated_by',
    ];

    protected $casts = [
        'employment_type' => EmploymentType::class,
        'status' => EmployeeStatus::class,
        'salary_frequency' => SalaryFrequency::class,
        'salary_amount' => 'integer',
        'working_hours_per_week' => 'decimal:1',
        'start_date' => 'date',
        'end_date' => 'date',
        'metadata' => 'array',
    ];

    // --- Relationships ---

    public function person(): BelongsTo
    {
        return $this->belongsTo(\App\Domain\Identity\Models\Person::class);
    }

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

    public function branch(): BelongsTo
    {
        return $this->belongsTo(\App\Domain\Identity\Models\Branch::class);
    }

    public function manager(): BelongsTo
    {
        return $this->belongsTo(self::class, 'manager_id');
    }

    public function directReports(): HasMany
    {
        return $this->hasMany(self::class, 'manager_id');
    }

    public function trainer(): HasOne
    {
        return $this->hasOne(Trainer::class);
    }

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

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

    public function documents(): MorphMany
    {
        return $this->morphMany(\App\Domain\Document\Models\Document::class, 'documentable');
    }

    // --- Scopes ---

    public function scopeActive(Builder $query): Builder
    {
        return $query->where('status', EmployeeStatus::Active->value);
    }

    public function scopeForBranch(Builder $query, ?int $branchId): Builder
    {
        return $query->where(function (Builder $q) use ($branchId) {
            $q->whereNull('branch_id')
                ->orWhere('branch_id', $branchId);
        });
    }
}
