<?php

namespace App\DBRealTestSupport\TransactionSupported;

use Faker\Factory;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use App\DBRealTestSupport\TableCreation as TC;
use App\DBRealTestSupport\TransactionSupported\Session\TSCityMother;
use App\DBRealTestSupport\TransactionSupported\Session\TSCountryMother;
use App\DBRealTestSupport\TransactionSupported\Session\TSStateMother;

class TSTourMother
{

    public static function buildRandom(
        $idCategoryTour = null,
        $idTypeTour = null,
        $idCountry = null,
        $idState = null,
        $idCity = null,
        $id = 1
    ) {
        $faker = Factory::create();
        $country = $idCountry ?? TSCountryMother::pushRandom()['Id_Country'];
        $state = $idState ?? TSStateMother::pushRandom($country)['Id_State'];
        $city = $idCity ?? TSCityMother::pushRandom($state)['Id_City'];
        return [
            'Id_Tour'                   => $id,
            'Tour_Code'                 => Str::upper($faker->text(rand(5, 50))),
            'Tour_Name'                 => Str::title($faker->text(rand(5, 250))),
            'Tour_UrlThumbnail'         => $faker->text(rand(5, 500)),
            'Tour_Duration'             => rand(1, 10),
            'Tour_MinPax'               => rand(0, 10),
            'Tour_Availability'         => rand(5, 10),
            'Tour_AvailabilityStatus'   => $faker->randomElement([1, 2]),
            'Tour_Anticipation'         => rand(0, 10),
            'Tour_AnticipationType'     => $faker->randomElement([1, 2, 3]),
            'Tour_Color'                => $faker->text(rand(5, 10)),
            'Tour_Score'                => 0,
            'Tour_ScoreCant'            => 0,
            'Tour_ScoreTotal'           => 0,
            'Tour_Status'               => $faker->randomElement([1, 2]),
            'Id_CategoryTour'           => $idCategoryTour ?? TSCategoryTourMother::pushRandom()['Id_CategoryTour'],
            'Id_TypeTour'               => $idTypeTour ?? TSTypeTourMother::pushRandom()['Id_TypeTour'],
            'Id_Country'                => $country,
            'Id_State'                  => $state,
            'Id_City'                   => $city
        ];
    }

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

    private static function push($object)
    {
        DB::select("INSERT INTO `t_tour` (
            `Tour_Code`,
            `Tour_Name`,
            `Tour_UrlThumbnail`,
            `Tour_Duration`,
            `Tour_MinPax`,
            `Tour_Availability`,
            `Tour_AvailabilityStatus`,
            `Tour_Anticipation`,
            `Tour_AnticipationType`,
            `Tour_Color`,
            `Tour_Score`,
            `Tour_ScoreCant`,
            `Tour_ScoreTotal`,
            `Tour_Status`,
            `Tour_BriefingDaysBefore`,
            `Tour_BriefingTime`,
            `Tour_BriefingText`,
            `Tour_BriefingUseCredential`,
            `Id_CategoryTour`,
            `Id_TypeTour`,
            `Id_Country`,
            `Id_State`,
            `Id_City`
        ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", [
            $object['Tour_Code'],
            $object['Tour_Name'],
            $object['Tour_UrlThumbnail'],
            $object['Tour_Duration'],
            $object['Tour_MinPax'],
            $object['Tour_Availability'],
            $object['Tour_AvailabilityStatus'],
            $object['Tour_Anticipation'],
            $object['Tour_AnticipationType'],
            $object['Tour_Color'],
            $object['Tour_Score'],
            $object['Tour_ScoreCant'],
            $object['Tour_ScoreTotal'],
            $object['Tour_Status'],
            1,
            '17:00:00',
            '',
            2,
            $object['Id_CategoryTour'],
            $object['Id_TypeTour'],
            $object['Id_Country'],
            $object['Id_State'],
            $object['Id_City'],
        ]);
        $object['Id_Tour'] = DB::select('SELECT LAST_INSERT_ID() as Id')[0]->Id;
        return $object;
    }

    public static function clear()
    {
        return [
            TC::tour,
            ...TSCategoryTourMother::tablesToClear(),
            ...TSTypeTourMother::tablesToClear(),
            ...TSCountryMother::tablesToClear(),
            ...TSStateMother::tablesToClear(),
            ...TSCityMother::tablesToClear()
        ];
    }
}
