<?php

class UserTableSSNDecrypter extends Seeder {

    public function run()
    {
        // True to show messages
        $debug = false;

        // Disable query logging to avoid memory exhaustion
        DB::disableQueryLog();

        $this->command->info('Users SSN Decryption started');

        // Initiates time
        $time_start = microtime(true);

        try
        {
            // Get all users in the database
            $users = User::all();

            // Iterate over all users
            foreach ($users as $user)
            {
                // Skip update if too short in length
                if(strlen($user->ssn)<10)
                    continue;

                // Update table
                DB::table('users')
                    ->where('id', $user->id)
                    ->update(array(
                        'ssn' => Crypt::decrypt($user->ssn),
                        // 'program_id' => $program_id, //should change when I know how this info will be given
                        // 'updated_at' => date("Y-m-d H:i:s", time()),
                        )
                    );

                if($debug){
                    $this->command->info('Decrypted ssn for '.$user->email.': '.$user->surnames.', '.$user->first_name);
                }

            }

        }

        // If an exception is raised, show the message and add to error
        catch(Exception $e)
        {
            $this->command->info($e->getMessage());
        };


        // Stop time
        $time_end = microtime(true);

        // Display separator
        $this->command->info('------------------------------------------------------------');
    }

}