<?php

use Illuminate\Database\Seeder;
use App\AnagraficaTipo;
use App\Anagrafica;

class StagingAnagraficaTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //(0=cliente/1=fornitore/2=committente/3=tecnico)
        AnagraficaTipo::truncate();
        Anagrafica::truncate();
        

        AnagraficaTipo::create([ 
            'id' => 1,
            'nome' => 'Cliente'
        ]); 
        AnagraficaTipo::create([ 
            'id' => 2,
            'nome' => 'Fornitore'
        ]);         
        AnagraficaTipo::create([ 
            'id' => 3,
            'nome' => 'Committente'
        ]); 
        //=====================================================
        
        $faker = Faker\Factory::create('it_IT');

        $anagraficaCount = 50;
        $typesIds = $this->getCategoriesIdsArray();

        for($c = 0; $c < $anagraficaCount; $c++)
        {
           

            // creo l'articolo
            $anagrafica = Anagrafica::create([
                'ragione_sociale' => $faker->company,
                'partita_iva' => $faker->text(20),
                'codice_fiscale' => $faker->text(13),
                'nome' => $faker->firstName,  
                'cognome' => $faker->lastName,
                
                'cap' => $faker->postcode,
                
                'indirizzo' => $faker->streetAddress,
                'citta' => $faker->city,
                
                'email' => $faker->email,
                'telefono' => $faker->phoneNumber,
                
                'note' => $faker->paragraph(10),

//                'is_published' => $faker->boolean(80),
//                'published_at' => $faker->dateTimeThisYear($max = 'now'),
//
//                'meta_keys' => implode(',', $faker->words(5)),
//                'meta_description' => $faker->sentence(),

                'anagrafica_tipo_id' => $faker->numberBetween(1,3)
                
                // associo casualmente un autore all'articolo
                //'user_id' => User::orderByRaw('RAND()')->first()->id
            ]);

            // associo le categorie casualmente (una o due per articolo)
            //$article->categories()->sync($faker->randomElements($typesIds, mt_rand(1, 2, 3)));
        }
    }
    
    private function getCategoriesIdsArray()
    {
        $result = [];

        $types = AnagraficaTipo::all();
        foreach($types as $type)
        {
            $result[] = $type->id;
        }

        return $result;
    }

}
