<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('posts', function (Blueprint $t) {
            if (! Schema::hasColumn('posts', 'hours_per_day')) {
                $t->decimal('hours_per_day', 5, 1)->default(10)->after('color');
            }
            if (! Schema::hasColumn('posts', 'description')) {
                $t->string('description', 255)->nullable()->after('hours_per_day');
            }
        });

        Schema::table('parts', function (Blueprint $t) {
            if (! Schema::hasColumn('parts', 'hidden_markup_pct')) {
                $t->decimal('hidden_markup_pct', 5, 2)->nullable()->after('sell_price');
            }
        });
    }

    public function down(): void
    {
        Schema::table('posts', function (Blueprint $t) {
            foreach (['hours_per_day', 'description'] as $col) {
                if (Schema::hasColumn('posts', $col)) {
                    $t->dropColumn($col);
                }
            }
        });
        Schema::table('parts', function (Blueprint $t) {
            if (Schema::hasColumn('parts', 'hidden_markup_pct')) {
                $t->dropColumn('hidden_markup_pct');
            }
        });
    }
};
