#!/usr/bin/env php
<?php

$workingPath = getcwd();

require __DIR__.'/../vendor/autoload.php';

$input = new Symfony\Component\Console\Input\ArgvInput();
$files = new Illuminate\Filesystem\Filesystem();

$version = ($input->hasParameterOption('--dev') && $input->hasParameterOption('--stable') === false) ? '8.x-dev' : '^8.0';

$files->deleteDirectory("{$workingPath}/skeleton");

Symfony\Component\Process\Process::fromShellCommandline(
    'composer create-project "laravel/laravel:'.$version.'" skeleton --no-install --no-scripts --no-plugins --quiet', $workingPath
)->mustRun();

Illuminate\Support\Collection::make([
    'app/Console/Kernel.php',
    'app/Exceptions/Handler.php',
    'app/Http/Kernel.php',
    'app/Http/Controllers/*.php',
    'app/Http/Middleware/*.php',
    'config/*.php',
    'resources/lang/en/*.php',
])->transform(fn ($file) => "{$workingPath}/skeleton/{$file}")
->map(fn ($file) => str_contains($file, '*') ? [...$files->glob($file)] : $file)
->flatten()
->each(function ($file) use ($files, $workingPath) {
    $files->copy($file, "{$workingPath}".Illuminate\Support\Str::after($file, "{$workingPath}/skeleton"));
});

transform([
    "// \Illuminate\Session\Middleware\AuthenticateSession::class," => "\Illuminate\Session\Middleware\AuthenticateSession::class,",
], fn ($changes) => $files->replaceInFile(array_keys($changes), array_values($changes), "{$workingPath}/app/Http/Kernel.php"));

transform([
    "env('APP_ENV', 'production')" => "env('APP_ENV', 'testing')",
    "    'asset_url' => env('ASSET_URL', null)," => "    'asset_url' => env('ASSET_URL', null),".PHP_EOL.PHP_EOL."    'mix_url' => env('MIX_ASSET_URL', null),",
    "        /*
         * Package Service Providers...
         */" => "        /*
         * Package Service Providers...
         */
        Laravel\Dusk\DuskServiceProvider::class,
        Otwell\CustomField\FieldServiceProvider::class,
        Otwell\IconsViewer\ToolServiceProvider::class,
        Otwell\RememberTokenCopier\AssetServiceProvider::class,
        Otwell\ResourceTool\ToolServiceProvider::class,
        Otwell\SidebarTool\ToolServiceProvider::class,",
    "        App\Providers\EventServiceProvider::class," => "        App\Providers\EventServiceProvider::class,".PHP_EOL."        App\Providers\NovaServiceProvider::class,"
], fn ($changes) => $files->replaceInFile(array_keys($changes), array_values($changes), "{$workingPath}/config/app.php"));

transform([
    "    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    ]," => "    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'web-subscribers' => [
            'driver' => 'session',
            'provider' => 'subscribers',
        ],
    ],",
    "        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ]," => "        'subscribers' => [
            'driver' => 'eloquent',
            'model' => \App\Models\Subscriber::class,
        ],"
], fn ($changes) => $files->replaceInFile(array_keys($changes), array_values($changes), "{$workingPath}/config/auth.php"));

transform([
    "        'single' => [
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'level' => env('LOG_LEVEL', 'debug'),
        ]," => "        'single' => [
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'level' => env('LOG_LEVEL', 'debug'),
        ],

        'deprecations' => [
            'driver' => 'single',
            'path' => storage_path('logs/deprecations.log'),
            'level' => env('LOG_LEVEL', 'debug'),
        ],"
], fn ($changes) => $files->replaceInFile(array_keys($changes), array_values($changes), "{$workingPath}/config/logging.php"));

$files->deleteDirectory("{$workingPath}/skeleton");

$files->deleteDirectory("{$workingPath}/lang/vendor/nova");
$files->deleteDirectory("{$workingPath}/public/vendor/nova");
$files->deleteDirectory("{$workingPath}/resources/lang/vendor/nova");
$files->deleteDirectory("{$workingPath}/resources/views/vendor/nova");

foreach ($files->glob('tests/Browser/*') as $file) {
    $files->delete($file);
}

$files->copy($workingPath.'/vendor/laravel/nova/tests/bootstrap.php', $workingPath.'/tests/bootstrap.php');
$files->copy($workingPath.'/vendor/laravel/nova/tests/DuskTestCase.php', $workingPath.'/tests/DuskTestCase.php');
$files->copy($workingPath.'/vendor/laravel/nova/tests/Concerns/DatabaseTruncation.php', $workingPath.'/tests/Concerns/DatabaseTruncation.php');

Symfony\Component\Process\Process::fromShellCommandline(
    'cp -rf ./vendor/laravel/nova/tests/Browser/* ./tests/Browser/', $workingPath
)->mustRun();

transform([
    'return realpath(__DIR__.\'/../vendor/laravel/nova-dusk-suite\');' => 'return realpath(__DIR__.\'/../\');',
], fn ($changes) => $files->replaceInFile(array_keys($changes), array_values($changes), "{$workingPath}/tests/DuskTestCase.php"));

Symfony\Component\Process\Process::fromShellCommandline(
    'php artisan nova:publish --force', $workingPath
)->mustRun();
