<?php

namespace App\Imports;

use App\Models\User;
use App\Models\Subscription;
use App\Models\Package;
use App\Models\Wallet;
use Maatwebsite\Excel\Concerns\ToModel;
use Illuminate\Support\Facades\Hash;


class UsersImport implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {

        $randomNumber = rand(1000, 9999);

        

    $user =  User::create([
            'name' => $row[1],
            'email' => $row[3].'@email.com',
            'password' =>  Hash::make(1234),
            'phone' => '0'.$row[3],
            'location' => $row[2],
            'region' => 1,
            'role' => 2
        ]);
        $price = Package::where('id',$row[0])->first();
        if($price)
        {
            $amount = $price->price;
        }else{
            $amount = 0;
        }
        $Subscription = Subscription::create([
            'username' => '0'.$row[3],
            'package' => $row[0],
            'password'=>  Hash::make(1234),
            'user_id' => $user->id,
            'status' => 1,
            'amount' => $amount,
             'start_date' => now(),
            'end_date' => now()->addMonths(1),
        ]);
        $balance = Wallet::create([
            'user_id' => $user->id,
            'amount' => '0.0',
        ]);

    return $user;




    }
}
