<?php

namespace App\Domain\HR\Models;

use App\Domain\HR\Enums\CompensationModel;
use App\Domain\HR\Enums\TrainerStatus;
use App\Domain\Shared\Traits\BelongsToAcademy;
use App\Domain\Shared\Traits\HasUuid;
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\MorphMany;
use App\Domain\HR\Models\TrainerCompensation;
use App\Domain\HR\Models\Payslip;
use App\Domain\HR\Models\TrainerAdvance;
use App\Domain\HR\Models\TrainerRateHistory;
use App\Domain\Shared\Traits\BelongsToBranch;

class Trainer extends Model
{
    use BelongsToAcademy, HasUuid, 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 $fillable = [
        'academy_id', 'branch_id',
        'employee_id',
        'person_id',
        'trainer_number',
        'bio',
        'bio_ar',
        'specializations',
        'sports',
        'max_daily_sessions',
        'max_weekly_hours',
        'compensation_model',
        'hourly_rate',
        'session_rate',
        'group_rate',
        'player_rate',
        'revenue_share_percent',
        'rating',
        'total_sessions_conducted',
        'status',
    ];

    protected $casts = [
        'compensation_model' => CompensationModel::class,
        'status' => TrainerStatus::class,
        'specializations' => 'array',
        'sports' => 'array',
        'hourly_rate' => 'integer',
        'session_rate' => 'integer',
        'group_rate' => 'integer',
        'player_rate' => 'integer',
        'max_daily_sessions' => 'integer',
        'max_weekly_hours' => 'decimal:1',
        'revenue_share_percent' => 'decimal:2',
        'rating' => 'decimal:2',
        'total_sessions_conducted' => 'integer',
    ];

    // --- Relationships ---

    public function employee(): BelongsTo
    {
        return $this->belongsTo(Employee::class);
    }

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

    public function compensations(): HasMany
    {
        return $this->hasMany(TrainerCompensation::class);
    }

    public function payslips(): HasMany
    {
        return $this->hasMany(Payslip::class);
    }

    public function advances(): HasMany
    {
        return $this->hasMany(TrainerAdvance::class);
    }

    public function rateHistory(): HasMany
    {
        return $this->hasMany(TrainerRateHistory::class);
    }

    public function qualifications(): HasMany
    {
        return $this->hasMany(TrainerQualification::class);
    }

    public function availabilities(): HasMany
    {
        return $this->hasMany(TrainerAvailability::class);
    }

    public function assignments()
    {
        return $this->hasManyThrough(
            \App\Domain\Scheduling\Models\Assignment::class,
            Employee::class,
            'id',       // employees.id
            'user_id',  // assignments.user_id
            'employee_id', // trainers.employee_id
            'user_id'   // employees.user_id
        );
    }

    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', TrainerStatus::Active->value);
    }
}
