<?php

namespace Database\Seeders;

use App\Models\Attachment;
use App\Models\Biography;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        DB::transaction(function () {
            $newUser = User::create([
                'role_id' => 1,
                'barbershop_id' => 1,
                'username' => 'yoga',
                'email' => 'yoga@developer.com',
                'password' => Hash::make(env('PASSWORD_DEFAULT_USER')),
                'entry_date' => now(),
            ]);

            Biography::create([
                'user_id' => $newUser->id,
                'full_name' => 'Yoga Pangestu',
                'pob' => 'Subang',
                'dob' => '2000-01-01',
                'contact_number' => '082121495806',
                'address' => 'Jl.Babaru Nagri',
                'gender' => '1',
            ]);

            $imagePath = public_path('assets/admin/images/avatar/speed.jpeg');

            if (file_exists($imagePath)) {
                $file = new \Symfony\Component\HttpFoundation\File\File($imagePath);

                $fileName = Str::uuid();

                Attachment::create([
                    'attachmentable_id' => $newUser->id,
                    'attachmentable_type' => User::class,
                    'name' => $fileName,
                    'extension' => $file->getExtension(),
                    'size' => $file->getSize(),
                    'type' => 'avatar',
                ]);

                $path = 'users/'.now()->format('Y-m-d');

                Storage::disk('public')->putFileAs($path, $file, $fileName.'.'.$file->getExtension());
            }
        });
    }
}
