<commit_before><?php

namespace Yab\Quarx\Console;

use Artisan;
use Illuminate\Support\Str;
use Illuminate\Support\Schema;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Illuminate\Filesystem\Filesystem;

class Setup extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'quarx:setup';

    protected $files;

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Quarx will setup your site';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function fire()
    {
        Artisan::call('vendor:publish', [
            '--provider' => 'Yab\Quarx\QuarxProvider',
            '--force' => true
        ]);

        Artisan::call('vendor:publish', [
            '--provider' => 'Yab\Laracogs\LaracogsProvider',
            '--force' => true
        ]);

        $this->line("Are you sure you want to run the setup? (yes/no)");
        Artisan::call('laracogs:starter');

        $files = [
            base_path('database/factories/ModelFactory.php'),
            base_path('config/auth.php'),
        ];

        foreach ($files as $file) {
            $contents = file_get_contents($file);
            $contents = str_replace('App\User::class', 'App\Repositories\User\User::class', $contents);
            file_put_contents($file, $contents);
        }

        // Route setup
        $routeContents = file_get_contents(app_path('Providers/RouteServiceProvider.php'));
        $routeContents = str_replace("require app_path('Http/routes.php');", "require app_path('Http/routes.php');\n\t\t\trequire app_path('Http/quarx-routes.php');", $routeContents);
        file_put_contents(app_path('Providers/RouteServiceProvider.php'), $routeContents);

        $routeToDashboardContents = file_get_contents(app_path('Http/routes.php'));
        $routeToDashboardContents = str_replace("Route::get('/dashboard', 'PagesController@dashboard');", "Route::get('/dashboard', function(){ return Redirect::to('quarx/dashboard'); });", $routeToDashboardContents);
        file_put_contents(app_path('Http/routes.php'), $routeToDashboardContents);

        // Kernel setup
        $routeContents = file_get_contents(app_path('Http/Kernel.php'));
        $routeContents = str_replace("'auth' => \App\Http\Middleware\Authenticate::class,", "'auth' => \App\Http\Middleware\Authenticate::class,\n\t\t'quarx' => \App\Http\Middleware\Quarx::class,\n\t\t'admin' => \App\Http\Middleware\Admin::class,", $routeContents);
        file_put_contents(app_path('Http/Kernel.php'), $routeContents);

        $fileSystem = new Filesystem;

        $files = $fileSystem->allFiles(__DIR__.'/../PublishedAssets/Setup');

        foreach ($files as $file) {
            $newFileName = str_replace(__DIR__.'/../PublishedAssets/Setup', '', $file);
            if (is_dir($file)) {
                $fileSystem->copyDirectory($file, base_path($newFileName));
            } else {
                @mkdir(base_path(str_replace(basename($newFileName), '', $newFileName)), 0755, true);
                $fileSystem->copy($file, base_path($newFileName));
            }
        }

        // AuthProviders
        $authProviderContents = file_get_contents(app_path('Providers/AuthServiceProvider.php'));
        $authProviderContents = str_replace("\$this->registerPolicies(\$gate);", "\$this->registerPolicies(\$gate);\n\t\t\$gate->define('quarx', function (\$user) {\n\t\t\treturn (\$user->roles->first()->name === 'admin');\n\t\t});\n\t\t\$gate->define('admin', function (\$user) {\n\t\t\treturn (\$user->roles->first()->name === 'admin');\n\t\t});", $authProviderContents);
        file_put_contents(app_path('Providers/AuthServiceProvider.php'), $authProviderContents);

        // Remove the teams
        @unlink(app_path('Http/Controllers/PagesController.php'));
        @unlink(app_path('Http/Controllers/TeamController.php'));
        @unlink(app_path('Http/Requests/TeamRequest.php'));
        @unlink(app_path('Http/Requests/UpdateTeamRequest.php'));
        @unlink(app_path('Repositories/Team/Team.php'));
        @unlink(app_path('Repositories/Team/TeamRepository.php'));
        @rmdir(app_path('Repositories/Team'));
        @unlink(app_path('Services/TeamService.php'));
        @unlink(base_path('resources/views/dashboard.blade.php'));
        @unlink(base_path('resources/views/dashboard/main.blade.php'));
        @unlink(base_path('resources/views/dashboard/panel.blade.php'));
        @unlink(base_path('resources/views/partials/navigation.blade.php'));
        @unlink(base_path('resources/views/team/create.blade.php'));
        @unlink(base_path('resources/views/team/edit.blade.php'));
        @unlink(base_path('resources/views/team/show.blade.php'));
        @unlink(base_path('resources/views/team/index.blade.php'));
        @rmdir(base_path('resources/views/team'));
        @rmdir(base_path('resources/views/dashboard'));

        $mainRoutes = file_get_contents(app_path('Http/routes.php'));
        $mainRoutes = str_replace("/*
    |--------------------------------------------------------------------------
    | Team Routes
    |--------------------------------------------------------------------------
    */

    Route::get('team/{name}', 'TeamController@showByName');
    Route::resource('teams', 'TeamController', ['except' => ['show']]);
    Route::post('teams/search', 'TeamController@search');
    Route::post('teams/{id}/invite', 'TeamController@inviteMember');
    Route::get('teams/{id}/remove/{userId}', 'TeamController@removeMember');
", "", $mainRoutes);
        file_put_contents(app_path('Http/routes.php'), $mainRoutes);

        $userModel = file_get_contents(app_path('Repositories/User/User.php'));
        $userModel = str_replace("/**
     * Teams
     *
     * @return Relationship
     */
    public function teams()
    {
        return \$this->belongsToMany(Team::class);
    }

    /**
     * Team member
     *
     * @return boolean
     */
    public function isTeamMember(\$id)
    {
        \$teamIds = [];

        foreach (\$this->teams->toArray() as \$team) {
            \$teamIds[] = \$team['id'];
        }

        return in_array(\$id, \$teamIds);
    }

    /**
     * Team admin
     *
     * @return boolean
     */
    public function isTeamAdmin(\$id)
    {
        \$team = \$this->teams->find(\$id);
        return (int)\$team->user_id === (int)\$this->id;
    }
", "", $userModel);
        file_put_contents(app_path('Repositories/User/User.php'), $userModel);

        $userService = file_get_contents(app_path('Services/UserService.php'));
        $userService = str_replace("/*
|--------------------------------------------------------------------------
| Teams
|--------------------------------------------------------------------------
*/

public function joinTeam(\$teamId, \$userId)
{
   return \$this->userRepo->joinTeam(\$teamId, \$userId);
}

public function leaveTeam(\$teamId, \$userId)
{
   return \$this->userRepo->leaveTeam(\$teamId, \$userId);
}

public function leaveAllTeams(\$userId)
{
   return \$this->userRepo->leaveAllTeams(\$userId);
}", "", $userService);
        file_put_contents(app_path('Services/UserService.php'), $userService);

        // Remove the dashboard

        //

        Artisan::call('migrate:reset', [
            '--force' => true
        ]);

        Artisan::call('migrate', [
            '--seed' => true,
            '--force' => true
        ]);

        $this->info("Finished setting up your site with Quarx");
        $this->line("You can now login with the following username and password:");
        $this->comment("<EMAIL>");
        $this->comment("admin");
    }

    /**
     * Get the console command options.
     *
     * @return array<commit_msg>Needs composer dump<commit_after><?php

namespace Yab\Quarx\Console;

use Artisan;
use Illuminate\Support\Str;
use Illuminate\Support\Schema;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Illuminate\Filesystem\Filesystem;

class Setup extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'quarx:setup';

    protected $files;

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Quarx will setup your site';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function fire()
    {
        Artisan::call('vendor:publish', [
            '--provider' => 'Yab\Quarx\QuarxProvider',
            '--force' => true
        ]);

        Artisan::call('vendor:publish', [
            '--provider' => 'Yab\Laracogs\LaracogsProvider',
            '--force' => true
        ]);

        $this->line("Are you sure you want to run the setup? (yes/no)");

        Artisan::call('laracogs:starter');

        $files = [
            base_path('database/factories/ModelFactory.php'),
            base_path('config/auth.php'),
        ];

        foreach ($files as $file) {
            $contents = file_get_contents($file);
            $contents = str_replace('App\User::class', 'App\Repositories\User\User::class', $contents);
            file_put_contents($file, $contents);
        }

        // Route setup
        $routeContents = file_get_contents(app_path('Providers/RouteServiceProvider.php'));
        $routeContents = str_replace("require app_path('Http/routes.php');", "require app_path('Http/routes.php');\n\t\t\trequire app_path('Http/quarx-routes.php');", $routeContents);
        file_put_contents(app_path('Providers/RouteServiceProvider.php'), $routeContents);

        $routeToDashboardContents = file_get_contents(app_path('Http/routes.php'));
        $routeToDashboardContents = str_replace("Route::get('/dashboard', 'PagesController@dashboard');", "Route::get('/dashboard', function(){ return Redirect::to('quarx/dashboard'); });", $routeToDashboardContents);
        file_put_contents(app_path('Http/routes.php'), $routeToDashboardContents);

        // Kernel setup
        $routeContents = file_get_contents(app_path('Http/Kernel.php'));
        $routeContents = str_replace("'auth' => \App\Http\Middleware\Authenticate::class,", "'auth' => \App\Http\Middleware\Authenticate::class,\n\t\t'quarx' => \App\Http\Middleware\Quarx::class,\n\t\t'admin' => \App\Http\Middleware\Admin::class,", $routeContents);
        file_put_contents(app_path('Http/Kernel.php'), $routeContents);

        $fileSystem = new Filesystem;

        $files = $fileSystem->allFiles(__DIR__.'/../PublishedAssets/Setup');

        foreach ($files as $file) {
            $newFileName = str_replace(__DIR__.'/../PublishedAssets/Setup', '', $file);
            if (is_dir($file)) {
                $fileSystem->copyDirectory($file, base_path($newFileName));
            } else {
                @mkdir(base_path(str_replace(basename($newFileName), '', $newFileName)), 0755, true);
                $fileSystem->copy($file, base_path($newFileName));
            }
        }

        // AuthProviders
        $authProviderContents = file_get_contents(app_path('Providers/AuthServiceProvider.php'));
        $authProviderContents = str_replace("\$this->registerPolicies(\$gate);", "\$this->registerPolicies(\$gate);\n\t\t\$gate->define('quarx', function (\$user) {\n\t\t\treturn (\$user->roles->first()->name === 'admin');\n\t\t});\n\t\t\$gate->define('admin', function (\$user) {\n\t\t\treturn (\$user->roles->first()->name === 'admin');\n\t\t});", $authProviderContents);
        file_put_contents(app_path('Providers/AuthServiceProvider.php'), $authProviderContents);

        // Remove the teams
        @unlink(app_path('Http/Controllers/PagesController.php'));
        @unlink(app_path('Http/Controllers/TeamController.php'));
        @unlink(app_path('Http/Requests/TeamRequest.php'));
        @unlink(app_path('Http/Requests/UpdateTeamRequest.php'));
        @unlink(app_path('Repositories/Team/Team.php'));
        @unlink(app_path('Repositories/Team/TeamRepository.php'));
        @rmdir(app_path('Repositories/Team'));
        @unlink(app_path('Services/TeamService.php'));
        @unlink(base_path('resources/views/dashboard.blade.php'));
        @unlink(base_path('resources/views/dashboard/main.blade.php'));
        @unlink(base_path('resources/views/dashboard/panel.blade.php'));
        @unlink(base_path('resources/views/partials/navigation.blade.php'));
        @unlink(base_path('resources/views/team/create.blade.php'));
        @unlink(base_path('resources/views/team/edit.blade.php'));
        @unlink(base_path('resources/views/team/show.blade.php'));
        @unlink(base_path('resources/views/team/index.blade.php'));
        @rmdir(base_path('resources/views/team'));
        @rmdir(base_path('resources/views/dashboard'));

        $mainRoutes = file_get_contents(app_path('Http/routes.php'));
        $mainRoutes = str_replace("/*
    |--------------------------------------------------------------------------
    | Team Routes
    |--------------------------------------------------------------------------
    */

    Route::get('team/{name}', 'TeamController@showByName');
    Route::resource('teams', 'TeamController', ['except' => ['show']]);
    Route::post('teams/search', 'TeamController@search');
    Route::post('teams/{id}/invite', 'TeamController@inviteMember');
    Route::get('teams/{id}/remove/{userId}', 'TeamController@removeMember');
", "", $mainRoutes);
        file_put_contents(app_path('Http/routes.php'), $mainRoutes);

        $userModel = file_get_contents(app_path('Repositories/User/User.php'));
        $userModel = str_replace("/**
     * Teams
     *
     * @return Relationship
     */
    public function teams()
    {
        return \$this->belongsToMany(Team::class);
    }

    /**
     * Team member
     *
     * @return boolean
     */
    public function isTeamMember(\$id)
    {
        \$teamIds = [];

        foreach (\$this->teams->toArray() as \$team) {
            \$teamIds[] = \$team['id'];
        }

        return in_array(\$id, \$teamIds);
    }

    /**
     * Team admin
     *
     * @return boolean
     */
    public function isTeamAdmin(\$id)
    {
        \$team = \$this->teams->find(\$id);
        return (int)\$team->user_id === (int)\$this->id;
    }
", "", $userModel);
        file_put_contents(app_path('Repositories/User/User.php'), $userModel);

        $userService = file_get_contents(app_path('Services/UserService.php'));
        $userService = str_replace("/*
|--------------------------------------------------------------------------
| Teams
|--------------------------------------------------------------------------
*/

public function joinTeam(\$teamId, \$userId)
{
   return \$this->userRepo->joinTeam(\$teamId, \$userId);
}

public function leaveTeam(\$teamId, \$userId)
{
   return \$this->userRepo->leaveTeam(\$teamId, \$userId);
}

public function leaveAllTeams(\$userId)
{
   return \$this->userRepo->leaveAllTeams(\$userId);
}", "", $userService);
        file_put_contents(app_path('Services/UserService.php'), $userService);

        exec('composer dump');

        Artisan::call('migrate:reset', [
            '--force' => true
        ]);

        Artisan::call('migrate', [
            '--seed' => true,
            '--force' => true
        ]);

        $this->info("Finished setting up your site with Quarx");
        $this->line("You can now login with the following username and password:");
        $this->comment("<EMAIL>");
        $this->comment("admin");
    }

    /**
     * Get the console command options.
     *
     * @return array
{'fraction_non_alphanumeric': 0.20704651455943526, 'fraction_numerical': 0.0005042228665069961, 'mean_word_length': 2.596328195829556, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2810603', 'n_tokens_mistral': 4774, 'n_tokens_neox': 4752, 'n_words': 646}