<?php

namespace App\Http\Repositories;

use App\Http\Enums\RoleType;
use App\Models\User;

class UserRepository
{
    function createUser($data)
    {
        $user = User::create($data);
        $user->assignRole(RoleType::Customer->value);
        return $user;
    }

    function userByNotVerifiedPhone($phone)
    {
        $user = User::where('phone', $phone)->first();

        if(!$user) respond()->notFound(__("The phone does not exist"))->throwResponse();
        if($user->email_phone_at) respond()->notFound(__("Phone is activated"))->throwResponse();

        return $user;
    }

    public function userWithRole($role)
    {
        return User::where('role', $role)->get();
    }

    public function getUsers()
    {
        return User::all();
    }

    public function findUserById($id)
    {
        return User::findOrFail($id);
    }

    function userByVerifiedEmail($email)
    {
        $user = User::where('email', $email)->first();

        if(!$user) respond()->notFound(__("The email does not exist"))->throwResponse();
        if(!$user->email_verified_at) respond()->notFound(__("Email is not activated"))->throwResponse();

        return $user;
    }

    function userByVerifiedPhone($phone)
    {
        $user = User::where('phone', $phone)->first();

        if(!$user) respond()->notFound(__("The phone does not exist"))->throwResponse();
        if(!$user->phone_verified_at) respond()->notFound(__("Phone is not activated"))->throwResponse();

        return $user;
    }

    function userByEmail($email)
    {
        return User::where('email', $email)->first();
    }

    function userByPhone($phone)
    {
        return User::where('phone', $phone)->first();
    }

    function getUserById($id)
    {
        return User::findOrFail($id);
    }

    public function deleteUserById(int $id)
    {
        $user = User::findOrFail($id);
        $user->delete();
        return true;
    }

    function updateUserById(int $id, array $data)
    {
        $user = User::findOrFail($id);
        $user->update($data);

        return $user;
    }
}
