<?php

namespace App\DBRealTestSupport;

use Faker\Factory;
use Illuminate\Support\Facades\DB;
use App\DBRealTestSupport\TableCreation as TC;

class HtlCampLockMother
{

    public static function build1($idHtlCampSchedule = null, $id = 0)
    {
        $faker = Factory::create();
        $idHtlCampSchedule = $idHtlCampSchedule ?? HtlCampScheduleMother::push1()['Id_HtlCampSchedule'];
        return [
            'Id_HtlCampLock'             => $id,
            'HtlCampLock_Date'           => date('Y-m-d H:i:s'),
            'HtlCampLock_DateStart'      => date('Y-m-d', strtotime('+' . rand(1, 30) . ' days')),
            'HtlCampLock_Remark'         => rand(5, 1000),
            'HtlCampLock_Status'         => $faker->randomElement([2]),
            'Id_HtlCampSchedule'         => $idHtlCampSchedule,
        ];
    }

    public static function push1($idHtlCampSchedule = null, $id = 0)
    {
        return self::push(self::build1($idHtlCampSchedule, $id));
    }

    public static function push($object)
    {
        DB::select("INSERT INTO " . TC::HTL_CAMP_LOCK . " (
            `Id_HtlCampLock`,
            `HtlCampLock_Date`,
            `HtlCampLock_DateStart`,
            `HtlCampLock_Remark`,
            `HtlCampLock_Status`,
            `Id_HtlCampSchedule`
        ) VALUES (?,?,?,?,?,?)", [
            $object['Id_HtlCampLock'] === 0 ? null : $object['Id_HtlCampLock'],
            $object['HtlCampLock_Date'],
            $object['HtlCampLock_DateStart'],
            $object['HtlCampLock_Remark'],
            $object['HtlCampLock_Status'],
            $object['Id_HtlCampSchedule']
        ]);
        $response = DB::select('SELECT LAST_INSERT_ID() as Id');
        $object['Id_HtlCampLock'] = $response[0]->Id;
        return $object;
    }

    public static function tablesToClear()
    {
        return [TC::HTL_CAMP_LOCK, ...HtlCampScheduleMother::tablesToClear()];
    }
}
