<?php

namespace Database\Seeders;

use File;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class CollectionTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $this->seedOldData();
    }

    /**
     * Seed old postshop data
     */
    public function seedOldData(): void
    {
        $datas = json_decode(File::get('database/data/collections.json'));

        $table = 'collections';
        foreach ($datas as $data) {
            DB::table($table)->insert([
                'id' => $data->id,
                'created_at' => $data->created_at,
                'updated_at' => $data->updated_at,
                'name' => $data->name,
                'slug' => $data->slug,
                'description' => $data->description,
                'sort_order' => $data->sort_order,
                'seo_title' => $data->seo_title,
                'seo_description' => $data->seo_description,
                'is_visible' => $data->shown_on_homescreen,
            ]);
        }

        DB::statement("
            SELECT setval('{$table}_id_seq', (SELECT MAX(id) from {$table}))
        ");
    }
}
