<?php

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

class AddCurtainToTailoringProducts extends Migration
{
    public function up()
    {
        Schema::create('curtains', function (Blueprint $table) {
            $table->id();
            $table->string('fabric_type');
            $table->string('netting_type');
            $table->unsignedInteger('panel_count');
            $table->decimal('panel_meter', 10, 2);
            $table->decimal('middle_netting_meter', 10, 2);
            $table->string('pleating_type');
            $table->string('ring_stitch_type');
            $table->string('curtain_model');
            $table->unsignedInteger('mattress_count');
            $table->unsignedInteger('pillow_count');
            $table->unsignedInteger('bolster_count');
            $table->string('mattress_pillow_size');
            $table->string('pillow_mattress_model');
            $table->text('curtain_order_details')->nullable();
            $table->unsignedInteger('qty')->default(1);
            $table->boolean('is_paid')->default(false);
            $table->unsignedBigInteger('tailor_id');
        });

        Schema::table('tailoring_products', function (Blueprint $table) {
            $table->unsignedBigInteger('curtain_id')->nullable()->after('dress_id');
        });

        if (Schema::hasTable('tailoring_categories')) {
            $exists = DB::table('tailoring_categories')
                ->where('name', 'Curtain')
                ->where('is_active', true)
                ->exists();

            if (!$exists) {
                DB::table('tailoring_categories')->insert([
                    'name' => 'Curtain',
                    'is_active' => true,
                    'parent_id' => null,
                    'created_at' => now(),
                    'updated_at' => now(),
                ]);
            }
        }
    }

    public function down()
    {
        Schema::table('tailoring_products', function (Blueprint $table) {
            if (Schema::hasColumn('tailoring_products', 'curtain_id')) {
                $table->dropColumn('curtain_id');
            }
        });

        Schema::dropIfExists('curtains');
    }
}
