<?php

namespace App\Services\Component;

use App\Models\ComponentField;
use App\Models\ComponentMaster;

class ComponentService
{

    public static function  ComponentCreate(array $data)
    {
        $prepard = collect($data);

        $master =  ComponentMaster::create([
            'name' => $prepard['name'],
            'set_from' => isset($prepard['set_from']) ? $prepard['set_from'] : null,
            'database' => isset($prepard['database']) ? $prepard['database'] : null,
            'limit' =>  isset($prepard['limit']) ?  $prepard['limit'] : null,

        ]);
        $fields = isset($prepard['field_name']) ? $prepard['field_name'] : [];
        $sub_fields = isset($prepard['sub_field_name']) ? $prepard['sub_field_name'] : [];
        foreach ($fields as $key => $field) {
            ComponentField::create([
                'component_id' => $master->id,
                'name' =>  $field,
                'type' =>  $prepard['field_type'][$key],
            ]);
        }

        foreach ($sub_fields as $key => $sub_field) {
            ComponentField::create([
                'component_id' => $master->id,
                'name' =>  $sub_field,
                'type' =>  $prepard['sub_field_type'][$key],
                'group' => 1,
            ]);
        }
        return true;
    }
    public static function  ComponentUpdate(array $data, $id)
    {

        $prepard = collect($data);

        $master = ComponentMaster::where('id', $id)->first();
        $master->update([
            'name' => $prepard['name'],
            'set_from' => isset($prepard['set_from']) ? $prepard['set_from'] : null,
            'database' => isset($prepard['database']) ? $prepard['database'] : null,
            'limit' =>  isset($prepard['limit']) ?  $prepard['limit'] : null,

        ]);

        $fields = isset($prepard['field_name']) ? $prepard['field_name'] : [];
        $sub_fields = isset($prepard['sub_field_name']) ? $prepard['sub_field_name'] : [];



        foreach ($fields as $key => $field) {
            $component =  ComponentField::where('id', isset($prepard['field_id'][$key]) ?  $prepard['field_id'][$key] : false)->first();
            if ($component) {
                $component->update([
                    'name' =>  $field,
                ]);
            } else {
                ComponentField::create([
                    'component_id' => $master->id,
                    'name' =>  $field,
                    'type' =>  $prepard['field_type'][$key],
                ]);
            }
        }
        $more = ComponentField::where('component_id', $master->id)
            ->whereNotNull('group')
            ->where('group', '!=', '1')
            ->groupBy('group',)
            ->select('group')
            ->get();

        $sub_field_count = 0;
        foreach ($sub_fields as $key => $sub_field) {
            $component_sub =  ComponentField::where('id', isset($prepard['sub_field_id'][$key]) ?  $prepard['sub_field_id'][$key] : false)->first();
            $sub_field_count = $key;
            if ($component_sub) {
                ComponentField::where('component_id', $component_sub->component_id)->where('name', $component_sub->name)
                    ->update([
                        'name' => $sub_field,
                    ]);
            } else {
                ComponentField::create([
                    'component_id' => $master->id,
                    'name' =>  $sub_field,
                    'type' =>  $prepard['sub_field_type'][$key],
                    'group' => 1,
                ]);

                if (!empty($more)) {
                    foreach ($more as $index => $value) {
                        ComponentField::create([
                            'component_id' => $master->id,
                            'name' =>  $sub_field,
                            'type' =>  $prepard['sub_field_type'][$sub_field_count],
                            'group' => $value->group,
                        ]);
                    }
                }
            }
        }


        // $Upload_all_sub_filed = ComponentField::where('component_id', $master->id)
        //     ->whereNotNull('group')
        //     ->where('group', '1')
        //     ->get();

        // $sub_fild_update_count = 0;
        // foreach ($Upload_all_sub_filed as $key => $value) {
        //     ComponentField::where('component_id', $value->component_id)->where('name', $value->)
        //         ->update([
        //             'name' => $sub_fields[$sub_fild_update_count],
        //         ]);
        //     $sub_fild_update_count++;
        //     if ($sub_fild_update_count == count($sub_fields)) {

        //         $sub_fild_update_count = 0;
        //     }
        // }
    }
}
