<?php

namespace Database\Seeders;

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

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

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

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

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