<?php

namespace Database\Seeders;

use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * All seeding runs inside a single database transaction. SQLite commits
     * and fsyncs on every individual INSERT by default, which made the
     * factory-heavy seeders (applications, memberships, blog posts) spend
     * most of their time waiting on disk syncs. Batching the writes into one
     * transaction collapses hundreds of fsyncs into a single commit.
     */
    public function run(): void
    {
        if (app()->environment('local', 'development')) {
            $this->clearStorageDirectories();
        }

        DB::transaction(function (): void {
            if (app()->environment('local', 'development')) {
                $user = User::where('email', 'tony@testing.com')->first();

                if (! $user) {
                    User::factory()->admin()->receivesNotifications()->withProfilePicture()->create([
                        'first_name' => 'Tony',
                        'last_name' => 'Testing',
                        'email' => 'tony@testing.com',
                        'password' => 'testing',
                    ]);
                }
                User::factory()->admin()->receivesNotifications()->withProfilePicture()->create([
                    'first_name' => 'Tony',
                    'last_name' => 'Testing2',
                    'email' => 'tony2@testing.com',
                    'password' => 'testing',
                ]);
            }

            $this->call([
                AdminUserSeeder::class,
                SpeciesSeeder::class,
                MembershipPlanSeeder::class,
                FormQuestionSeeder::class,
                PetSeeder::class,
                AdoptionApplicationSeeder::class,
                FosteringApplicationSeeder::class,
                AssistanceApplicationSeeder::class,
                ApplicationStatusHistorySeeder::class,
                InterviewSeeder::class,
                SettingSeeder::class,
                TagSeeder::class,
                BlogPostSeeder::class,
                MenuSeeder::class,
                PageSeeder::class,
                MembershipSeeder::class,
                DrawSeeder::class,
                DemoUserSeeder::class,
                DocumentSeeder::class,
            ]);
        });
    }

    private function clearStorageDirectories(): void
    {
        $directories = [
            storage_path('app/private'),
            storage_path('app/public'),
        ];

        foreach ($directories as $directory) {
            if (! File::isDirectory($directory)) {
                continue;
            }

            $files = File::allFiles($directory);
            foreach ($files as $file) {
                if ($file->getFilename() !== '.gitignore') {
                    File::delete($file->getPathname());
                }
            }

            $subdirectories = File::directories($directory);
            foreach (array_reverse($subdirectories) as $subdirectory) {
                if (count(File::allFiles($subdirectory)) === 0 && count(File::directories($subdirectory)) === 0) {
                    File::deleteDirectory($subdirectory);
                }
            }
        }
    }
}
