<?php

namespace Database\Seeders;

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

class AttributeTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $this->seedAttributes();
        $this->seedAttributeValues();
        $this->seedProductAttributes();
        $this->seedAttributeValueProductAttribute();
        $this->seedAttributeCategory();
    }

    /**
     * Seed "attributes" table
     */
    public function seedAttributes(): void
    {
        $datas = json_decode(File::get('database/data/attributes.json'));

        $table = 'attributes';
        foreach ($datas as $data) {
            try {
                DB::table($table)->insert([
                    'id' => $data->id,
                    'name' => $data->name,
                    'slug' => $data->slug,
                    'description' => $data->description,
                    'type' => $data->type,
                    'is_visible' => $data->is_enabled,
                    'is_required' => $data->is_required,
                    'is_searchable' => $data->is_searchable,
                    'is_filterable' => $data->is_filterable,
                ]);
            } catch (Exception $e) {
                info(['attributes error: ' => $e->getMessage()]);
            }
        }

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

    /**
     * Seed "attribute_values" table
     */
    public function seedAttributeValues(): void
    {
        $datas = json_decode(File::get('database/data/attribute_values.json'));

        $table = 'attribute_values';
        foreach ($datas as $data) {
            if (DB::table($table)->where('key', $data->key)->exists()) {
                continue;
            }

            try {
                DB::table($table)->insert([
                    'id' => $data->id,
                    'value' => $data->value,
                    'key' => $data->key,
                    'attribute_id' => $data->attribute_id,
                    'created_at' => $data->created_at,
                    'updated_at' => $data->updated_at,
                ]);
            } catch (Exception $e) {
                info(['attributes error: ' => $e->getMessage()]);
            }
        }

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

    /**
     * Seed "product_attributes" table
     */
    public function seedProductAttributes(): void
    {
        $datas = json_decode(File::get('database/data/product_attributes.json'));

        $table = 'product_attributes';
        foreach ($datas as $data) {
            try {
                DB::table($table)->insert([
                    'id' => $data->id,
                    'product_id' => $data->product_id,
                    'attribute_id' => $data->attribute_id,
                ]);
            } catch (Exception $e) {
                info(['Ignored product attribute', $data->id]);
            }
        }

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

    /**
     * Seed "attribute_value_product_attribute" table
     */
    public function seedAttributeValueProductAttribute(): void
    {
        $datas = json_decode(File::get('database/data/attribute_value_product_attribute.json'));

        $table = 'attribute_value_product_attribute';
        foreach ($datas as $data) {
            try {
                DB::table($table)->insert([
                    'id' => $data->id,
                    'attribute_value_id' => $data->attribute_value_id,
                    'product_attribute_id' => $data->product_attribute_id,
                    'product_custom_value' => $data->product_custom_value,
                ]);
            } catch (Exception $e) {
                info(['Ignored attribute value product attribute', $data->id]);
            }
        }

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

    /**
     * Seed "attribute_category" table
     */
    public function seedAttributeCategory(): void
    {
        $datas = json_decode(File::get('database/data/attribute_category.json'));

        $table = 'attribute_category';
        foreach ($datas as $data) {
            try {
                DB::table($table)->insert([
                    'id' => $data->id,
                    'attribute_id' => $data->attribute_id,
                    'category_id' => $data->category_id,
                ]);
            } catch (Exception $e) {
                info(['error attribute' => $e->getMessage()]);
            }
        }

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