<?php

namespace App\DBRealTestSupport\TransactionSupported\Session;

use App\Modules\Shared\Application\Util;
use Faker\Factory;
use Illuminate\Support\Facades\DB;
use App\DBRealTestSupport\TableCreation as TC;

class TSStateMother
{

    public static function buildRandom($idCountry = null, $id = 1)
    {
        $faker = Factory::create();
        return [
            'Id_State'              => $id,
            'State_Code'            => Util::generateString(3),
            'State_Name'            => $faker->text(rand(5, 250)),
            'State_Status'          => $faker->randomElement([1, 2]),
            'Id_Country'            => $idCountry !== null ? $idCountry : TSCountryMother::pushRandom()['Id_Country'],
        ];
    }

    public static function pushRandom($idCountry = null, $id = 1)
    {
        return self::push(self::buildRandom($idCountry, $id));
    }

    private static function push($object)
    {
        DB::insert("INSERT INTO `t_state` (
            `State_Code`,
            `State_Name`,
            `State_Status`,
            `Id_Country`
        ) VALUES (?,?,?,?)", [
            $object['State_Code'],
            $object['State_Name'],
            $object['State_Status'],
            $object['Id_Country']
        ]);
        $object['Id_State'] = DB::select('SELECT LAST_INSERT_ID() as `Id`')[0]->Id;
        return $object;
    }

    public static function tablesToClear()
    {
        return [TC::STATE];
    }
}
