<?php

use App\Models\Registries\Registry;
use App\Models\Registries\RegistryType;
use App\Models\User;
use App\Services\Registries\DevelopmentsImportService;
use App\Services\Registries\RulesetImportService;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;

/*
|--------------------------------------------------------------------------
| Console Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of your Closure based console
| commands. Each Closure is bound to a command instance allowing a
| simple approach to interacting with each command's IO methods.
|
*/

Artisan::command('dev:generate-personal-token {userId}', function ($userId) {
    $user = User::find($userId);
    $this->info('Token for user '.$user->name);
    $token = $user->createToken('Personal Access Token')->accessToken;
    $this->info($token);
})->describe('Generates a personal access token for a user');


Artisan::command('cleaner:clean', function() {
    $service = new \App\Services\Registries\CleanerService();
    $service->clean();
});



Artisan::command('htmlparser:import-rulesets', function() {
    $registry = Registry::query()->where(['type' => RegistryType::RULESET])->first();
    $url = "https://faufcc.ru/technical-regulation-in-constuction/formulary-list/";
    $service = new RulesetImportService($registry, $url);
    $service->import();
});

Artisan::command('htmlparser:import-developments', function() {
    $registry = Registry::query()->where(['type' => RegistryType::DEVELOPMENTS])->first();
    $urls = [
        "https://www.faufcc.ru/_deyatelnost/_tehnicheskoe_normirovanie/_razrabotka_svodov_pravil/",
        "https://www.faufcc.ru/_deyatelnost/_tehnicheskoe_normirovanie/_razrabotka_svodov_pravil/?PAGEN_1=2",
        "https://www.faufcc.ru/_deyatelnost/_tehnicheskoe_normirovanie/_razrabotka_svodov_pravil/?PAGEN_1=3"
    ];
    foreach ($urls as $url) {
        $service = new DevelopmentsImportService($registry, $url);
        $service->import();
    }
});

Artisan::command('dev:import-ntd', function() {
    Excel::import(new \App\Imports\NtdRegistryImport(), Storage::path('import/registries/ntd.xlsx'));
});

Artisan::command('htmlparser:import-ts', function() {
    $registry = Registry::query()->where(['type' => RegistryType::TECHNICAL_CERTIFICATES])->first();
    $url = 'https://www.faufcc.ru/_deyatelnost/_otsenka-prigodnosti/_reestr-tekhnicheskikh-svidetelstv/?PAGEN_1=';

    for ($i = 1; $i <= 44; $i++) {
        echo "Parsing page {$i}\n";
        $service = new \App\Services\Registries\TechnicalCertificatesImportService($registry, "{$url}{$i}");
        $service->import();
    }
});

Artisan::command('htmlparser:import-news', function() {
    $url = 'https://www.faufcc.ru/_press-tsentr/novosti/?PAGEN_1=';
    for ($i = 1; $i <= 88; $i++) {
        echo "Parsing page {$i}\n";
        $service = new \App\Services\Registries\NewsImportService(Registry::find(1), "{$url}{$i}");
        $service->import();
    }
});

Artisan::command('htmlparser:import-anticor', function() {
    $url = 'https://www.faufcc.ru/about-us/protivodeystvie-korruptsii/';
    $registry = \App\Models\Pages\Page::byUrl('/o-tsentre/protivodeistvie-korruptsii')->registry;
    $service = new \App\Services\Registries\AnticorImportService($registry, $url);
    $service->import();
});

