<?php

namespace App\Repositories;

use App\Models\User;
use Carbon\Carbon;

class UserRepository
{
    public static function getUserById($id,$required_fields = [])
    {
        $query = User::where('id',$id);
        if($required_fields) $query->select($required_fields);
        return $query->first();
    }

    public static function getUserByEmail($email,$required_fields = [])
    {
        $query = User::where('email',$email);
        if($required_fields) $query->select($required_fields);
        return $query->first();
    }

    public static function getReferrals($email)
    {
        return User::where('referenceEmail',$email)->where('status','approved')->get();
    }

    public static function create($payload)
    {
        $user = User::create($payload);
        return $user;
    }

    public static function fetchItems($filters)
    {
        if(isset($filters['status']) && $filters['status']){
            switch ($filters['status']){
                case 'pending':
                    return User::where('status',$filters['status'])->simplePaginate(20);
                case 'paid':
                    return User::where('status','pending')->where('paymentStatus',true)->simplePaginate(20);
                case 'expired':
                    return User::where('status','!=','expired')->where('subscription_expired_time','<=',Carbon::now('UTC')->toDateTimeString())->simplePaginate(20);
            }
        }
    	return User::simplePaginate(20);
    }

    public static function fetchItemsCounts($filters)
    {
        switch ($filters['status']){
            case 'paid':
                return User::where('status','pending')->where('paymentStatus',true)->count();
            case 'expired':
                return User::where('status','!=','expired')->where('subscription_expired_time','<=',Carbon::now('UTC')->toDateTimeString())->count();
        }
    }

    public static function getReferences($email,$required_fields = []){
        $query = User::where('referenceEmail',$email);
        if($required_fields) $query->select($required_fields);
        return $query->get();
    }

    public static function changeUserStatus($payload)
    {
        $user = User::where('id',$payload['id'])->first();
        if($user){
            $user->status = $payload['status'];
            if($payload['status'] == "approved"){

                $package = ProjectsRepository::fetchPackage(['id' => $user['subscription']]);
                if($package){
                    $user->subscription_time = Carbon::now('UTC')->toDateTimeString();
                    $user->subscription_expired_time = Carbon::now('UTC')->addMonths($package['duration'])->toDateTimeString();
                    return $user->save();
                }
                return false;
            }
            return $user->save();
        }
        return false;
    }

    public static function deleteUser($filters)
    {
        return User::where('id',$filters['id'])->delete();
    }
}
