<?php

namespace Database\Seeders;

use App\Models\System\Settings\OS;
use Exception;
use File;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

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

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

        $table = 'carousels';

        DB::table($table)->truncate();

        try {
            foreach ($mobileCarousels as $mobileCarousel) {
                DB::table($table)->insert([
                    'id' => $mobileCarousel->id,
                    'app' => OS::MOBILE_APP,
                    'place' => $mobileCarousel->place,
                    'link' => $mobileCarousel->link,
                    'is_visible' => $mobileCarousel->is_visible,
                    'resource_id' => $mobileCarousel->banner_resource,
                    'resource_type' => $mobileCarousel->banner_type,
                    'sort_order' => $mobileCarousel->sort_order,
                ]);
            }
        } catch (Exception $e) {
            info(['Ignore error', $e->getMessage()]);
        }

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