<?php

namespace App\Models\Administration;

use App\Models\Application\UserGroup;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use App\Models\Bussiness\Collaborator;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\DB;
use Termwind\Components\Raw;

class User extends Authenticatable implements JWTSubject
{
    protected $table = 'sc_users';
    public $timestamps = false;

    protected $fillable = [
        'id',
        'fk_site',
        'username',
        'password',
        'fk_collaborator',
        'visible',
        'available',
        'fk_usercreator',
        'created_at',
        'fk_userupdater',
        'udapted_at',
        'is_active',
    ];

    protected $casts = [
        'id' => 'integer',
        'fk_site' => 'integer',
        'password' => 'hashed',
        'fk_collaborator' => 'integer',
        'visible' => 'boolean',
        'available' => 'boolean',
        'fk_usercreator' => 'integer',
        'created_at' => 'timestamp',
        'fk_userupdater' => 'integer',
        'updated_at' => 'timestamp',
        'is_active' => 'boolean'
    ];

    protected $hidden = [
        'fk_site',
        'password',
        'fk_collaborator',
        'fk_usercreator',
        'fk_userupdater',
        'is_active',
    ];

    protected static function booted()
    {
        static::updated(function ($user) {
            Log::channel('user')->info('User Updated', [
                'id' => $user->id,
                'fk_userupdater' => $user->fk_userupdater,
                'updated_at' => Carbon::now(),
                'changes' => $user->getChanges()
            ]);
        });
    }

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }

    public function site()
    {
        return $this->belongsTo(Site::class, 'fk_site', 'id');
    }

    public function collaborator()
    {
        return $this->belongsTo(Collaborator::class, 'fk_collaborator', 'id')->select('id', DB::raw("CONCAT(firstname, ' ', lastname, ' ', secondlastname) as fullname"));
    }

    public function offices()
    {
        return $this->hasManyThrough(
            Office::class,
            OfficeUser::class,
            'fk_office',
            'id',
            'id',
            'fk_user'
        );
    }

    public function role($fk_office)
    {
        return Role::select('sc_roles.id', 'rolename')
            ->join('sc_office_users', 'sc_roles.id', '=', 'sc_office_users.fk_role')
            ->where([['sc_office_users.fk_user', $this->id], ['sc_office_users.fk_office', $fk_office]])
            ->first();
    }

    public function user_groups()
    {
        return $this->hasMany(UserGroup::class, 'fk_user', 'id')->where('is_active', true)->with(['group']);
    }

    public function user_creator()
    {
        return $this->hasOne(User::class, 'id', 'fk_usercreator')->select('id', 'username');
    }

    public function user_updater()
    {
        return $this->hasOne(User::class, 'id', 'fk_userupdater')->select('id', 'username');
    }
}
