<?php

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

return new class extends Migration
{
    public function up(): void
    {
        $tables = [
            'paper_sizes',
            'paper_types',
            'paper_brands',
            'paper_gsms',
            'print_types',
            'color_types',
            'print_methods',
            'laminations',
            'lamination_sides',
            'bindings',
            'foldings',
            'corner_types',
            'other_cost_masters',
        ];

        foreach ($tables as $table) {
            if (Schema::hasTable($table) && !Schema::hasColumn($table, 'price')) {
                Schema::table($table, function (Blueprint $blueprint) {
                    $blueprint->decimal('price', 12, 2)->default(0)->after('description');
                });
            }
        }

        if (!Schema::hasTable('carbon_copies')) {
            Schema::create('carbon_copies', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->text('description')->nullable();
                $table->decimal('price', 12, 2)->default(0);
                $table->boolean('status')->default(1);
                $table->integer('sort_order')->default(0);
                $table->unsignedBigInteger('created_by')->nullable();
                $table->unsignedBigInteger('updated_by')->nullable();
                $table->timestamps();
            });

            $now = now();
            foreach (['1+1', '1+2', 'No'] as $i => $name) {
                DB::table('carbon_copies')->insert([
                    'name' => $name,
                    'description' => 'Bill Book carbon copy option',
                    'price' => 0,
                    'status' => 1,
                    'sort_order' => $i + 1,
                    'created_at' => $now,
                    'updated_at' => $now,
                ]);
            }
        }

        // Ensure Bill Book binding options exist in bindings master
        $bindingOptions = ['Pad', 'Wrapper', 'Binding', 'Stiff Binding'];
        foreach ($bindingOptions as $i => $name) {
            if (!DB::table('bindings')->where('name', $name)->exists()) {
                DB::table('bindings')->insert([
                    'name' => $name,
                    'description' => 'Bill Book binding option',
                    'price' => 0,
                    'status' => 1,
                    'sort_order' => 100 + $i,
                    'created_at' => now(),
                    'updated_at' => now(),
                ]);
            }
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('carbon_copies');

        $tables = [
            'paper_sizes',
            'paper_types',
            'paper_brands',
            'paper_gsms',
            'print_types',
            'color_types',
            'print_methods',
            'laminations',
            'lamination_sides',
            'bindings',
            'foldings',
            'corner_types',
            'other_cost_masters',
        ];

        foreach ($tables as $table) {
            if (Schema::hasTable($table) && Schema::hasColumn($table, 'price')) {
                Schema::table($table, function (Blueprint $blueprint) {
                    $blueprint->dropColumn('price');
                });
            }
        }
    }
};
