<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

use Carbon\Carbon;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        require __DIR__ . '/questionires.php';
        $date = Carbon::now()->format('Y-m-d H:i:s');
        foreach ($questionArray as $key => $value) {
            //insert question groups here with $key

            $questionGroups = DB::table('question_groups')->insertGetId(
                array(
                    'title'   => $key,
                    "created_at" => $date,
                    "updated_at" => $date
                )
            );
            $type = '';
            $options = [];
            foreach ($value as $question => $type) {
                //insert question title here $question

                if (is_array($type)) {
                    //check if type is radio then add question options
                    foreach ($type as $typeR => $options) {
                        //insert option with type here
                        $type = $typeR;
                        $options = $options;
                    }
                } else {
                    //insert type onlye without options here
                    $type = $type;
                }
                $questions = DB::table('questions')->insert(
                    array(
                        'question'   => $question,
                        'type' => $type,
                        'options' => serialize($options),
                        "created_at" => $date,
                        "updated_at" => $date,
                        "group_id" => $questionGroups
                    )
                );
            }
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        //
    }
};
