<?php

namespace App\Console\Commands\Utils;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

trait ResetTrait
{
    /**
     * Reset icons
     */
    protected function resetIcons() : void
    {
        $this->deleteIcons();
        $this->generateIcons();
    }

    /**
     * Delete all icons
     */
    protected function deleteIcons() : void
    {
        $filesForDelete = \Illuminate\Support\Facades\File::glob('public/icons/*.png');
        Storage::delete($filesForDelete);

        $this->line('Existing icons deleted');
    }

    /**
     * Generate icons for seeded accounts
     */
    protected function generateIcons() : void
    {
        IconGenerator::generateIcon('amazon', IconGenerator::AMAZON);
        IconGenerator::generateIcon('apple', IconGenerator::APPLE);
        IconGenerator::generateIcon('dropbox', IconGenerator::DROPBOX);
        IconGenerator::generateIcon('facebook', IconGenerator::FACEBOOK);
        IconGenerator::generateIcon('github', IconGenerator::GITHUB);
        IconGenerator::generateIcon('google', IconGenerator::GOOGLE);
        IconGenerator::generateIcon('instagram', IconGenerator::INSTAGRAM);
        IconGenerator::generateIcon('linkedin', IconGenerator::LINKEDIN);
        IconGenerator::generateIcon('twitter', IconGenerator::TWITTER);

        $this->line('Icons regenerated');
    }

    /**
     * Reset DB
     */
    protected function resetDB(string $seeder) : void
    {
        $this->flushDB();
        $this->seedDB($seeder);
    }

    /**
     * Delete all DB tables
     */
    protected function flushDB() : void
    {
        // Reset the db
        DB::table(config('auth.passwords.users.table'))->delete();
        DB::table('oauth_access_tokens')->delete();
        DB::table('oauth_personal_access_clients')->delete();
        DB::table('oauth_refresh_tokens')->delete();
        DB::table('webauthn_credentials')->delete();
        DB::table(config('auth.passwords.webauthn.table'))->delete();
        DB::table('twofaccounts')->delete();
        DB::table('groups')->delete();
        DB::table('users')->delete();
        DB::table('options')->delete();
        DB::table('auth_logs')->delete();

        $this->line('Database cleaned');
    }

    /**
     * Seed the DB
     */
    protected function seedDB(string $seeder) : void
    {
        $this->callSilent('db:seed', [
            '--class' => $seeder,
            '--no-interaction' => 1
        ]);

        $this->line('Database seeded');
    }
}
