<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Book;
use App\Models\Category;
use App\Services\DummyDataService;
use Illuminate\Support\Str;

class BookSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $books = DummyDataService::getAllBooks();

        foreach ($books as $data) {
            $category = Category::where('name', $data['kategori'])->first();

            Book::updateOrCreate(
                ['kode_buku' => $data['kode_buku']],
                [
                    'id' => $data['id'],
                    'judul' => $data['judul'],
                    'penulis' => $data['penulis'],
                    'cover' => $data['cover'],
                    'category_id' => $category ? $category->id : null,
                    'tahun' => $data['tahun'],
                    'status' => $data['status'],
                    'is_new' => $data['is_new'],
                    'tipe_akses' => is_array($data['tipe_akses']) ? $data['tipe_akses'] : [$data['tipe_akses']],
                ]
            );
        }
    }
}
