<?php

namespace App\Console\Commands;

use App\Models\flibusta;
use Illuminate\Console\Command;

class createdb extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'createdb';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        //Гурджиев;Лаврентий;Константинович;Сталин шутит…;litres;ru;2013;Проза великих 0;361528
        $txt = file_get_contents('/var/www/html/booksbot/storage/catalog.txt');
        $rows = explode("\n",$txt);
        $l=0;
        foreach ($rows as $row) {
            $l++;
            try {
                if ($l == 1) {
                    continue;
                }
                $row = explode(";", $row);
                $last_name = $row[0];
                $first_name = $row[1];
                $middle_name = $row[2];
                $title = $row[3];
                $lang = $row[5];
                $year = $row[6];
                $collection = $row[7];
                $id = $row[8];




                $last_name = str_replace(['"', "'"], "", $last_name);
                $first_name = str_replace(['"', "'"], "", $first_name);
                $middle_name = str_replace(['"', "'"], "", $middle_name);
                $title = str_replace(['"', "'"], "", $title);
                $year = str_replace(['"', "'"], "", $year);
                $collection = str_replace(['"', "'"], "", $collection);
                $lang = str_replace(['"', "'"], "", $lang);
                $id = str_replace(['"', "'"], "", $id);

                $flibusta = new flibusta();
                $flibusta->query = trim(implode(" ", [
                    $last_name,
                    $first_name,
                    $middle_name,
                    $title,
                    $year,
                    $collection,
                    $lang,
                    $id
                ]));
                $flibusta->first_name = $first_name;
                $flibusta->middle_name = $middle_name;
                $flibusta->last_name = $last_name;
                $flibusta->title = $title;
                $flibusta->title = $title;
                $flibusta->year = $year;
                $flibusta->collection = $collection;
                $flibusta->lang = $lang;
                $flibusta->flibusta_id = $id;
                $flibusta->save();
            }catch (\Exception $e){

            }
        }
    }
}
