<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use App\Models\User;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        // 1. Matikan pengecekan foreign key sementara
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');

        // 2. Hapus semua data di tabel recipes dan users
        DB::table('recipes')->delete();
        DB::table('users')->delete();

        // 3. Reset auto-increment agar ID mulai dari 1 lagi
        DB::statement('ALTER TABLE recipes AUTO_INCREMENT = 1;');
        DB::statement('ALTER TABLE users AUTO_INCREMENT = 1;');

        // 4. Hidupkan kembali pengecekan foreign key
        DB::statement('SET FOREIGN_KEY_CHECKS=1;');

        // 5. Tambahkan data user baru
        User::insert([
            [
                'name' => 'Admin',
                'email' => 'admin@gmail.com',
                'email_verified_at' => now(),
                'password' => Hash::make('123'),
                'phone' => '1234567890',
                'address' => 'Jl. Admin No. 1',
                'profile_picture' => 'https://via.placeholder.com/150',
                'bio' => 'Saya adalah administrator.',
                'is_active' => true,
                'role' => 'admin',
                'preferences' => json_encode(['theme' => 'dark']),
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'name' => 'User 1',
                'email' => 'user1@gmail.com',
                'email_verified_at' => now(),
                'password' => Hash::make('123'),
                'phone' => '0987654321',
                'address' => 'Jl. User 1 No. 2',
                'profile_picture' => 'https://via.placeholder.com/150',
                'bio' => 'Saya adalah pengguna biasa.',
                'is_active' => true,
                'role' => 'user',
                'preferences' => json_encode(['theme' => 'light']),
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);

        // 6. Panggil seeder lain (misal, RecipesTableSeeder)
        $this->call(RecipesTableSeeder::class);
    }
}
