<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Helpers\FileUploadHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Laravel\Sanctum\HasApiTokens;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use HasApiTokens, HasFactory, Notifiable;
    // Rest omitted for brevity

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'profile',
        'phone',
        'role',
        'status',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
        'email_verified_at',
        'status',
        'create_by',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
    ];

    private static function saveBasicInfo($user, $request, $image = true)
    {
        if ($request->has('profile') && $image) {
            $user->profile = FileUploadHelper::uploadImage($request->file('profile'), 'storage/profile/', $user->profile, true);
        }
        $user->name = $request->name;
        $user->email = $request->email;
        $user->phone = $request->phone;
        $user->save();

        return $user;
    }

    public static function newUser($request)
    {
        $user = new User;
        $user->password = Hash::make($request->password);
        $user->create_by = Auth::id();
        $user->role = Auth::user()->role == 'admin' ? 'user' : (Auth::user()->role == 'user' ? 'staff' : '');

        return self::saveBasicInfo($user, $request);
    }

    public static function updateUser($request)
    {
        $user = User::query()->find(Auth::id());

        return self::saveBasicInfo($user, $request, false);
    }

    public static function photoUpdate($request)
    {
        $user = User::query()->find(Auth::id());
        $user->profile = FileUploadHelper::uploadImage($request->file('profile'), 'storage/profile/', $user->profile, true);
        $user->save();

        return [
            'image' => asset($user->profile),
        ];
    }

    public static function change_status($user_id): array
    {
        $targeted_user = User::query()->find($user_id);
        $authenticated_user = User::query()->find(Auth::id());
        if (($authenticated_user->role == 'admin' && $targeted_user->role == 'user') || ($authenticated_user->role == 'user' && $targeted_user->role == 'staff')) {
            if ($targeted_user->status == '1') {
                $targeted_user->status = '0';
                $message = 'User inactive successfully!';
            } else {
                $targeted_user->status = '1';
                $message = 'User active successfully!';
            }
            $targeted_user->save();

            return [
                'message' => $message,
                'status' => 200,
            ];
        }

        return [
            'message' => 'Invalid Process',
            'status' => 400,
        ];
    }
}
