<?php

namespace Database\Seeders;

use App\Models\Legal\LegalPage;
use Exception;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;

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

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

        $table = 'legal_pages';
        DB::table($table)->truncate();

        try {
            foreach ($datas as $data) {
                LegalPage::create([
                    'slug' => $data->slug,
                    'title' => translatable($data->title),
                    'content' => $data->content ? translatable($data->content) : null,
                    'is_active' => $data->is_enabled,
                ]);
            }
        } catch (Exception $e) {
            info(['Ignore error', $e->getMessage()]);
        }
    }
}
