<?php

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

class CreateFormFieldsTable extends Migration
{
    public function up()
    {
        Schema::create('form_fields', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('form_section_id');
            $table->enum('field_type', ['text', 'textarea', 'radio', 'checkbox', 'email']);
            $table->boolean('is_mandatory')->default(false);
            $table->enum('layout', ['single', 'double'])->default('single');
            $table->string('conditional_field_dependent_id', 200)->nullable();
            $table->nullableTimestamps();

            $table->unsignedBigInteger('backend_user_id')->nullable();
            $table->foreign('backend_user_id')->references('id')
                ->on('backend_users')->onDelete('SET NULL')->onUpdate('CASCADE');

            $table->foreign('form_section_id')->references('id')->on('form_sections')->onDelete('cascade');
            $table->index(['form_section_id']);
        });
    }

    public function down()
    {
        Schema::dropIfExists('form_fields');
    }
}
